How to use Google's Closure to compile JavaScript

后端 未结 6 1727
说谎
说谎 2021-02-05 14:56

Google just released Closure, which is a compiler to minify JavaScript.

On the product site, it says \"The Closure Compiler has also been integrated with Page Speed\".

6条回答
  •  遇见更好的自我
    2021-02-05 15:11

    The Closure compiler is now available as a JavaScript application. No need for the Java dependency anymore

    There are a few ways to integrate with it. I have done it as part of Rollup

    ex:

    import rollup from 'rollup';
    import closure from 'rollup-plugin-closure-compiler-js';
    
    export default {
      entry: 'index.js',
      dest: 'dist/build.js',
      format: 'iife',
      plugins: [
        closure({
          languageIn: 'ECMASCRIPT6',
          languageOut: 'ECMASCRIPT5',
          compilationLevel: 'ADVANCED',
          warningLevel: 'VERBOSE',
          externs: [{src:`
                          var jQuery;
                          jQuery.fadeIn = function() {};  
    
                          var ko;  
                          ko.applyBindings = function(vm) {};
                          ko.computed = function(a,b) {};
                          ko.observable = function(a) {};
                   `}],
        })
      ]
    }
    

    More info here:

    http://www.syntaxsuccess.com/viewarticle/using-the-closure-compiler---advanced_optimizations

提交回复
热议问题