gpt4 book ai didi

javascript - 如何使用 Gulp 压缩 ES6

转载 作者:数据小太阳 更新时间:2023-10-29 04:50:38 24 4
gpt4 key购买 nike

我对手动编写 gulpfile.js 比较陌生。我有一个基于 Backbone 和 Marionette 的项目,到目前为止,gulp 文件如下所示:

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var del = require('del');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var stylish = require('jshint-stylish');
var buffer = require('vinyl-buffer');
var _ = require('lodash');

var browserSync = require('browser-sync').create();
var reload = browserSync.reload;

gulp.task('clean', function(cb) {
return del([
'app/tmp'
], cb);
});

gulp.task('html', function() {
return gulp.src('./src/index.html')
.pipe($.plumber())
.pipe(gulp.dest('./dist'));
});

gulp.task('images', function() {
gulp.src(['./src/assets/images/*.jpg', './src/assets/images/*.svg', './src/assets/images/*.gif',
'./src/assets/images/*.png'
])
.pipe(gulp.dest('./dist/images'));
});

gulp.task('vendor-css', function() {
gulp.src(['./src/assets/styles/vendor/*.css'
])
.pipe(gulp.dest('./dist'));
});

gulp.task('fonts', function() {
gulp.src(['./src/assets/fonts/*.eot', './src/assets/fonts/*.woff', './src/assets/fonts/*.ttf',
'./src/assets/fonts/*.svg'
])
.pipe(gulp.dest('./dist/fonts'));
});

gulp.task('styles', function() {
return gulp.src('./src/main.styl')
.pipe($.stylus())
.pipe($.autoprefixer())
.pipe($.rename('bundle.css'))
.pipe(gulp.dest('./dist'))
.pipe(reload({ stream: true }));
});

var bundler = _.memoize(function(watch) {
var options = {debug: true};

if (watch) {
_.extend(options, watchify.args);
}

var b = browserify('./src/main.js', options);

if (watch) {
b = watchify(b);
}

return b;
});

var handleErrors = function() {
var args = Array.prototype.slice.call(arguments);
delete args[0].stream;
$.util.log.apply(null, args);
this.emit('end');
};

function bundle(cb, watch) {
return bundler(watch).bundle()
.on('error', handleErrors)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest('./dist'))
.on('end', cb)
.pipe(reload({ stream: true }));
}

gulp.task('scripts', function(cb) {
bundle(cb, true);
});

gulp.task('jshint', function() {
return gulp.src(['./src/**/*.js', './test/**/*.js'])
.pipe($.plumber())
.pipe($.jshint())
.pipe($.jshint.reporter(stylish));
});

var reporter = 'spec';

gulp.task('mocha', ['jshint'], function() {
return gulp.src([
'./test/setup/node.js',
'./test/setup/helpers.js',
'./test/unit/**/*.js'
], { read: false })
.pipe($.plumber())
.pipe($.mocha({ reporter: reporter }));
});

gulp.task('build', [
'clean',
'html',
'images',
'vendor-css',
'fonts',
'styles',
'scripts',
'test'
]);

gulp.task('test', [
'jshint'
]);

gulp.task('watch', ['build'], function() {
browserSync.init({
server: {
baseDir: 'dist'
}
});

reporter = 'dot';
bundler(true).on('update', function() {
gulp.start('scripts');
gulp.start('test');
});
gulp.watch('./test/**/*.js', ['test']);
gulp.watch(['./src/main.styl', './src/pages/**/*.styl', './src/assets/styles/*.styl'], ['styles']);
gulp.watch(['./src/*.html'], ['html']);
});

gulp.task('default', ['watch']);

现在我需要启用 Javascript 的缩小,为此我引用了以下 link .我使用带有 uglify-js-harmony 的压缩器作为 ES6 支持的压缩器,因为我的大部分代码都使用 ES6 语法。修改后的 gulp 文件如下所示:

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var del = require('del');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
var stylish = require('jshint-stylish');
var buffer = require('vinyl-buffer');
var uglifyjs = require('uglify-js-harmony');
var minifier = require('gulp-uglify/minifier');
var pump = require('pump');
var _ = require('lodash');

var browserSync = require('browser-sync').create();
var reload = browserSync.reload;

gulp.task('clean', function(cb) {
return del([
'app/tmp'
], cb);
});

