Gulp task to create a DEV and PROD version of main

2019-05-30 17:28发布

问题:

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