Javascript 使用全局变量在Grunt中设置生成输出路径
我有几个grunt任务,我试图在这些任务中共享全局变量,我遇到了一些问题 我已经编写了一些自定义任务,根据构建类型设置正确的输出路径。这似乎是正确的设置Javascript 使用全局变量在Grunt中设置生成输出路径,javascript,gruntjs,Javascript,Gruntjs,我有几个grunt任务,我试图在这些任务中共享全局变量,我遇到了一些问题 我已经编写了一些自定义任务,根据构建类型设置正确的输出路径。这似乎是正确的设置 // Set Mode (local or build) grunt.registerTask("setBuildType", "Set the build type. Either build or local", function (val) { // grunt.log.writeln(val + " :setBuildType va
// 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);
});
因此,我尝试在后续任务中引用global.outputPath,并遇到错误
如果我从命令行调用grunt test
,它将输出正确的路径
但是,如果我有这样的任务:
清洁:{
发布:{
src:global.outputPath
}
}
它抛出以下错误:
警告:无法调用未定义用法的方法“indexOf”——强制继续。
此外,setOutput任务中的常量设置在grunfile.js的顶部
有什么想法吗?我做错什么了吗?所以,我走的是正确的道路。问题是模块在设置这些全局变量之前导出,因此在initConfig()任务中定义的后续任务中都未定义这些变量 我提出的解决方案是覆盖grunt.option值,尽管可能更好 我的任务有一个可选选项--target 工作解决方案如下所示:
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"));
});
clean: {
build: {
src: ["<%= grunt.option(\"target\") %>"]
}
}
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 )
}
initConfig()中定义的任务如下所示:
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"));
});
clean: {
build: {
src: ["<%= grunt.option(\"target\") %>"]
}
}
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 )
}
clean:{
建造:{
src:[“”]
}
}
如果你有更好的解决方案,请随时插话。否则,这可能会对其他人有所帮助。我有一种方法,允许您使用
--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()
这是一个老问题,我只是想把我的5美分扔进去 如果您需要从任何任务访问配置变量,只需在主(您将始终加载的)配置文件中定义它,如下所示:
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"));
});
clean: {
build: {
src: ["<%= grunt.option(\"target\") %>"]
}
}
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 )
}
module.exports=函数(grunt)
{
//
//通用项目配置
//
变量配置=
{
pkg:grunt.file.readJSON('package.json'),
“项目”的选项:/
{
地区:
{
outputPath:“/lib”,
},
开发人员:
{
outputPath:“/build”,
},
},
}
grunt.config.merge(配置)
}
然后,您可以简单地访问如下值:
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"));
});
clean: {
build: {
src: ["<%= grunt.option(\"target\") %>"]
}
}
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 )
}
- 在配置文件中
...
我的想法:
[
在这里结束:'/bundle',
],
...
- 在任务中
//作为原始价值
grunt.config.data.options.dist.outputPath
//处理(最终)模板后
grunt.config('options.dist.outputPath')
我在这里使用key
options
只是为了符合惯例,但是您可以使用任何东西,只要您记得不注册名为'options'
的任务,或者您用于key:)的任何东西。我想它可能与在grunt.initConfig({})之外设置的global.outputPath有关,我试图访问grunt.initConfig({})中的变量,我喜欢全局设置构建的目标目录。另一种选择是,仅仅因为目标不同,就为每个流程定义单独的规则。(例如:sass:dev
,coffee:dev
,sass:dist
,coffee:dist
)。这是一种疼痛,而且不是很干燥。谢谢你做了这个腿的工作!如果定义了global.Outputpath
等于grunt.option('target')
,那么将grunt.option('target')
的值设置为global.Outputpath
,这一点让我感到困惑。你打算去那里干什么?啊。现在来看,如果在运行时设置了选项(“目标”),那么更好的方法是在该函数的第一行输入一个返回值。否则,我将global.Outputpath设置为grunt.option(“target”),这样,如果它们在运行时传入一个目标,它将覆盖所有默认值。哦,我现在明白了!您希望人们能够在外部指定它。酷!我知道这是一个非常古老的项目,但我在做一个老项目&试图使用grunt.config,但没有用。你的回答救了我…谢谢!你的Grunfile是coffescript吗?链接到设置/如何请:)很好的提示顺便说一句