diff --git a/external/builder/babel-plugin-pdfjs-preprocessor.mjs b/external/builder/babel-plugin-pdfjs-preprocessor.mjs index 180370d92..7c74f0ef5 100644 --- a/external/builder/babel-plugin-pdfjs-preprocessor.mjs +++ b/external/builder/babel-plugin-pdfjs-preprocessor.mjs @@ -68,8 +68,14 @@ function babelPluginPDFJSPreprocessor(babel, ctx) { return { name: "babel-plugin-pdfjs-preprocessor", - manipulateOptions({ parserOpts }) { - parserOpts.attachComment = false; + manipulateOptions({ parserOpts, generatorOpts }) { + const namedWorkerChunks = + ctx.defines?.MOZCENTRAL && ctx.defines.WORKER_THREAD; + parserOpts.attachComment = !!namedWorkerChunks; + if (namedWorkerChunks) { + generatorOpts.shouldPrintComment = comment => + /webpack(?:ChunkName|Ignore|Mode):|@vite-ignore/.test(comment); + } }, visitor: { "ExportNamedDeclaration|ImportDeclaration": ({ node }) => { diff --git a/gulpfile.mjs b/gulpfile.mjs index fec2a713d..fbbeacb13 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -657,10 +657,35 @@ function createWorkerBundle(defines) { }; const workerFileConfig = createWebpackConfig(workerDefines, { filename: workerDefines.MINIFIED ? "pdf.worker.min.mjs" : "pdf.worker.mjs", + // Keep the standalone worker used by generic builds self-contained. + asyncChunks: workerDefines.MOZCENTRAL, + ...(workerDefines.MOZCENTRAL && { + chunkFilename: "pdf.worker.[name].mjs", + chunkFormat: "module", + chunkLoading: "import", + }), library: { type: "module", }, }); + if (workerDefines.MOZCENTRAL) { + workerFileConfig.optimization.chunkIds = "named"; + workerFileConfig.optimization.splitChunks = { + cacheGroups: { + default: { + // Keep the XHTML parser in both chunks so each can load on its own. + test: module => + !/[/\\]src[/\\]core[/\\]xfa[/\\]/.test(module.nameForCondition()), + idHint: "", + minChunks: 2, + // Share small modules too, e.g. the writer. + minSize: 0, + priority: -20, + reuseExistingChunk: true, + }, + }, + }; + } return createWebpackStream("./src/pdf.worker.js", workerFileConfig); }