fix: add runtime condition for harmony reexport checked

This commit is contained in:
ahabhgk 2024-07-26 00:57:47 +08:00
parent aa7174671e
commit a08d9d590e
11 changed files with 87 additions and 3 deletions

View File

@ -5,6 +5,7 @@
"use strict";
const ConditionalInitFragment = require("../ConditionalInitFragment");
const Dependency = require("../Dependency");
const { UsageState } = require("../ExportsInfo");
const HarmonyLinkingError = require("../HarmonyLinkingError");
@ -16,7 +17,11 @@ const { first, combine } = require("../util/SetHelpers");
const makeSerializable = require("../util/makeSerializable");
const propertyAccess = require("../util/propertyAccess");
const { propertyName } = require("../util/propertyName");
const { getRuntimeKey, keyToRuntime } = require("../util/runtime");
const {
getRuntimeKey,
keyToRuntime,
filterRuntime
} = require("../util/runtime");
const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
const HarmonyImportDependency = require("./HarmonyImportDependency");
const processExportInfo = require("./processExportInfo");
@ -1083,8 +1088,18 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
for (const { name, ids, checked, hidden } of mode.items) {
if (hidden) continue;
if (checked) {
const connection = moduleGraph.getConnection(dep);
const moduleKey = importedModule
? importedModule.identifier()
: dep.request;
const key = `harmony reexport (checked) ${moduleKey}`;
const runtimeCondition = dep.weak
? false
: connection
? filterRuntime(runtime, r => connection.isTargetActive(r))
: true;
initFragments.push(
new InitFragment(
new ConditionalInitFragment(
"/* harmony reexport (checked) */ " +
this.getConditionalReexportStatement(
module,
@ -1096,7 +1111,9 @@ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedS
moduleGraph.isAsync(importedModule)
? InitFragment.STAGE_ASYNC_HARMONY_IMPORTS
: InitFragment.STAGE_HARMONY_IMPORTS,
dep.sourceOrder
dep.sourceOrder,
key,
runtimeCondition
)
);
} else {

View File

@ -0,0 +1,5 @@
import { utilA } from "./lib"
it("should not emit error when running a.js (runtime a)", () => {
expect(utilA()).toBe("a");
})

View File

@ -0,0 +1,5 @@
import { utilB } from "./lib"
it("should not emit error when running b.js (runtime b)", () => {
expect(utilB()).toBe("[object Object] 1");
})

View File

@ -0,0 +1,6 @@
// usually this is generated by typescript `enum common { C }`
var common = /* @__PURE__ */ ((common) => {
common[common["C"] = 1] = "C";
return common;
})(common || {}); // the `{}` (inside `common || {}`) has side effect
export { common }

View File

@ -0,0 +1,2 @@
export * from "./common"
export * from "./empty"

View File

@ -0,0 +1,3 @@
export * from "./util-a"
export * from "./common"
export * from "./util-b"

View File

@ -0,0 +1,3 @@
export function utilA() {
return 'a';
}

View File

@ -0,0 +1,5 @@
import { common } from "./common"
var b = ({}).toString(); // side effect
export function utilB() {
return b + ' ' + common.C;
}

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle() {
return ["./lib.js", "./a.js", "./b.js"];
}
};

View File

@ -0,0 +1,33 @@
/** @type {import("webpack").Configuration} */
module.exports = {
entry: {
a: "./a.js",
b: "./b.js"
},
output: {
filename: "[name].js"
},
target: "web",
mode: "production",
module: {
rules: [
{
test: /lib\/common/,
sideEffects: false
}
]
},
optimization: {
concatenateModules: false,
splitChunks: {
cacheGroups: {
lib: {
name: "lib",
test: /lib/,
chunks: "all",
minSize: 0
}
}
}
}
};