javascript 单个文件中的RequireJS多个模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18308086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RequireJS multiple module in single file
提问by Steely Wing
I want to merge multiple modules into a single file, but I can't find an official document about how to do it. Now I'm using the below method, it works, but I'm wondering about the following:
我想将多个模块合并到一个文件中,但是我找不到有关如何执行此操作的官方文档。现在我正在使用下面的方法,它有效,但我想知道以下几点:
- Is it technically correct?
- How does RequireJS check whether a module is loaded or not? By using the module name or the file name?
- Will this solution init modules multiple times in some situation?
- 技术上正确吗?
- RequireJS 如何检查模块是否已加载?通过使用模块名还是文件名?
- 在某些情况下,此解决方案会多次初始化模块吗?
index.html
索引.html
<script src="require.js"></script>
<script>
requirejs.config({
paths: {
'a': 'test',
'b': 'test'
}
});
require(['a', 'b'], function () {
console.log('a & b loaded');
});
</script>
test.js
测试.js
console.log('loading test.js');
// I have some init here, like "Avoid `console` errors in IE"
define('a', function () {
console.log('a module');
});
define('b', function () {
console.log('b module');
});
回答by AlexCode
This is old but I just bumped into it. Sure you can have multiple define in the same file, and yes, it makes sense.
这是旧的,但我刚碰到它。当然你可以在同一个文件中有多个定义,是的,这是有道理的。
The only thing you need to do is to use named defines.
您唯一需要做的就是使用命名定义。
define('module1', ['dependency1', 'dependency2'], function (d1, d2) {
// your code here
}
Think that sometimes you don't need your resources to be loaded dynamically, having them loaded all at the beginning might actually be required, co creating a single concatenated/minified js file at packaging time is the best solution.
考虑到有时您不需要动态加载资源,实际上可能需要在开始时将它们全部加载,在打包时共同创建单个连接/缩小的 js 文件是最好的解决方案。
So you won't take advantage of the "lazy loading" but you still design your app in a modular way by using RequireJS.
因此,您不会利用“延迟加载”,但您仍然可以使用 RequireJS 以模块化方式设计您的应用程序。
回答by Prusdrum
Shangan is right. In http://requirejs.org/docs/api.html#definethere is a part that says:
山安说得对。在http://requirejs.org/docs/api.html#define 中有一段说:
There should only be one module definition per file on disk.
磁盘上的每个文件应该只有一个模块定义。
But if you want to do it anyway I found something like that: It is from official documentation: http://requirejs.org/docs/api.html#config-bundles
但是如果你无论如何都想这样做,我发现了类似的东西:它来自官方文档:http: //requirejs.org/docs/api.html#config-bundles
It says that you can define in which file you can find which modules by bundles
property. For example: I have file called
modules.jswith such content:
它说您可以定义在哪个文件中可以通过bundles
属性找到哪些模块。例如:我有一个名为modules.js 的文件,
其中包含以下内容:
define('module_one', function(){
return 'Hello. I am module one';
})
define('module_two', function(){
return 'Hello. I am module two';
})
Then in my main.jsfile I do something like this:
然后在我的main.js文件中我做这样的事情:
requirejs.config({
baseUrl : 'modules/',
bundles: {
'modules': ['module_one', 'module_two']
}
})
require(['module_one', 'module_two'], function(moduleOne, moduleTwo){
alert(moduleOne);
alert(moduleTwo);
})
And it works like I want to.
它像我想要的那样工作。
回答by Shankar Ganesh Jayaraman
is that correct ?
那是对的吗 ?
NO. There should only be one module definition per file on disk.
不。磁盘上的每个文件应该只有一个模块定义。
how do RequireJS check a module loaded or not ? using the module name or file name ?
RequireJS 如何检查模块加载与否?使用模块名或文件名?
Using the file name.
使用文件名。
will that init multiple times in some situation ?
在某些情况下会多次初始化吗?
The modules can be grouped into optimized bundles by the optimization tool.
可以通过优化工具将模块分组为优化包。
http://requirejs.org/docs/optimization.html
http://requirejs.org/docs/optimization.html
Example of Hello World module is given here.
此处给出了 Hello World 模块的示例。