Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Oct 28, 2023
1 parent faa049b commit 50ea22e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exact-realty/esbuild-plugin-closure-compiler",
"version": "1.0.0",
"version": "1.0.1",
"description": "esbuild plugin for compiling with Google Closure Compiler as the last step",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/esmDynamicImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const dymamicImportExpressionRegex =
const preprocess = (text: string): string =>
text.replace(
dymamicImportExpressionRegex,
'globalThis.__reserved__["%dymamic_import%"]',
'globalThis["__reserved__"]["%dymamic_import%"]',
);

const postprocess = (text: string): string => {
Expand Down
4 changes: 2 additions & 2 deletions src/esmExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const preprocess = (text: string): string =>
text
.replace(
/export\s+default\b/g,
'/** @nocollapse */globalThis.__reserved__["%export"]["default"]=',
'/** @nocollapse */globalThis["__reserved__"]["%export"]["default"]=',
)
.replace(/export\s*{([^}]+)}/g, (_, p1: string) => {
return p1
.split(',')
.map(
(e) =>
'/** @nocollapse */globalThis.__reserved__["%export"]' +
'/** @nocollapse */globalThis["__reserved__"]["%export"]' +
(/\sas\s/.test(e)
? e.replace(/(.+)\sas\s(.+)/, (_, p1, p2) => {
return `[${JSON.stringify(
Expand Down
8 changes: 4 additions & 4 deletions src/esmImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const preprocess = (text: string): string =>
importExpressionRegex,
(_, defaultExport, starName, namedImports, from, moduleName) => {
return (
'/** @nocollapse */ globalThis.__reserved__["%import_start"]();' +
'/** @nocollapse */ globalThis["__reserved__"]["%import_start"]();' +
`${
from
? `const {${[
Expand All @@ -35,8 +35,8 @@ const preprocess = (text: string): string =>
.filter(Boolean)
.join(',')}}=`
: ''
}globalThis.__reserved__["%import"](${moduleName});` +
'/** @nocollapse */ globalThis.__reserved__["%import_end"]()'
}globalThis["__reserved__"]["%import"](${moduleName});` +
'/** @nocollapse */ globalThis["__reserved__"]["%import_end"]()'
);
},
)
Expand All @@ -45,7 +45,7 @@ const preprocess = (text: string): string =>
.split(',')
.map(
(e) =>
'/** @nocollapse */globalThis.__reserved__["%export"]' +
'/** @nocollapse */globalThis["__reserved__"]["%export"]' +
(/\sas\s/.test(e)
? e.replace(/(.+)\sas\s(.+)/, (_, p1, p2) => {
return `[${JSON.stringify(
Expand Down
78 changes: 47 additions & 31 deletions src/getCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@

import type esbuild from 'esbuild';
import googleClosureCompiler from 'google-closure-compiler';
import * as esmDynamicImports from './esmDynamicImports.js';
import * as esmExports from './esmExports.js';
import * as esmImports from './esmImports.js';
import * as esmDynamicImports from './esmDynamicImports.js';
import * as umdExports from './umdExports.js';

/* The following functions ensure that external code can include __reserved__
in it, without causing interference with the preprocessing and postprocessing
functions
*/
const preprocessReserved = (x: string) => x.replace(/(__reserved__)/g, '_$1_');
const postprocessReserved = (x: string) => x.replace(/_(__reserved__)_/g, '$1');

const esm = {
preprocess: (x: string): string => {
return [
preprocessReserved,
esmDynamicImports.preprocess,
esmExports.preprocess,
esmImports.preprocess,
Expand All @@ -33,22 +41,25 @@ const esm = {
esmDynamicImports.postprocess,
esmExports.postprocess,
esmImports.postprocess,
postprocessReserved,
].reduce((acc, cv) => cv(acc), x);
},
};

const umd = {
preprocess: (x: string): string => {
return [esmDynamicImports.preprocess, umdExports.preprocess].reduce(
(acc, cv) => cv(acc),
x,
);
return [
preprocessReserved,
esmDynamicImports.preprocess,
umdExports.preprocess,
].reduce((acc, cv) => cv(acc), x);
},
postprocess: (x: string): string => {
return [esmDynamicImports.postprocess, umdExports.postprocess].reduce(
(acc, cv) => cv(acc),
x,
);
return [
esmDynamicImports.postprocess,
umdExports.postprocess,
postprocessReserved,
].reduce((acc, cv) => cv(acc), x);
},
};

Expand All @@ -60,34 +71,39 @@ const getCompiler = (

const buildModule = esmOutput ? esm : umd;

const compiler = new googleClosureCompiler.compiler({
language_in: 'ECMASCRIPT_2020',
language_out: 'ECMASCRIPT_2015',
env: 'BROWSER',
...compilerOptions,
rewrite_polyfills: false,
process_closure_primitives: false,
apply_input_source_maps: false,
warning_level: 'QUIET',
isolate_polyfills: true,
assume_function_wrapper: esmOutput,
});
// A new instance is needed every time because of how the command lines
// are constructed
const compilerFactory = () =>
new googleClosureCompiler.compiler({
language_in: 'ECMASCRIPT_2020',
language_out: 'ECMASCRIPT_2015',
env: 'BROWSER',
...compilerOptions,
rewrite_polyfills: false,
process_closure_primitives: false,
apply_input_source_maps: false,
warning_level: 'QUIET',
isolate_polyfills: true,
assume_function_wrapper: esmOutput,
});

return (text: string) => {
text = buildModule.preprocess(text);

return new Promise<string>((resolve, reject) => {
const process = compiler.run((exitCode, stdout, stderr) => {
if (exitCode === 0) {
// TODO: Warnings
stdout = buildModule.postprocess(stdout);
const process = compilerFactory().run(
(exitCode, stdout, stderr) => {
if (exitCode === 0) {
// TODO: Warnings
stdout = buildModule.postprocess(stdout);

resolve(stdout);
} else {
// TODO: Better error handing
return reject(new Error(stderr));
}
});
resolve(stdout);
} else {
// TODO: Better error handing
return reject(new Error(stderr));
}
},
);

if (!process.stdin) {
process.kill();
Expand Down

0 comments on commit 50ea22e

Please sign in to comment.