简体   繁体   English

单个文件VS导入/导出加载的模块中的JavaScript命名空间模块(例如requireJS,es6)

[英]JavaScript namespaced modules in single file VS import/export loaded modules (e.g. requireJS, es6)

I'm trying to figure the most efficient way to structure modules in our website that contains a lot of JavaScript namespaced modules in a single file (app.js) already. 我正在尝试找出最有效的方法来构建我们网站中的模块,该模块已经在单个文件(app.js)中包含许多JavaScript命名空间模块。

Currently things look a bit like this: 当前情况看起来像这样:

app.js app.js

OURAPP.mapsModule = (function() {
  return ...
})();
...
OURAPP.searchModule = (function() {
  return ...
})();

..and these are all contained within a single file. ..这些都包含在一个文件中。 So, when we want to use any of these modules we: 因此,当我们要使用这些模块中的任何一个时,我们:

search-page.js 搜索page.js

...
OURAPP.searchModule.search(query);
...

search.html search.html

...
<script src="js/main.js">
<script src="js/search-page.js">
...

We do have a lot of such modules. 我们确实有很多这样的模块。 However, I'm wondering whether we should be doing something with import/export modules: 但是,我想知道我们是否应该对导入/导出模块做一些事情:

Method #2 方法#2

searchModule.js searchModule.js

export default {
    ...
}

search-page.js 搜索page.js

import searchModule from "./js/searchModule";
...
searchModule.search(query);
...

I'm guessing that a single file namespaced modules will be faster for the page to load(?) as it's only a single file to download. 我猜测单个文件命名空间模块将使页面加载(?)更快,因为它只是一个文件下载。 But when I'm looking at testing frameworks (eg Jest) they give many examples where module script files are loaded in this manner. 但是,当我查看测试框架(例如Jest)时,它们给出了许多以这种方式加载模块脚本文件的示例。 Does it lend itself better to testing I'm wondering? 我想知道它是否更适合测试? Personally, I prefer this structure anyway but it's a bold change of direction from how the site has been built up until now and I'll need to good reason to suggest this. 就个人而言,无论如何我还是更喜欢这种结构,但是从网站的构建方式到现在,这是一个大胆的方向改变,我需要充分的理由来建议这一点。 This website's is pretty much all generated server-side with JavaScript just doing the show/hide, query APIs, etc, with the addition on scrips and JavaScript libraries such as jQuery, Bootstrap, Isotope when required on each page. 该网站几乎都是用JavaScript生成的所有服务器端,仅用于显示/隐藏,查询API等,并且在每页上都需要时,在脚本和JavaScript库(如jQuery,Bootstrap,Isotope)上进行了添加。

I've read around but can't find anything comparing exactly both methods here and reasons for and against either. 我已经阅读了很多,但是找不到确切地比较这两种方法以及支持和反对两种方法的任何信息。 Would appreciate any suggestions or helpful advice, thanks. 感谢任何建议或有用的建议,谢谢。

Don't default-export objects, use named exports instead: 不要默认导出对象,而是使用命名的导出:

// searchModule.js
export function search(…) {
  …
}

// search-page.js
import * as searchModule from "./js/searchModule";
…
searchModule.search(query);
…

I'm guessing that a single file namespaced modules will be faster for the page to load as it's only a single file to download. 我猜测单个文件命名空间模块将使页面加载更快,因为它只是一个文件下载。

Yes, that's true. 是的,这是真的。 However, do not let that affect your decision on how to structure your modules. 但是,不要让那影响您对如何构造模块的决定。 You should always modularise your code so that it is the cleanest possible. 您应该始终对代码进行模块化,以使其尽可能干净。 Put the maps stuff in a different module than the search stuff. 将地图内容放置在与搜索内容不同的模块中。

You will then use a bundler or packer tool to create a single minified JS file to download that contains all the modules that the respective page needs. 然后,您将使用捆绑程序打包程序工具来创建一个要下载的小型JS文件,其中包含相应页面所需的所有模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM