Search code examples
javascriptpluginsgulpstrict

use strict and gulp


I want to add 'use strict'; to the top of my JS files. I have a lot files from different folders, that are merged into one file by concat. But some of them are jquery files, so adding 'use strict'; isn't needed. My task looks like this:

gulp.task(command, function () {
    gulp.src(path) // array of diff files
    .pipe(sourcemaps.init())
    .pipe(uglify())
    .pipe(concat(outfile))
    .pipe(sourcemaps.write('./maps', {
        sourceMappingURL: function(file) {
            return mappingURL + file.relative + '.map';
        }}))
    .pipe(gulp.dest(BUILD));
});

I didn't use any tools like browserify or webpack. Does anyone have any ideas?


Solution

  • Using the addsrc and the wrapper plugins.

    gulp.task(command, function () {
        gulp.src('some/src/...')
          .pipe(sourcemaps.init())
          // Minimize your app files.
          .pipe(uglify())
          // Add Jquery to the beginning.
          .pipe(addsrc.prepend('jquery/path...'))
          // Concat all files.
          .pipe(concat('some-file'))
          // Add the "use strict" header.
          .pipe(wrapper({ header: '"use strict";\n' }))
          .pipe(sourcemaps.write('some-file', {}))
          .pipe(gulp.dest('some/path/...'));
    });
    

    **Not tested.