请任何人在google闭包编译器的基本过程中添加一个代码片段,我正在尝试通过js代码来实现这一点,但没有成功。我正在使用官方npm页面中的示例片段。当我运行它时,似乎发生了一些事情,但是没有创建输出文件。
我的代码:
const ClosureCompiler = require('google-closure-compiler').jsCompiler;
console.log(ClosureCompiler.CONTRIB_PATH); // absolute path to the contrib folder which contains externs
const closureCompiler = new ClosureCompiler({
compilation_level: 'ADVANCED'
});
const compilerProcess = closureCompiler.run([{
path: './',
src: 'a.js',
sourceMap: null // optional input source map
}], (exitCode, stdOut, stdErr) => {
console.log(stdOut)
//compilation complete
});
发布于 2019-06-06 15:31:46
根据你的经验,我只改变了几件事:
1) src属性不是路径,而是文件:在本例中使用fs.readFileSync读取文件。
2)输出在回调中返回:您需要将它写入磁盘。
文件:
index.js
const ClosureCompiler = require('google-closure-compiler').jsCompiler;
const {writeFile, readFileSync} = require('fs');
const closureCompiler = new ClosureCompiler({
compilation_level: 'ADVANCED'
});
let src = readFileSync('a.js', 'UTF-8');
const compilerProcess = closureCompiler.run([{
path: './',
src: src,
sourceMap: null
}], (exitCode, stdOut, stdErr) => {
stdOut.map((fileResults) => {
writeFile(fileResults.path, fileResults.src, () => {});
});
});a.js
console.log('hello world!')compiled.js
console.log("hello world!");发布于 2019-06-07 19:08:18
好的,所以如果不使用“fs”库,就无法创建文件。
根据“闭包编译器-js.js”,当“运行”完成时,回调只会记录结果。https://github.com/google/closure-compiler-npm/blob/master/packages/google-closure-compiler/lib/node/closure-compiler-js.js
这很有趣,因为‘闭包编译器-npm’确实使用fs来读取文件内容,但是它有任何“写文件”机制。
即使在官方的“cli.js”上,也使用“fs”库:https://github.com/google/closure-compiler-npm/blob/master/packages/google-closure-compiler/cli.js
const ClosureCompiler = require('google-closure-compiler').jsCompiler;
const { writeFile } = require('fs');
const closureCompiler = new ClosureCompiler({
js:['a.js','a1.js'],
js_output_file: 'out.js'
});
const compilerProcess = closureCompiler.run([{
path: './',
}], (exitCode, stdOut, stdErr) => {
writeFile(stdOut[0].path, stdOut[0].src,()=>{});
});
https://stackoverflow.com/questions/56477837
复制相似问题