반응형
환경 변수를 gulp 작업으로 설정하려면 어떻게해야합니까?
NODE_ENV='production' gulp
환경 변수를 설정하기 위해 gulp를 실행할 때마다 추가 인수를 입력하고 싶지 않습니다 .
차라리 작업을 통해 gulp 내 에서 환경 변수 를 설정하고 싶습니다 .
이것을 달성하는 좋은 방법은 무엇입니까?
gulp.task('set-dev-node-env', function() {
return process.env.NODE_ENV = 'development';
});
gulp.task('set-prod-node-env', function() {
return process.env.NODE_ENV = 'production';
});
다음과 같이 사용하십시오.
gulp.task('build_for_prod', ['set-prod-node-env'], function() {
// maybe here manipulate config object
config.paths.src.scripts = config.paths.deploy.scripts;
runSequence(
'build',
's3'
);
});
gulp-env 시도
nodemon 태스크를 실행하기 전에 일부 환경 변수를 설정하는 방법에 대한 빠른 예 :
// gulpfile.js
var gulp = require('gulp');
var nodemon = require('nodemon');
var env = require('gulp-env');
gulp.task('nodemon', function() {
// nodemon server (just an example task)
});
gulp.task('set-env', function () {
env({
vars: {
MONGO_URI: "mongodb://localhost:27017/testdb-for-british-eyes-only",
PORT: 9001
}
})
});
gulp.task('default', ['set-env', 'nodemon'])
또한 스크립트로 정의 할 수 있습니다. package.json
{
"name": "myapp",
"scripts": {
"gulp": "NODE_ENV='development' gulp",
"gulp-build": "NODE_ENV='production' gulp"
},
...
}
그리고 npm run gulp-build
. 이것은 몇 가지 이점이 있습니다
- 매번 입력하는 대신 인수를 쉽게 정의 할 수 있습니다.
- 전역 gulp 설치가 필요하지 않습니다 (웹팩과 같은 다른 도구에도 동일).
- You can define multiple variants with different environment variables and(or) arguments without changing the gulpfile (as you can see above - gulp and gulp-build for development and production respectively)
You can also set one by default, and read the variables from a json
file:
gulp.task('set-env', function () {
var envId = gutil.env.env;
if (!envId) {
envId = "dev";
}
genv({
file: "env." + envId + ".json"
});
});
This would be always dev
env by default, and you could call it setting another env, like this:
gulp --env prod
More of gulp-env
You can Setup the environment like follows:
// Environment Setup
var env = process.env.NODE_ENV || 'development';
Then you can use the environment to process the code as follows:
gulp.task('js',function(){
gulp.src(jsPath)
.pipe(browserify({debug: env === 'development'}))
.pipe(gulpif(env === 'production' , uglify()))
.pipe(gulp.dest(jsDest));
});
참고URL : https://stackoverflow.com/questions/28787457/how-can-i-set-an-environment-variable-as-gulp-task
반응형
'Nice programing' 카테고리의 다른 글
Node.js-Express를 사용하여 원시 요청 본문 가져 오기 (0) | 2020.12.06 |
---|---|
사용자가 카메라 사용 권한을 부여했는지 확인하는 방법은 무엇입니까? (0) | 2020.12.06 |
Swift에서 뒤로 스 와이프 제스처 비활성화 (0) | 2020.12.06 |
"Project Navigator"를 클릭하면 Xcode 폴더가 자동으로 확장 됨 (0) | 2020.12.06 |
WPF의 텍스트 블록에 스트로크 적용 (0) | 2020.12.06 |