mirror of https://github.com/webpack/webpack.git
fix: defer import mangling (#19988)
This commit is contained in:
parent
d32f1711ac
commit
f3ef1428b3
|
@ -949,7 +949,16 @@ class RuntimeTemplate {
|
|||
// when the defaultInterop is used (when a ESM imports a CJS module),
|
||||
if (exportName.length > 0 && exportName[0] === "default") {
|
||||
if (isDeferred && exportsType !== "namespace") {
|
||||
const access = `${importVar}.a${propertyAccess(exportName, 1)}`;
|
||||
const exportsInfo = moduleGraph.getExportsInfo(module);
|
||||
const name = exportName.slice(1);
|
||||
const used = exportsInfo.getUsedName(name, runtime);
|
||||
if (!used) {
|
||||
const comment = Template.toNormalComment(
|
||||
`unused export ${propertyAccess(exportName)}`
|
||||
);
|
||||
return `${comment} undefined`;
|
||||
}
|
||||
const access = `${importVar}.a${propertyAccess(used)}`;
|
||||
if (isCall || asiSafe === undefined) {
|
||||
return access;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"foo": "bar",
|
||||
"nested": { "foo": "bar" }
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
import defer * as mod1 from "./file.ext" with { type: "bytes" };
|
||||
import defer * as mod2 from "./file.ext" with { type: "json" };
|
||||
import * as mod3 from "./file.ext" with { type: "bytes" };
|
||||
import * as mod4 from "./file.ext" with { type: "json" };
|
||||
|
||||
it("should work with defer and import attributes", () => {
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
const mod1Decoded = JSON.parse(decoder.decode(mod1.default));
|
||||
expect(mod1Decoded.foo).toBe("bar");
|
||||
expect(mod1Decoded.nested.foo).toBe("bar");
|
||||
expect(mod2.default.foo).toBe("bar");
|
||||
expect(mod2.default.nested.foo).toBe("bar");
|
||||
const mod2Decoded = JSON.parse(decoder.decode(mod3.default));
|
||||
expect(mod2Decoded.foo).toBe("bar");
|
||||
expect(mod2Decoded.nested.foo).toBe("bar");
|
||||
expect(mod4.default.foo).toBe("bar");
|
||||
expect(mod4.default.nested.foo).toBe("bar");
|
||||
});
|
|
@ -0,0 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
const supportsTextDecoder = require("../../../helpers/supportsTextDecoder");
|
||||
|
||||
module.exports = () => supportsTextDecoder();
|
|
@ -0,0 +1,9 @@
|
|||
"use strict";
|
||||
|
||||
/** @type {import("../../../../").Configuration} */
|
||||
module.exports = {
|
||||
target: [`async-node${process.versions.node.split(".").map(Number)[0]}`],
|
||||
experiments: {
|
||||
deferImport: true
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue