How to use google-closure-compiler-js for a node.j

2019-07-10 21:47发布

问题:

The docs don't have any examples of using this on its own but they do say this:

Unless you're using the Gulp or Webpack plugins, you'll need to specify code via flags. Both jsCode and externs accept an array containing objects in the form {src, path, sourceMap}. Using path, you can construct a virtual filesystem for use with ES6 or CommonJS imports—although for CommonJS, be sure to set processCommonJsModules: true.

I've created a "compile.js" file based on the docs:

const compile = require('google-closure-compiler-js').compile;
const flags = {
  jsCode: [{path: './server/server.js'}],
  processCommonJsModules: true
};
const out = compile(flags);
console.info(out.compiledCode); 

In my "./server/server.js" file, I put a console.log but it doesn't output. Not sure where to go from here...

回答1:

Borrowing from icidasset/quotes.

It appears, to me, that path is not intended to be used as you are using it.

Quote:

Using path, you can construct a virtual filesystem for use with ES6 or CommonJS imports—although for CommonJS, be sure to set processCommonJsModules: true.

So instead you must expand your own sources, something webpack and gulp must be doing for you when you go that route.

files=['./server/server.js']
files.map(f => {
    const out = compile({
      jsCode: [{ src: f.content }],
      assumeFunctionWrapper: true,
      languageIn: 'ECMASCRIPT5'
    });
    return out;
}