From 4ab0ad32167bd1189dead51a565e66772317d571 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 8 Feb 2024 12:13:30 +0100 Subject: [PATCH] Move the `__non_webpack_import__` re-writing into the Babel plugin Note how we're using custom `__non_webpack_import__`-calls in the code-base, that we replace during the post-processing stage of the build, to be able to write `import`-calls that Webpack will leave alone during parsing. This work-around is necessary since we let Babel discards all comments, given that we generally don't need/want them in the builds, hence why we cannot utilize `/* webpackIgnore: true */`-comments in the source-code. After the changes in PR 17563 it thus seems to me that we should be able to just move this re-writing into the Babel plugin instead. --- .../babel-plugin-pdfjs-preprocessor.mjs | 16 +++++++++++++++ .../fixtures_esprima/importalias-expected.js | 1 + .../builder/fixtures_esprima/importalias.js | 1 + gulpfile.mjs | 20 ++----------------- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/external/builder/babel-plugin-pdfjs-preprocessor.mjs b/external/builder/babel-plugin-pdfjs-preprocessor.mjs index 653c459fa..87b024bab 100644 --- a/external/builder/babel-plugin-pdfjs-preprocessor.mjs +++ b/external/builder/babel-plugin-pdfjs-preprocessor.mjs @@ -172,6 +172,22 @@ function babelPluginPDFJSPreprocessor(babel, ctx) { ); path.replaceWith(t.inherits(t.valueToNode(result), path.node)); } + + if (t.isIdentifier(node.callee, { name: "__non_webpack_import__" })) { + if (node.arguments.length !== 1) { + throw new Error("Invalid `__non_webpack_import__` usage."); + } + // Replace it with a standard `import`-call and + // ensure that Webpack will leave it alone. + const source = node.arguments[0]; + source.leadingComments = [ + { + type: "CommentBlock", + value: "webpackIgnore: true", + }, + ]; + path.replaceWith(t.importExpression(source)); + } }, BlockStatement: { // Visit node in post-order so that recursive flattening diff --git a/external/builder/fixtures_esprima/importalias-expected.js b/external/builder/fixtures_esprima/importalias-expected.js index cbf824b82..e869ed044 100644 --- a/external/builder/fixtures_esprima/importalias-expected.js +++ b/external/builder/fixtures_esprima/importalias-expected.js @@ -1,3 +1,4 @@ import { Test } from "import-name"; import { Test2 } from './non-alias'; export { Test3 } from "import-name"; +await import( /*webpackIgnore: true*/"./non-alias"); diff --git a/external/builder/fixtures_esprima/importalias.js b/external/builder/fixtures_esprima/importalias.js index 4e6d79e8a..955b67e6e 100644 --- a/external/builder/fixtures_esprima/importalias.js +++ b/external/builder/fixtures_esprima/importalias.js @@ -1,3 +1,4 @@ import { Test } from 'import-alias'; import { Test2 } from './non-alias'; export { Test3 } from 'import-alias'; +await __non_webpack_import__("./non-alias"); diff --git a/gulpfile.mjs b/gulpfile.mjs index df641c746..2babd3d82 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -419,7 +419,7 @@ function checkChromePreferencesFile(chromePrefsPath, webPrefs) { } function tweakWebpackOutput(jsName) { - const replacer = ["__non_webpack_import__\\("]; + const replacer = []; if (jsName) { replacer.push( @@ -431,8 +431,6 @@ function tweakWebpackOutput(jsName) { return replace(regex, match => { switch (match) { - case "__non_webpack_import__(": - return "import(/* webpackIgnore: true */ "; case " __webpack_exports__ = {};": return ` __webpack_exports__ = globalThis.${jsName} = {};`; case " __webpack_exports__ = await __webpack_exports__;": @@ -1572,17 +1570,6 @@ gulp.task("types", function (done) { }); function buildLibHelper(bundleDefines, inputStream, outputDir) { - function babelPluginReplaceNonWebpackImport(b) { - return { - visitor: { - Identifier(curPath, state) { - if (curPath.node.name === "__non_webpack_import__") { - curPath.replaceWith(b.types.identifier("import")); - } - }, - }, - }; - } function preprocessLib(content) { const skipBabel = bundleDefines.SKIP_BABEL; content = babel.transform(content, { @@ -1590,10 +1577,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) { presets: skipBabel ? undefined : [["@babel/preset-env", { loose: false, modules: false }]], - plugins: [ - babelPluginReplaceNonWebpackImport, - [babelPluginPDFJSPreprocessor, ctx], - ], + plugins: [[babelPluginPDFJSPreprocessor, ctx]], targets: BABEL_TARGETS, }).code; content = content.replaceAll(