javascript 在 Grunt 中使用全局变量设置构建输出路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15647484/
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
Use Global Variable to Set Build Output Path in Grunt
提问by ThePuzzleMaster
I have a couple grunt tasks and I am trying to share global variables across those tasks and I am running into issues.
我有几个繁重的任务,我试图在这些任务之间共享全局变量,但我遇到了问题。
I have written a some custom tasks which set the proper output path depending on the build type. This seems to be setting things correctly.
我编写了一些自定义任务,根据构建类型设置正确的输出路径。这似乎是正确设置。
// Set Mode (local or build)
grunt.registerTask("setBuildType", "Set the build type. Either build or local", function (val) {
// grunt.log.writeln(val + " :setBuildType val");
global.buildType = val;
});
// SetOutput location
grunt.registerTask("setOutput", "Set the output folder for the build.", function () {
if (global.buildType === "tfs") {
global.outputPath = MACHINE_PATH;
}
if (global.buildType === "local") {
global.outputPath = LOCAL_PATH;
}
if (global.buildType === "release") {
global.outputPath = RELEASE_PATH;
}
if (grunt.option("target")) {
global.outputPath = grunt.option("target");
}
grunt.log.writeln("Output folder: " + global.outputPath);
});
grunt.registerTask("globalReadout", function () {
grunt.log.writeln(global.outputPath);
});
So, I'm trying to then reference global.outputPath in a subsequent task, and running into errors.
所以,我试图在随后的任务中引用 global.outputPath,并遇到错误。
If I call grunt test
from the command line, it outputs the correct path no problem.
如果我grunt test
从命令行调用,它输出正确的路径没问题。
However, if I have a task like this: clean: { release: { src: global.outputPath } }
但是,如果我有这样的任务: clean: { release: { src: global.outputPath } }
It throws the following error:
Warning: Cannot call method 'indexOf' of undefined Use --force to continue.
它引发以下错误:
Warning: Cannot call method 'indexOf' of undefined Use --force to continue.
Also, my constants in the setOutput task are set at the top of my Gruntfile.js
此外,我在 setOutput 任务中的常量设置在 Gruntfile.js 的顶部
Any thoughts? Am I doing something wrong here?
有什么想法吗?我在这里做错了吗?
回答by ThePuzzleMaster
So, I was on the right path. The issue is that the module exports before those global variables get set, so they are all undefined in subsequent tasks defined within the initConfig() task.
所以,我走在正确的道路上。问题是模块在设置这些全局变量之前导出,因此它们在 initConfig() 任务中定义的后续任务中都是未定义的。
The solution I came up with, although, there may be better, is to overwrite a grunt.option value.
我想出的解决方案,虽然,可能有更好的,是覆盖 grunt.option 值。
I have an optional option for my task --target
我的任务有一个可选选项 --target
working solution looks like this:
工作解决方案如下所示:
grunt.registerTask("setOutput", "Set the output folder for the build.", function () {
if (global.buildType === "tfs") {
global.outputPath = MACHINE_PATH;
}
if (global.buildType === "local") {
global.outputPath = LOCAL_PATH;
}
if (global.buildType === "release") {
global.outputPath = RELEASE_PATH;
}
if (grunt.option("target")) {
global.outputPath = grunt.option("target");
}
grunt.option("target", global.outputPath);
grunt.log.writeln("Output path: " + grunt.option("target"));
});
And the task defined in initConfig() looked like this:
initConfig() 中定义的任务如下所示:
clean: {
build: {
src: ["<%= grunt.option(\"target\") %>"]
}
}
Feel free to chime in if you have a better solution. Otherwise, perhaps this may help someone else.
如果您有更好的解决方案,请随时加入。否则,也许这可能会帮助其他人。
回答by SimplGy
I have a way to do this that allows you to specify the output path using values like --dev
. So far it's working very well, I quite like it. Thought I'd share it, as someone else may like it, too.
我有一种方法可以做到这一点,它允许您使用--dev
. 到目前为止,它运行得非常好,我非常喜欢它。我想我会分享它,因为其他人可能也喜欢它。
# Enum for target switching behavior
TARGETS =
dev: 'dev'
dist: 'dist'
# Configurable paths and globs
buildConfig =
dist: "dist"
dev: '.devServer'
timestamp: grunt.template.today('mm-dd_HHMM')
grunt.initConfig
cfg: buildConfig
cssmin:
crunch:
options: report: 'min'
files: "<%= grunt.option('target') %>/all-min.css": "/**/*.css"
# Set the output path for built files.
# Most tasks will key off this so it is a prerequisite
setPath = ->
if grunt.option 'dev'
grunt.option 'target', buildConfig.dev
else if grunt.option 'dist'
grunt.option 'target', "#{buildConfig.dist}/#{buildConfig.timestamp}"
else # Default path
grunt.option 'target', buildConfig.dev
grunt.log.writeln "Output path set to: `#{grunt.option 'target'}`"
grunt.log.writeln "Possible targets:"
grunt.log.writeln target for target of TARGETS
setPath()
With this setup, you can run commands like:
通过此设置,您可以运行以下命令:
grunt cssmin --dist #sent to dist target
grunt cssmin --dev #sent to dev target
grunt cssmin --dev #sent to default target (dev)
回答by nidalpres
This is an older question, I just thought to throw in my 5 cents.
这是一个较旧的问题,我只是想投入我的 5 美分。
If you need config variable to be accessible from any task, just define it in your main (the one that you'll always load) config file like this:
如果您需要可以从任何任务访问配置变量,只需在您的主(您将始终加载的)配置文件中定义它,如下所示:
module.exports = function(grunt)
{
//
// Common project configuration
//
var config =
{
pkg: grunt.file.readJSON('package.json'),
options: // for 'project'
{
dist:
{
outputPath: '<%= process.cwd() %>/lib',
},
dev:
{
outputPath: '<%= process.cwd() %>/build',
},
},
}
grunt.config.merge( config )
}
Then you can simply access value like this:
然后你可以简单地访问这样的值:
- in config file(s)
- 在配置文件中
...
my_thingie:
[
ends_up_here: '<%= options.dev.outputPath %>/bundle',
],
...
...
my_thingie:
[
ends_up_here: '<%= options.dev.outputPath %>/bundle',
],
...
- in tasks
- 在任务中
// as raw value
grunt.config.data.options.dist.outputPath
// after (eventual) templates have been processed
grunt.config('options.dist.outputPath')
// as raw value
grunt.config.data.options.dist.outputPath
// after (eventual) templates have been processed
grunt.config('options.dist.outputPath')
I used key options
here just to be in line with convention, but you can use anything as long as you remember not to register a task named 'options'
or whatever you used for the key :)
我options
在这里使用 key只是为了符合约定,但是你可以使用任何东西,只要你记得不要注册一个名为的任务'options'
或你用于 key 的任何东西:)