我正在尝试在开发时找到 plovr 的替代方案,因为 plovr 有点落后,每次升级谷歌关闭都变得很麻烦。
所以我考虑使用 gulp。我使用 gulp connect 设置了一个 gulp 网络服务器,在回退时,它会触发 google 闭包编译,然后等待文件生成,然后将其提供给客户端。我面临的问题是编译时间比 plovr ~40s vs ~20s 要大得多。对于前端开发人员来说,在进行小的更改后等待页面加载需要很长时间。知道可以做些什么来优化闭包编译器的编译时间。这是我的 gulpfile.js,让您了解我的设置。
var gulp = require('gulp'),
closureCompiler = require('google-closure-compiler').gulp({
extraArguments: ['-Xms2048m']
}),
fs = require('fs-extra'),
grepit = require('grepit'),
connect = require('gulp-connect'),
sleep = require('system-sleep'),
outputPath = './target/js',
pathToJavascriptCode = './project/',
compilationLevel = 'ADVANCED',
entryPoint = undefined;
gulp.task('js-clean', function() {
fs.removeSync(outputPath);
});
gulp.task('js-compile', function() {
var outputFileName = entryPoint + '.' + compilationLevel.toLowerCase() + '.js.compiled';
return gulp.src([
pathToJavascriptCode + '/**/*.js',
'./node_modules/google-closure-library/closure/goog/**/*.js',
'!./node_modules/google-closure-library/closure/goog/**/*_test.js'],
{base: './'})
.pipe(closureCompiler({
closure_entry_point: entryPoint,
externs: [
'./public/javascripts/libs/jquery-1.7-externs.js'],
compilation_level: compilationLevel,
warning_level: 'VERBOSE',
js_output_file: './' + outputFileName,
manage_closure_dependencies: true,
language_in: 'ECMASCRIPT6',
language_out: 'ECMASCRIPT5'
}))
.pipe(gulp.dest(outputPath))
.pipe(connect.reload());
});
gulp.task('webserver', function () {
connect.server({
root: ['./target/js/'],
port: 8080,
livereload: true,
middleware: function() {
return [function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
}];
},
fallback: function(req, res) {
var requestedFileName = req.url.replace('/', '');
var requestedFileNameParts = requestedFileName.split('.');
compilationLevel = requestedFileNameParts[3] === 'simple' ? 'SIMPLE' : 'ADVANCED';
entryPoint = requestedFileNameParts[2] !== 'all' ? requestedFileNameParts.splice(0, 3).join('.') : undefined;
gulp.start('js-compile');
var path = './target/js' + req.url;
while(fs.existsSync(path) !== true) {
console.log(path + ' does not exist yet, sleeping for 1 second');
sleep(1000)
}
return path;
}
})
});
gulp.task('watch', function() {
gulp.watch('./project/pauljs/**/*.js', ['js-clean']);
});
gulp.task('default', ['js-clean', 'webserver', 'watch']);
经过一番研究,我猜问题是 jvm 的启动时间很慢,我发现这个https://www.npmjs.com/package/closure-gun使用 nailgun 来运行 google 闭包编译器,但我不知道怎么能我将它与我当前的设置集成,任何指针将不胜感激:)