Skip to content

Commit

Permalink
🐛 Change ESbuild 3p vendor target to ES5 (#33250)
Browse files Browse the repository at this point in the history
* Change ESbuild 3p vendor target to ES5

* Add teaser/fix rebuild

* Comments

* fix rebuild and miscg
  • Loading branch information
powerivq committed Mar 18, 2021
1 parent 4c06c96 commit 9c00326
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 28 deletions.
5 changes: 2 additions & 3 deletions build-system/babel-config/minified-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ function getMinifiedConfig() {
{
bugfixes: true,
modules: false,
targets: {esmodules: true},
targets: argv.esm ? {esmodules: true} : {ie: 11, chrome: 41},
},
];
const presets = argv.esm ? [presetEnv] : [];
return {
compact: false,
plugins,
presets,
presets: [presetEnv],
retainLines: true,
};
}
Expand Down
99 changes: 74 additions & 25 deletions build-system/tasks/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const magicstring = require('magic-string');
const open = require('open');
const path = require('path');
const Remapping = require('@ampproject/remapping');
const terser = require('terser');
const wrappers = require('../compile/compile-wrappers');
const {
VERSION: internalRuntimeVersion,
Expand Down Expand Up @@ -554,42 +555,90 @@ async function compileJsWithEsbuild(srcDir, srcFilename, destDir, options) {
options.minify ? 'minified' : 'unminified',
/* enableCache */ true
);
const buildResult = await esbuild
.build({
entryPoints: [entryPoint],
bundle: true,
sourcemap: true,
outfile: destFile,
plugins: [plugin],
minify: options.minify,
incremental: !!options.watch,
logLevel: 'silent',
})
.then((result) => {
finishBundle(srcFilename, destDir, destFilename, options, startTime);
return result;
})
.catch((err) => handleBundleError(err, !!options.watch, destFilename));

let result = null;

/**
* @param {number} time
*/
async function build(time) {
if (!result) {
result = await esbuild.build({
entryPoints: [entryPoint],
bundle: true,
sourcemap: true,
outfile: destFile,
plugins: [plugin],
minify: options.minify,
target: argv.esm ? 'es6' : 'es5',
incremental: !!options.watch,
logLevel: 'silent',
});
} else {
result = await result.rebuild();
}
await minifyWithTerser(destDir, destFilename, options);
await finishBundle(srcFilename, destDir, destFilename, options, time);
}

await build(startTime).catch((err) =>
handleBundleError(err, !!options.watch, destFilename)
);

if (options.watch) {
watchedTargets.set(entryPoint, {
rebuild: async () => {
const time = Date.now();
const buildPromise = buildResult
.rebuild()
.then(() =>
finishBundle(srcFilename, destDir, destFilename, options, time)
)
.catch((err) =>
handleBundleError(err, /* continueOnError */ true, destFilename)
);
options?.onWatchBuild(buildPromise);
const buildPromise = build(time).catch((err) =>
handleBundleError(err, !!options.watch, destFilename)
);
if (options.onWatchBuild) {
options.onWatchBuild(buildPromise);
}
await buildPromise;
},
});
}
}

/**
* Minify the code with Terser. Only used by the ESBuild.
*
* @param {string} destDir
* @param {string} destFilename
* @param {?Object} options
* @return {!Promise}
*/
async function minifyWithTerser(destDir, destFilename, options) {
if (!options.minify) {
return;
}

const filename = destDir + destFilename;
const terserOptions = {
mangle: true,
compress: true,
output: {
beautify: !!argv.pretty_print,
comments: /\/*/,
// eslint-disable-next-line google-camelcase/google-camelcase
keep_quoted_props: true,
},
sourceMap: true,
};
const minified = await terser.minify(
fs.readFileSync(filename, 'utf8'),
terserOptions
);
const remapped = remapping(
[minified.map, fs.readFileSync(`${filename}.map`, 'utf8')],
() => null,
!argv.full_sourcemaps
);
fs.writeFileSync(filename, minified.code);
fs.writeFileSync(`${filename}.map`, remapped.toString());
}

/**
* Transpiles from TypeScript into intermediary files before compilation and
* deletes them afterwards.
Expand Down

0 comments on commit 9c00326

Please sign in to comment.