Skip to content

Commit

Permalink
Change compiler from Google Closure to esbuild
Browse files Browse the repository at this point in the history
Google Closure requires Java and does no longer maintain the JS API.
This commit changes to `esbuild` which is more modern, faster and has
same capabilities.
  • Loading branch information
undergroundwires committed Dec 28, 2024
1 parent 2023abf commit 535b35f
Show file tree
Hide file tree
Showing 3 changed files with 723 additions and 363 deletions.
47 changes: 17 additions & 30 deletions compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs');
const babel = require('@babel/core');
const path = require('path');
const ClosureCompiler = require('google-closure-compiler').compiler;
const esbuild = require('esbuild');

Check failure on line 6 in compile.js

View workflow job for this annotation

GitHub Actions / lint (npm run lint-js)

'esbuild' should be listed in the project's dependencies, not devDependencies
const CleanCSS = require('clean-css');

const outputFolder = 'dist';
Expand All @@ -17,16 +17,8 @@ const cssFolder = './src/themes/';
plugins: ['remove-import-export'],
})).code;
await Promise.all([
compileWithClosureAsync(noModulesCode, path.join(outputFolder, path.basename(inputFile)), {
compilation_level: 'SIMPLE',
formatting: 'PRETTY_PRINT',
env: 'BROWSER',
debug: true,
}),
compileWithClosureAsync(noModulesCode, path.join(outputFolder, `${removeExtension(path.basename(inputFile))}.min.js`), {
compilation_level: 'SIMPLE',
env: 'BROWSER',
}),
compileAsync(noModulesCode, path.join(outputFolder, path.basename(inputFile))),
compileAsync(noModulesCode, path.join(outputFolder, `${removeExtension(path.basename(inputFile))}.min.js`), true),
minifyCssAsync(cssFolder, path.join(outputFolder, 'themes')),
]);
})().catch((err) => {
Expand Down Expand Up @@ -61,26 +53,21 @@ function removeExtension(str) {
return str.slice(0, -path.extname(str).length);
}

async function compileWithClosureAsync(code, output, options) {
return new Promise((resolve, reject) => {
new ClosureCompiler(options).run([{
path: './',
src: code,
sourceMap: null,
}], async (exitCode, stdOut, stdErr) => {
if (stdErr) {
console.error('[ERROR] Google Closure', stdErr);
}
if (exitCode !== 0) {
reject(new Error(`Unexpected exit code : ${exitCode}`));
}
await Promise.all(stdOut.map(async (fileResult) => {
console.log(`JS compiled to "${output}", with options: ${JSON.stringify(options)}`);
await fs.promises.writeFile(output, fileResult.src);
}));
resolve();
async function compileAsync(codeString, targetPath, minifyCode = false) {
try {
await esbuild.build({
stdin: {
contents: codeString,
loader: 'js',
},
outfile: targetPath,
minify: minifyCode,
});
});
console.log(`JavaScript compiled to "${targetPath}" (${minifyCode ? 'minified' : 'not minified'})`);
} catch (error) {
console.error('[ERROR] esbuild', error);
process.exit(1);
}
}

async function deleteFolderRecursiveAsync(folderPath) {
Expand Down
Loading

0 comments on commit 535b35f

Please sign in to comment.