How to use google-closure-compiler-js for a node.js app without gulp/grunt/webpack?

柔情痞子 提交于 2019-12-24 01:29:34

问题


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;
}


来源:https://stackoverflow.com/questions/39925364/how-to-use-google-closure-compiler-js-for-a-node-js-app-without-gulp-grunt-webpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!