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(