RequireJS路径配置说“所有模块都在此文件中” [英] RequireJS paths config saying "all modules are in this file"

查看:81
本文介绍了RequireJS路径配置说“所有模块都在此文件中”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RequireJS中,可以为给定模块显式配置路径。例如,您可以指定模块 foo 应该加载模块 bar (文件 bar.js 应加载):

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"
    }
});


推荐答案

根据您的问题和评论


TypeScript编译器能够将多个外部模块编译到放置在单个输出文件中的命名AMD模块中。但是,为了有效地使用这些模块,必须配置RequireJS以便它知道在哪里找到它们。

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.

有一种解决方法可以应用。首先,除了所有模块的路径之外,不要在 config 中定义任何模块路径。

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.
});

然后加载 config / main file

And then load the config/main file

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

从这个requireJS将只读取所有 define() c> modules.js 中的$ c>块。它将注册您在js文件中获得的所有模块名称。例如,如果你的 module.js define('myModule',[function(){...}]); C $ C>。您可以直接调用在任何地方加载它而无需为其定义路径。

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();
});

(注意:是的,这个答案并不是试图解决问题的原始点。但只是尝试解决最初的问题。如果有人发现这个答案没用,请随意投票。

这篇关于RequireJS路径配置说“所有模块都在此文件中”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