gulp.task('html', function() {
return gulp.src('./src/index.html')
.pipe($.plumber())
.pipe(gulp.dest('./dist'));
});

gulp.task('images', function() {
gulp.src(['./src/assets/images/*.jpg', './src/assets/images/*.svg', './src/assets/images/*.gif',
'./src/assets/images/*.png'
])
.pipe(gulp.dest('./dist/images'));
});

gulp.task('vendor-css', function() {
gulp.src(['./src/assets/styles/vendor/*.css'
])
.pipe(gulp.dest('./dist'));
});

gulp.task('fonts', function() {
gulp.src(['./src/assets/fonts/*.eot', './src/assets/fonts/*.woff', './src/assets/fonts/*.ttf',
'./src/assets/fonts/*.svg'
])
.pipe(gulp.dest('./dist/fonts'));
});

gulp.task('styles', function() {
return gulp.src('./src/main.styl')
.pipe($.stylus())
.pipe($.autoprefixer())
.pipe($.rename('bundle.css'))
.pipe(gulp.dest('./dist'))
.pipe(reload({ stream: true }));
});

var bundler = _.memoize(function(watch) {
var options = {debug: true};

if (watch) {
_.extend(options, watchify.args);
}

var b = browserify('./src/main.js', options);

if (watch) {
b = watchify(b);
}

return b;
});

var handleErrors = function() {
var args = Array.prototype.slice.call(arguments);
delete args[0].stream;
$.util.log.apply(null, args);
this.emit('end');
};

function bundle(cb, watch) {
return bundler(watch).bundle()
.on('error', handleErrors)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest('./dist'))
.on('end', cb)
.pipe(reload({ stream: true }));
}

gulp.task('scripts', function(cb) {
bundle(cb, true);
});

gulp.task('jshint', function() {
return gulp.src(['./src/**/*.js', './test/**/*.js'])
.pipe($.plumber())
.pipe($.jshint())
.pipe($.jshint.reporter(stylish));
});

gulp.task('compress', function (cb) {
var options = {
preserveComments: 'license'
};

pump([
gulp.src('./dist/bundle.js'),
minifier(options, uglifyjs),
gulp.dest('./dist')
],
cb
);
});

var reporter = 'spec';

gulp.task('mocha', ['jshint'], function() {
return gulp.src([
'./test/setup/node.js',
'./test/setup/helpers.js',
'./test/unit/**/*.js'
], { read: false })
.pipe($.plumber())
.pipe($.mocha({ reporter: reporter }));
});

gulp.task('build', [
'clean',
'html',
'images',
'vendor-css',
'fonts',
'styles',
'scripts',
'test',
'compress'
]);

gulp.task('test', [
'jshint'
]);

gulp.task('watch', ['build'], function() {
browserSync.init({
server: {
baseDir: 'dist'
}
});

reporter = 'dot';
bundler(true).on('update', function() {
gulp.start('scripts');
gulp.start('test');
});
gulp.watch('./test/**/*.js', ['test']);
gulp.watch(['./src/main.styl', './src/pages/**/*.styl', './src/assets/styles/*.styl'], ['styles']);
gulp.watch(['./src/*.html'], ['html']);
});

gulp.task('default', ['watch']);

现在,当我运行 gulp 时,压缩任务开始,没有给出任何错误但从未完成并且构建(dist)与之前相同(没有缩小发生!) 。我是否需要使用另一个 .pipe 以某种方式将此压缩任务集成到 bundle 函数中,或者我在这里以错误的方式做其他事情?此外,在创建 bundle.js 之后进行缩小是否正确,这是我在这里尝试做的,还是需要在源文件仍未连接的阶段进行?

最佳答案

  1. 克隆https://github.com/terinjokes/gulp-uglify/
  2. 找到 https://github.com/terinjokes/gulp-uglify/blob/80da765a266cb7ff9d034a73bde0abe18d72d6de/package.json#L13在首选结帐中(首选最新)
  3. 将 uglify-js 的版本设置为 mishoo/UglifyJS2#harmony(https://github.com/mishoo/UglifyJS2#harmony 的快捷方式)

请注意,在正式发布 uglify 支持 harmony 之前,这是一个临时设置

关于javascript - 如何使用 Gulp 压缩 ES6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38444054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com