使用RequireJS中的相同配置文件在并行目录下优化/构建模块 [英] Optimize/build modules under parallel directories using the same config file in RequireJS

查看:102
本文介绍了使用RequireJS中的相同配置文件在并行目录下优化/构建模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得标题可能不是解释性的:)

I have a feeling that the title just might not be explanatory :)

设置

假设我具有以下结构:

其中app.js文件是应用程序的主要引导/进入模块,如下所示:

where app.js files are main bootstrapping/entry modules for the applications that look like this:

app01

require.config({});

require([
    'app/component1.js'
], 
function(component){
    // do something with component1
});

app02

require.config({});

require([
    'app/component2.js'
], 
function(component){
    // do something with component2
});

都可以与相应的index.html文件一起使用.

which both work with appropriate index.html files.

我有一个 app01 的RequireJS构建配置文件(假定与路径相关的正确放置):

I have a RequireJS build configuration file (assume correct placement related to the paths) for app01:

({
    appDir: 'apps/app01',
    baseUrl: '.',
    dir: 'built_apps/app01',
    optimize: 'closure',
    paths: {
    },

    modules: [
        {
            name: 'app'
        }
    ]
})

效果很好.相似的文件(用app02替换app01)对于 app02 来说很好用.

which works just fine. Similar file (replacing app01 with app02) works just fine for app02.

问题/目标

现在,我希望能够为 app01 app02 使用相同构建配置文件的应用程序,最好不要实际按名称列出所有应用程序(因为编号和名称可能会随时间变化).

Now I want to be able to run RequireJS build tool (using Google Closure with Rhino under Ant, not that it really matters in this case) for both app01 and app02 applications using the same build configuration file and, preferably, without actually listing all the apps by name (since the number and names may vary over time).

基本上,我希望(或更希望)有这样的东西:

Basically I expect (or rather hope) to have something like this:

({
    appDir: 'apps',
    baseUrl: '.',
    dir: 'built_apps',
    optimize: 'closure',
    paths: {

    },

    modules: [
        {
            name: 'app*/app' // notice the wildcard
        }
    ]
})

,它将在built_apps目录上运行,找到app */app下的所有应用,并对每个应用进行优化.

which would run over built_apps directory, find all apps under app*/app and optimize each one of them.

我知道我可以使用Ant在每个应用程序上动态创建这样的构建配置文件,对它运行构建,然后清理,但是我宁愿拥有RequireJS解决方案.

I know I can use Ant to create such build configuration file on the fly per app, run build against it and then clean up, but I'd rather have RequireJS solution.

是否可以使用RequireJS做类似的事情?

推荐答案

RequireJS没有内置的通配符配置.一种或另一种方式,您将需要代码来执行此操作.我将观察到,您在这里要求的内容等同于将通配符转换为模块对象上的某种隐式迭代,类似于 mustache.js 提供了其模板. IMO,在这种情况下,这是一种相当脆弱且有限的方法.

There's no built-in wildcarding configuration for RequireJS. One way or another, you'll need code to do this. I'll observe that what you're asking for here amounts to translating a wildcard into some kind of implicit iteration on the module objects, akin to what mustache.js provides for its templates. IMO, that's a fairly brittle and limited approach in this context.

相反,我建议直接在构建配置中使用JavaScript以编程方式生成modules数组.回想一下,构建配置是JavaScript,而不仅仅是JSON数据结构.这为您提供了完善的脚本编写功能.

Instead, I recommend generating your modules array programmatically in JavaScript right in the build config. Recall, the build config is JavaScript not just a JSON data structure. This gives you sophisticated scripting capabilities there.

我已经在 requirejs-rails gem .这是一个示例要点,它显示了r.js在构建时会看到的内容.

I've done this kind of scripting in the build config for the requirejs-rails gem. Here's an example gist that shows what r.js would see at build time.

这篇关于使用RequireJS中的相同配置文件在并行目录下优化/构建模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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