RequireJS paths config saying “all modules are in this file”

白昼怎懂夜的黑 提交于 2019-12-21 22:47:05

问题


In RequireJS, it's possible to configure paths explicitly for given modules. For example, you can specify that for module foo the module bar should be loaded instead (file bar.js should be loaded):

require.config({
    paths: {
        "foo": "bar"
    }
});

How can I do the same, but for all modules?


I tried using an asterisk, but it will only create a mapping for module * literally:

require.config({
    paths: {
        "*": "bar"
    }
});

回答1:


According to your question and comment

The TypeScript compiler is able to compile multiple external modules into named AMD modules placed into a single output file. However, to effectively use those modules, RequireJS must be configured so that it knows where to find them.

There is a work-around can be applied. First of all, let's not define any module paths in config except the path for all modules.

require.config({
    paths: {
        "all": "path/to/all/modules"
    },

    deps: { "all" } // This to tell requireJS to load 'all' at initial state.
});

And then load the config/main file

<script data-main="scripts/main" src="scripts/require.js"></script>

From this requireJS will simply read all the define() blocks in modules.js. It will registering all the module names you got in the js file. For example if you got define('myModule', [function(){...}]); in your module.js. You can simply call to load it at any place without define a path for it.

For example some where in your code.

requirejs(['myModule', function(myModule){
    myModule.doSemething();
});

(NOTE: yes this answer is not trying to solve the original point of the question. But only try to solve the initial issue. If anyone find this answer useless feel free to down vote)



来源:https://stackoverflow.com/questions/36072622/requirejs-paths-config-saying-all-modules-are-in-this-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!