Skip to content

Commit

Permalink
Merge pull request #17637 from Snuffleupagus/babel-plugin-__non_webpa…
Browse files Browse the repository at this point in the history
…ck_import__

Move the `__non_webpack_import__` re-writing into the Babel plugin
  • Loading branch information
Snuffleupagus authored Feb 14, 2024
2 parents 14874e5 + 4ab0ad3 commit dbda3ec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
16 changes: 16 additions & 0 deletions external/builder/babel-plugin-pdfjs-preprocessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions external/builder/fixtures_esprima/importalias-expected.js
Original file line number Diff line number Diff line change
@@ -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");
1 change: 1 addition & 0 deletions external/builder/fixtures_esprima/importalias.js
Original file line number Diff line number Diff line change
@@ -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");
20 changes: 2 additions & 18 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ function checkChromePreferencesFile(chromePrefsPath, webPrefs) {
}

function tweakWebpackOutput(jsName) {
const replacer = ["__non_webpack_import__\\("];
const replacer = [];

if (jsName) {
replacer.push(
Expand All @@ -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__;":
Expand Down Expand Up @@ -1572,28 +1570,14 @@ 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, {
sourceType: "module",
presets: skipBabel
? undefined
: [["@babel/preset-env", { loose: false, modules: false }]],
plugins: [
babelPluginReplaceNonWebpackImport,
[babelPluginPDFJSPreprocessor, ctx],
],
plugins: [[babelPluginPDFJSPreprocessor, ctx]],
targets: BABEL_TARGETS,
}).code;
content = content.replaceAll(
Expand Down

0 comments on commit dbda3ec

Please sign in to comment.