Skip to content

Commit

Permalink
Merge pull request mozilla#17686 from Snuffleupagus/Webpack-TerserPlugin
Browse files Browse the repository at this point in the history
Run minification directly during Webpack building
  • Loading branch information
timvandermeij authored Feb 17, 2024
2 parents 8487c67 + 091e861 commit 4ac8ee8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 74 deletions.
104 changes: 38 additions & 66 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import replace from "gulp-replace";
import rimraf from "rimraf";
import stream from "stream";
import streamqueue from "streamqueue";
import TerserPlugin from "terser-webpack-plugin";
import through from "through2";
import Vinyl from "vinyl";
import webpack2 from "webpack";
Expand Down Expand Up @@ -210,6 +211,7 @@ function createWebpackConfig(
!bundleDefines.TESTING &&
!disableSourceMaps;
const isModule = output.library?.type === "module";
const isMinified = bundleDefines.MINIFIED;
const skipBabel = bundleDefines.SKIP_BABEL;

const babelExcludeRegExp = [
Expand Down Expand Up @@ -333,7 +335,28 @@ function createWebpackConfig(
mode: "production",
optimization: {
mangleExports: false,
minimize: false,
minimize: isMinified,
minimizer: !isMinified
? undefined
: [
new TerserPlugin({
extractComments: false,
parallel: false,
terserOptions: {
compress: {
// V8 chokes on very long sequences, work around that.
sequences: false,
},
mangle: {
// Ensure that the `tweakWebpackOutput` function works.
reserved: ["__webpack_exports__"],
},
keep_classnames: true,
keep_fnames: true,
module: isModule,
},
}),
],
},
experiments,
output,
Expand Down Expand Up @@ -421,24 +444,30 @@ function checkChromePreferencesFile(chromePrefsPath, webPrefs) {
function tweakWebpackOutput(jsName) {
const replacer = [
" __webpack_exports__ = {};",
",__webpack_exports__={};",
" __webpack_exports__ = await __webpack_exports__;",
"\\(__webpack_exports__=await __webpack_exports__\\)",
];
const regex = new RegExp(`(${replacer.join("|")})`, "gm");

return replace(regex, match => {
switch (match) {
case " __webpack_exports__ = {};":
return ` __webpack_exports__ = globalThis.${jsName} = {};`;
case ",__webpack_exports__={};":
return `,__webpack_exports__=globalThis.${jsName}={};`;
case " __webpack_exports__ = await __webpack_exports__;":
return ` __webpack_exports__ = globalThis.${jsName} = await (globalThis.${jsName}Promise = __webpack_exports__);`;
case "(__webpack_exports__=await __webpack_exports__)":
return `(__webpack_exports__=globalThis.${jsName}=await (globalThis.${jsName}Promise=__webpack_exports__))`;
}
return match;
});
}

function createMainBundle(defines) {
const mainFileConfig = createWebpackConfig(defines, {
filename: "pdf.mjs",
filename: defines.MINIFIED ? "pdf.min.mjs" : "pdf.mjs",
library: {
type: "module",
},
Expand Down Expand Up @@ -502,7 +531,9 @@ function createSandboxBundle(defines, extraOptions = undefined) {
const sandboxFileConfig = createWebpackConfig(
sandboxDefines,
{
filename: "pdf.sandbox.mjs",
filename: sandboxDefines.MINIFIED
? "pdf.sandbox.min.mjs"
: "pdf.sandbox.mjs",
library: {
type: "module",
},
Expand All @@ -518,7 +549,7 @@ function createSandboxBundle(defines, extraOptions = undefined) {

function createWorkerBundle(defines) {
const workerFileConfig = createWebpackConfig(defines, {
filename: "pdf.worker.mjs",
filename: defines.MINIFIED ? "pdf.worker.min.mjs" : "pdf.worker.mjs",
library: {
type: "module",
},
Expand Down Expand Up @@ -578,7 +609,9 @@ function createComponentsBundle(defines) {

function createImageDecodersBundle(defines) {
const componentsFileConfig = createWebpackConfig(defines, {
filename: "pdf.image_decoders.mjs",
filename: defines.MINIFIED
? "pdf.image_decoders.min.mjs"
: "pdf.image_decoders.mjs",
library: {
type: "module",
},
Expand Down Expand Up @@ -1178,59 +1211,6 @@ function buildMinified(defines, dir) {
]);
}

async function parseMinified(dir) {
const pdfFile = fs.readFileSync(dir + "build/pdf.mjs").toString();
const pdfWorkerFile = fs
.readFileSync(dir + "build/pdf.worker.mjs")
.toString();
const pdfSandboxFile = fs
.readFileSync(dir + "build/pdf.sandbox.mjs")
.toString();
const pdfImageDecodersFile = fs
.readFileSync(dir + "image_decoders/pdf.image_decoders.mjs")
.toString();

console.log();
console.log("### Minifying js files");

const { minify } = await import("terser");
const options = {
compress: {
// V8 chokes on very long sequences, work around that.
sequences: false,
},
keep_classnames: true,
keep_fnames: true,
module: true,
};

await Promise.all([
minify(pdfFile, options).then(res => {
fs.writeFileSync(dir + "build/pdf.min.mjs", res.code);
}),
minify(pdfWorkerFile, options).then(res => {
fs.writeFileSync(dir + "build/pdf.worker.min.mjs", res.code);
}),
minify(pdfSandboxFile, options).then(res => {
fs.writeFileSync(dir + "build/pdf.sandbox.min.mjs", res.code);
}),
minify(pdfImageDecodersFile, options).then(res => {
fs.writeFileSync(
dir + "image_decoders/pdf.image_decoders.min.mjs",
res.code
);
}),
]);

console.log();
console.log("### Cleaning js files");

fs.unlinkSync(dir + "build/pdf.mjs");
fs.unlinkSync(dir + "build/pdf.worker.mjs");
fs.unlinkSync(dir + "build/pdf.sandbox.mjs");
fs.unlinkSync(dir + "image_decoders/pdf.image_decoders.mjs");
}

gulp.task(
"minified",
gulp.series(
Expand All @@ -1252,10 +1232,6 @@ gulp.task(
const defines = { ...DEFINES, MINIFIED: true, GENERIC: true };

return buildMinified(defines, MINIFIED_DIR);
},
async function compressMinified(done) {
await parseMinified(MINIFIED_DIR);
done();
}
)
);
Expand Down Expand Up @@ -1291,10 +1267,6 @@ gulp.task(
};

return buildMinified(defines, MINIFIED_LEGACY_DIR);
},
async function compressMinifiedLegacy(done) {
await parseMinified(MINIFIED_LEGACY_DIR);
done();
}
)
);
Expand Down
8 changes: 1 addition & 7 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
Expand Up @@ -52,7 +52,7 @@
"streamqueue": "^1.1.2",
"stylelint": "^16.2.1",
"stylelint-prettier": "^5.0.0",
"terser": "^5.27.1",
"terser-webpack-plugin": "^5.3.10",
"through2": "^4.0.2",
"tsc-alias": "^1.8.8",
"ttest": "^4.0.0",
Expand Down

0 comments on commit 4ac8ee8

Please sign in to comment.