How can I use gulp to replace a string in a file?

后端 未结 4 1501
执笔经年
执笔经年 2021-01-01 10:40

I am using gulp to uglify and make ready my javascript files for production. What I have is this code:

var concat = require(\'gulp-concat\');
var del = requi         


        
4条回答
  •  感情败类
    2021-01-01 10:50

    I think that the most correct solution is to use the gulp-preprocess module. It will perform the actions you need, depending on the variable PRODUCTION, defined or not defined during the build.

    Source code:

    /* @ifndef PRODUCTION */
    dataServer: "http://localhost:3048",
    /* @endif */
    /* @ifdef PRODUCTION **
    dataServer: "http://example.com",
    /* @endif */
    

    Gulpfile:

    let preprocess = require('gulp-preprocess');
    const preprocOpts = {
      PRODUCTION: true
    };
    
    gulp.task('scripts', ['clean-js'], function () {
      return gulp.src(js.src)
        .pipe(preprocess({ context: preprocOpts }))
        .pipe(uglify())
        .pipe(concat('js.min.js'))
        .pipe(gulp.dest('content/bundles/'));
    }
    

    This is the best solution because it allows you to control the changes that are made during the build phase.

提交回复
热议问题