使用gulp减少谷歌关闭运行时间

时间:2017-10-26 15:12:27

标签: javascript gulp google-closure-compiler nailgun

我正在尝试在开发过程中找到plovr的替代方案,因为plovr有点远,每次升级谷歌关闭都会变得麻烦。

所以我考虑过使用gulp。我使用gulp connect设置了一个gulp webserver,并且在后备时,它触发了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来运行谷歌闭包编译器,但我不知道如何将它与我当前的设置集成,任何指针将不胜感激:)

1 个答案:

答案 0 :(得分:0)

我找到了答案,并在google-closure-compiler npm repo的自述文件中添加了解决方案 https://github.com/google/closure-compiler-npm/pull/77/files