Gulp task to create a DEV and PROD version of main.js

我们两清 提交于 2019-12-08 05:07:41

问题


Im new to Gulp and have been playing with gulp-minify and gulp-uglify to create an overall main.js file via gulp-requirejs.

However, i need to create 2 versions of this main.js file, one for my DEV environment, and one for PROD.

Reason being, in some .js files, i have listed URLs, for example:

currentPage == '/products/chairs.html'

However, when moving these html files into my .Net structure, the actual URL is:

currentPage == 'goodsandservices/products/our-chairs.html'

My current rJS task is as follows:

gulp.task('rjs', function() {

  rjs({
        baseUrl: config.src.js,
        out: 'main.js',
        name: 'main',
        mainConfigFile: config.src.js + '/main.js',
        exclude: [ 'angular']
    })
    .pipe(prod ? uglify({ mangle: false, compress: { drop_console: true } }) : gutil.noop())
    .pipe(gulp.dest(config.dest.js))
});

I have tried using gulp-replace, and updated my rjs task as:

gulp.task('rjs', function() {

  rjs({
        baseUrl: config.src.js,
        out: 'main.js',
        name: 'main',
        mainConfigFile: config.src.js + '/main.js',
        exclude: [ 'angular']
    })
    .pipe(prod ? uglify({ mangle: false, compress: { drop_console: true } }) : gutil.noop())
            .pipe(replace(/NETStructure/g, 'goodsandservices'))
    .pipe(gulp.dest(config.dest.js))
});

And in my .js files, i have updated the URLs to:

currentPage == /NETStructure/g'/products/chairs.html'

However this didnt appear to work.


回答1:


I suggest you to keep those strings in an object and have two different files for the same object, like:

constants.dev.js

var constants = {
    chairs: '/products/chairs.html'
};

constants.prod.js

var constants = {
    chairs: 'goodsandservices/products/our-chairs.html'
};

Then you only need to include the correct file (or concatenate wrapped in a closure) and:

currentPage == constants.chairs


来源:https://stackoverflow.com/questions/24409306/gulp-task-to-create-a-dev-and-prod-version-of-main-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!