簡體   English   中英

將字符串添加到gulp流的開頭

[英]Add string to the beginning of a gulp stream

我已經設置了一個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是它正在修改的真實對象。

TL; DR

  1. 我如何將一小段代碼添加到gulp流文件的開頭
  2. 如果這不可能,那么我將如何使用另一種工具實現這種效果?

只需制作一個包含摘要的文件,然后執行以下操作:

src / first.js

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

src / Game.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'));
});

這將輸出build / ethereal.js

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

或僅使用http://browserify.org/並在實現它的每個模塊中都需要Ethereal模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM