Merge pull request #17637 from Snuffleupagus/babel-plugin-__non_webpack_import__

Move the `__non_webpack_import__` re-writing into the Babel plugin
This commit is contained in:
Jonas Jenwald 2024-02-14 22:23:24 +01:00 committed by GitHub
commit dbda3ec5f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 18 deletions

View File

@ -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

View File

@ -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");

View File

@ -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");

View File

@ -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(