我一直在尝试使用 Gulp + Browserify + Tsify 将 4 个文件及其库转换为 4 个 js 文件,目前需要 4 或 5 秒。
在我当前的构建脚本中,watchify 会触发对所有 4 个构建管道的任何 ts 文件的更改的更新,每个构建管道都会对项目中的每个 ts 文件进行转换,即使它没有在该管道中使用。
目前我正在寻找一种更好更快的方法来完成这项工作。
import * as browserify from "browserify"
import * as gulp from "gulp"
import * as rename from "gulp-rename"
import * as sass from "gulp-sass"
import * as uglify from "gulp-uglify"
import * as gutil from "gulp-util"
import * as fs from "fs"
import * as tsify from "tsify"
import * as buffer from "vinyl-buffer"
import * as source from "vinyl-source-stream"
import * as watchify from "watchify"
import {assign} from "lodash"
const sassOptions: sass.Options = {
outputStyle: 'expanded'
}
function combinejs(update: boolean, minify: boolean) {
['backScript.ts', 'mainFrame.ts', 'commentFrame.ts','script-inject.ts'].forEach(f => {
let b = browserify(assign({},watchify.args,{basedir:'src/', entries: f})),
bundle = () => {
let pipe = b.bundle().on('error',gutil.log)
.pipe(source(f)).pipe(rename({extname:'.js'}))
if (minify) {
pipe = pipe.pipe(buffer()).pipe(uglify())
}
pipe.pipe(gulp.dest('build/files/src'))
}
b.plugin(tsify)
if (update){
b.plugin(watchify,{})
b.on('update',()=>{
console.log({update:f})
bundle()
})
}
b.on('log', console.log)
console.log(f)
bundle()
})
}
gulp.add('js', () => combinejs(false, true))
gulp.add('css', () => {
gulp.src('src/*.scss')
.pipe(sass(sassOptions))
.pipe(gulp.dest('build/files/src'))
})
gulp.add('copyFiles', () => {
gulp.src(['manifest.json', 'popup.html', 'images/*'], { base: '.' })
.pipe(gulp.dest('build/files'))
})
gulp.add("default", ['copyFiles','js', 'css'])
gulp.add('build',['copyFiles','css'],()=>{
combinejs(false,false)
})
gulp.add("watch", ['copyFiles', 'css'], () => {
combinejs(true, false)
gulp.watch('src/*.scss', ['css'])
})