Files
lexical/scripts/plugins/closure-plugin.js
Justin Haaheim 1c856e80ec Add sort keys and sort imports lint rules, and enable fix+format on save (#1209)
* Add sort keys rule and update configs so it works

Bump version of flowtype eslint plugin to fix bugs with autofixing

* Add sort imports fixable lint rule

* Run lint fix and prettier on whole repo 

* Fix unit tests broken by key sorting
2022-04-09 00:43:22 -07:00

37 lines
987 B
JavaScript

'use strict';
const ClosureCompiler = require('google-closure-compiler').compiler;
const {promisify} = require('util');
const fs = require('fs');
const tmp = require('tmp');
const writeFileAsync = promisify(fs.writeFile);
function compile(flags) {
return new Promise((resolve, reject) => {
const closureCompiler = new ClosureCompiler(flags);
closureCompiler.run(function (exitCode, stdOut, stdErr) {
if (!stdErr) {
resolve(stdOut);
} else {
reject(new Error(stdErr));
}
});
});
}
module.exports = function closure(flags = {}) {
return {
name: 'scripts/plugins/closure-plugin',
async renderChunk(code) {
const inputFile = tmp.fileSync();
const tempPath = inputFile.name;
flags = Object.assign({}, flags, {js: tempPath});
await writeFileAsync(tempPath, code, 'utf8');
const compiledCode = await compile(flags);
inputFile.removeCallback();
return {code: compiledCode};
},
};
};