admin管理员组文章数量:1334684
I'm trying to configure my JS build to do next:
I'm using a JS variable to define the application root:
globals.js
define(function (require) {
"use strict";
return {
mainRoot: ""
//mainRoot: "http://localhost:3000" - local run
//mainRoot: "" - test server
};
});
During local development I'm using code without Grunt build and running Grunt only for test & production builds.
Grunt is running from the Maven plugin using mand-line configuration. So it is the only way to pass the environment variable.
pom.xml
<plugin>
<groupId>pl.allegro</groupId>
<artifactId>grunt-maven-plugin</artifactId>
<configuration>
<gruntOptions>
<gruntOption>--verbose</gruntOption>
</gruntOptions>
<target>build</target>
</configuration>
</plugin>
Grunt configuration is pretty simple and looks like this:
Gruntfile.js
grunt.registerTask('build', [
'karma',
'requirejs',
'concat',
'csso',
'copy',
'processhtml'
]);
The question:
How can I configure Grunt to change my variable in next way?
- Default value of
mainRoot
should behttp://localhost:3000
- Environment variable should be set via mand-line from the Maven plugin
- When running Grunt with
PROD
environment -mainRoot
should be changed to - When running Grunt with
TEST
environment -mainRoot
should be changed to
Thank you!
I'm trying to configure my JS build to do next:
I'm using a JS variable to define the application root:
globals.js
define(function (require) {
"use strict";
return {
mainRoot: "http://myapp."
//mainRoot: "http://localhost:3000" - local run
//mainRoot: "http://myapp-test." - test server
};
});
During local development I'm using code without Grunt build and running Grunt only for test & production builds.
Grunt is running from the Maven plugin using mand-line configuration. So it is the only way to pass the environment variable.
pom.xml
<plugin>
<groupId>pl.allegro</groupId>
<artifactId>grunt-maven-plugin</artifactId>
<configuration>
<gruntOptions>
<gruntOption>--verbose</gruntOption>
</gruntOptions>
<target>build</target>
</configuration>
</plugin>
Grunt configuration is pretty simple and looks like this:
Gruntfile.js
grunt.registerTask('build', [
'karma',
'requirejs',
'concat',
'csso',
'copy',
'processhtml'
]);
The question:
How can I configure Grunt to change my variable in next way?
- Default value of
mainRoot
should behttp://localhost:3000
- Environment variable should be set via mand-line from the Maven plugin
- When running Grunt with
PROD
environment -mainRoot
should be changed tohttp://myapp.
- When running Grunt with
TEST
environment -mainRoot
should be changed tohttp://myapp-test.
Thank you!
Share Improve this question asked Jun 12, 2015 at 7:18 Igor LuzhanovIgor Luzhanov 8401 gold badge8 silver badges15 bronze badges 3- Have you tried using a plugin like grunt-replace? – doldt Commented Jun 12, 2015 at 7:22
-
Yes, but it is not clear for me how to setup it with
grunt-env
or similar environment plugins. – Igor Luzhanov Commented Jun 12, 2015 at 7:24 - Frankly, there are countless of ways to do this - show us what you tried with grunt-env then? – doldt Commented Jun 12, 2015 at 7:29
1 Answer
Reset to default 10I find a bination of grunt-replace
and grunt-config
works well.
In your Gruntfile.js
, configure grunt-config
like this (see the README):
config: {
local: {
options: {
variables: {
mainroot: 'http://localhost:3000'
}
}
},
test: {
options: {
variables: {
mainroot: 'http://myapp-test.'
}
}
},
prod: {
options: {
variables: {
mainroot: 'http://myapp.'
}
}
}
}
In your globals.js
, create an @@
placeholder for grunt-replace
to find and replace:
define(function (require) {
"use strict";
return {
mainRoot: "@@MAINROOT"
};
});
In your Gruntfile.js
, configure grunt-replace
like this:
replace: {
my_target: {
options: {
patterns: [
{
match: 'MAINROOT',
replacement: '<%= grunt.config.get("mainroot") %>'
}
]
},
src: ... ,
dest: ...
}
}
Then create a mand-line option such as --env
, which will accept local
or test
or prod
, and will default to local
if omitted:
var envTarget = grunt.option('env') || 'local';
and update your build
task to use config
and replace
:
grunt.registerTask('build', [
'config:' + envTarget,
'replace',
'karma',
'requirejs',
'concat',
'csso',
'copy',
'processhtml'
]);
Now you can run Grunt from the mand-line with the new --env
option:
grunt build --env=local
grunt build --env=test
grunt build --env=prod
本文标签: javascriptChanging JS variable with Grunt for different environmentsStack Overflow
版权声明:本文标题:javascript - Changing JS variable with Grunt for different environments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742377515a2463458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论