将字符串添加到吞咽流的开头

Add string to the beginning of a gulp stream

本文关键字:开头 字符串 添加      更新时间:2023-09-26

我已经设置了一个gulpfile.js它将我的js文件目录编译到一个(缩小的)源。但是我需要一小段代码来执行它(初始化他们正在修改的对象文字),但我似乎无法弄清楚如何实现这一点。(见下面的gulpfile)

var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
gulp.task('build', function() {
    return gulp.src('src/*.js')
        .pipe(concat('ethereal.js'))
        .pipe(gulp.dest('build'))
        .pipe(rename('ethereal.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('build'));
});
gulp.task('lint', function() {
    return gulp.src('src/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});
gulp.task('watch', function() {
    gulp.watch('src/*.js', ["lint", "build"]);
})

src 中的每个文件都会修改一个对象文字,我需要将其添加到输出脚本的最开头

例如,src/Game.js如下所示:

Ethereal.Game = function() {
    // init game code
}

注意它如何假设 Ethereal 是它正在修改的真实对象,它确实如此。

博士

  1. 如何将一段代码添加到 gulp 流文件的开头
  2. 如果这是不可能的,我怎么能用另一个工具达到这样的效果?

只需先创建一个包含代码片段的文件,然后执行以下操作:

SRC/第一.js

var Ethereal = function() {
    // define Ethereal class constructor and stuff
}

src/游戏.js

Ethereal.Game = function() {
    // init game code
}

然后在gulpfile中:

gulp.task('build', function() {
    return gulp.src(['src/first.js', 'src/*.js'])
        .pipe(concat('ethereal.js'))
        .pipe(gulp.dest('build'))
        .pipe(rename('ethereal.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('build'));
});

这将输出构建/空灵.js

var Ethereal = function() {
    // define Ethereal class constructor and stuff
} 
Ethereal.Game = function() {
    // init game code
}

或者只是使用 http://browserify.org/并在实现它的每个模块中要求 Ethereal 模块。