Javascript 大口喝

Javascript 大口喝,javascript,plugins,gulp,strict,Javascript,Plugins,Gulp,Strict,我想添加“使用严格”到我的JS文件的顶部。我有很多来自不同文件夹的文件,它们被concat合并成一个文件。但其中一些是jquery文件,因此添加“使用strict”是不需要的。我的任务如下所示: gulp.task(command, function () { gulp.src(path) // array of diff files .pipe(sourcemaps.init()) .pipe(uglify()) .pipe(concat(outfile))

我想添加
“使用严格”到我的JS文件的顶部。我有很多来自不同文件夹的文件,它们被concat合并成一个文件。但其中一些是jquery文件,因此添加
“使用strict”是不需要的。我的任务如下所示:

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));
});
我没有使用任何像browserify或webpack这样的工具。有人有什么想法吗?

使用
addsrc
wrapper
插件。
  • addsrc
    来自
  • 包装器
    来自

**未测试。

您希望连接并最小化应用程序文件,然后将JQuery与结果连接在一起而不最小化它。是吗?是的,类似于:concatFiles.js=>wrap('strict mode')=>concat with jQueryHm看起来像是一些没有最小化的jQuery文件,所以应该最小化这个问题,因为jQuery已经有了“use strict”detective,所以应该从包装中排除它。我也知道为什么,但在您的解决方案中,jQuery文件没有最小化,所以jQuery必须被最小化?那我看不出你有什么问题?如果jQuery有“use strict”,只需在jQuery末尾连接所有文件并最小化结果。jQuery有模块化结构,所以它包装在iife中,所以“use strict”没有全局效果。我也试着对其他代码做同样的处理。在我看来,将代码包装在一个生命周期中并添加“严格使用”并不是一件容易的事情。你应该在你所有的js文件中这样做。因此,您可以单独或捆绑使用您的文件。
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/...'));
});