gulp 实战

const gulp = require('gulp');
const browserify = require("browserify");
const source = require("vinyl-source-stream");
const tsify = require("tsify");
const uglify = require('gulp-uglify');
// const sourcemaps = require('gulp-sourcemaps');
const buffer = require('vinyl-buffer');
const rename = require('gulp-rename');
const clean = require('gulp-clean');
const addName = process.argv.slice(2)[1];
const entryPath = `logic/${addName}/test.ts`;

const paths = {
  pages:['src/*.html']
};

const browserifyFun = browserify({
  basedir: '.',
  debug: true,
  entries: [entryPath],
  cache: {},
  packageCache: {}
}).plugin(tsify);
// 打包前清除旧有打包内容文件
gulp.task('clean:dist', function() {
  return gulp.src(`./dist/${addName}`)
    .pipe(clean());
});
// 复制模板文件到对应的站点包文件下
gulp.task("copy:html", function () {
  return gulp.src(paths.pages)
    .pipe(gulp.dest(`dist/${addName}`));
});

function bundle() {
  return browserifyFun
    .transform('babelify', {
      presets: ['@babel/preset-env'],
      extensions: ['.ts']
    })
    .bundle()
    .pipe(source('bundle.js'))
    .pipe(buffer())
    // .pipe(rename({suffix: '_1_0_0'})) // 打包带版本文件
    // .pipe(sourcemaps.init({loadMaps: true})) // 代码调试用
    .pipe(uglify()) // 压缩代码
    // .pipe(sourcemaps.write('./')) // 代码调试用
    .pipe(gulp.dest(`dist/${addName}`));
}

gulp.task("default", gulp.series('clean:dist', gulp.parallel('copy:html', bundle)));

你可能感兴趣的:(gulp 实战)