Grunt任务可选地包含适用于不同环境的AMD模块 [英] Grunt task to optionally include an AMD module for different environment

查看:108
本文介绍了Grunt任务可选地包含适用于不同环境的AMD模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为AMD开发一个使用Require.js的web应用程序,并放大请求以抽象出我的AJAX调用。放大。请求的另一个好处是我定义了一个替代模块,其中包含我的请求的模拟版本,可以用于测试目的。目前,我通过简单地在我的main.js文件中评论/取消注释模块引用来切换我的请求模块的两个版本。

I'm developing a web app using Require.js for AMD and amplify.request to abstract away my AJAX calls. The other advantage to amplify.request is that I've defined an alternative module containing mocked versions of my requests that I can use for testing purposes. Currently, I'm switching between the two versions of my request module by simply commenting/un-commenting the module reference in my main.js file.

喜欢做的是使用Grunt来创建我的应用程序的不同版本,具体取决于我希望包含哪个模块。我也可以用它来开启或关闭我的调试模式。我想像的东西类似于 usemin ,仅用于JavaScript内的引用,而不是HTML。

What I'd love to do is use Grunt to create different builds of my app depending on which module I wanted included. I could also use it to do things like turn my debug mode on or off. I'm picturing something similar to usemin, only for references inside JavaScript, not HTML.

任何人都知道这样做的插件,或者有关于如何使用Grunt做的建议?

Anyone know of a plugin that does this, or have a suggestion about how I could do it with Grunt?

推荐答案

在我们当前的项目中,我们有几个不同的环境。对于它们中的每一个,我们都可以为requirejs构建指定不同的配置设置。
为了区分这些不同的环境,我使用了一个参数 target

On our current project we have a few different environments. For each of them, we can specify different configuration settings for the requirejs build. To distinguish between these different environments, I've used a parameter target.

通过将其附加到您的调用中来传递它,例如:

You can simply pass this to grunt by appending it to your call like

grunt --target=debug

你可以通过使用 grunt.option 来访问Gruntfile中的这个参数,就像

And you can access this parameter in the Gruntfile, by using grunt.option, like

var target = (grunt.option('target') || 'debug').toLowerCase();

上面的行默认为 debug 。然后,您可以使用requirejs的路径配置设置将构建指向正确的模块。示例代码如下。

The line above will default to debug. You could then make use of the paths configuration setting of requirejs to point the build to the correct module. Example code below.

requirejs: {
    compile: {
        options: {  
            paths: {
                "your/path/to/amplify/request": target === "debug" ? "path/to/mock" : "path/to/real",
                }   
            }
        }
}

这篇关于Grunt任务可选地包含适用于不同环境的AMD模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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