runtimeConditionExpression({
chunkGraph,
runtimeCondition,
runtime,
runtimeRequirements
}) {
if (runtimeCondition === undefined) return "true";
if (typeof runtimeCondition === "boolean") return `${runtimeCondition}`;
const positiveRuntimeIds = new Set();
forEachRuntime(runtimeCondition, runtime =>
positiveRuntimeIds.add(`${chunkGraph.getRuntimeId(/** @type {string} */ (runtime))}`)
);
const negativeRuntimeIds = new Set();
forEachRuntime(subtractRuntime(runtime, runtimeCondition), runtime =>
negativeRuntimeIds.add(`${chunkGraph.getRuntimeId(/** @type {string} */ (runtime))}`)
);
runtimeRequirements.add(RuntimeGlobals.runtimeId);
return compileBooleanMatcher.fromLists(
Array.from(positiveRuntimeIds),
Array.from(negativeRuntimeIds)
)(RuntimeGlobals.runtimeId);
},
importStatement({
update,
module,
chunkGraph,
request,
importVar,
originModule,
weak,
runtimeRequirements
}) {
if (!module) {
return [
this.missingModuleStatement({ request }),
""
];
}
if (chunkGraph.getModuleId(module) === null) {
if (weak) {
return [
this.weakError({ module, chunkGraph, request, type: "statements" }),
""
];
}
throw new Error(
`RuntimeTemplate.importStatement(): ${noModuleIdErrorMessage(module, chunkGraph)}`
);
}
const moduleId = this.moduleId({
module,
chunkGraph,
request,
weak
});
const optDeclaration = update ? "" : "var ";
const exportsType = module.getExportsType(
chunkGraph.moduleGraph,
(originModule.buildMeta).strictHarmonyModule
);
runtimeRequirements.add(RuntimeGlobals.require);
const importContent = `/* harmony import */ ${optDeclaration}${importVar} = ${RuntimeGlobals.require}(${moduleId});\n`;
if (exportsType === "dynamic") {
runtimeRequirements.add(RuntimeGlobals.compatGetDefaultExport);
return [
importContent,
`/* harmony import */ ${optDeclaration}${importVar}_default = /*#__PURE__*/${RuntimeGlobals.compatGetDefaultExport}(${importVar});\n`
];
}
return [importContent, ""];
},
exportFromImport({
moduleGraph,
module,
request,
exportName,
originModule,
asiSafe,
isCall,
callContext,
defaultInterop,
importVar,
initFragments,
runtime,
runtimeRequirements
}) {
if (!module) return this.missingModule({ request });
if (!Array.isArray(exportName)) {
exportName = exportName ? [exportName] : [];
}
const exportsType = module.getExportsType(
moduleGraph,
(originModule.buildMeta).strictHarmonyModule
);
if (defaultInterop) {
if (exportName.length > 0 && exportName[0] === "default") {
switch (exportsType) {
case "dynamic":
if (isCall) {
return `${importVar}_default()${propertyAccess(exportName, 1)}`;
}
return asiSafe
? `(${importVar}_default()${propertyAccess(exportName, 1)})`
: asiSafe === false
? `;(${importVar}_default()${propertyAccess(exportName, 1)})`
: `${importVar}_default.a${propertyAccess(exportName, 1)}`;
case "default-only":
case "default-with-named":
exportName = exportName.slice(1);
break;
}
} else if (exportName.length > 0) {
if (exportsType === "default-only") {
return `/* non-default import from non-esm module */undefined${propertyAccess(exportName, 1)}`;
} else if (exportsType !== "namespace" && exportName[0] === "__esModule") {
return "/* __esModule */true";
}
} else if (
exportsType === "default-only" ||
exportsType === "default-with-named"
) {
runtimeRequirements.add(RuntimeGlobals.createFakeNamespaceObject);
initFragments.push(
new InitFragment(
`var ${importVar}_namespace_cache;\n`,
InitFragment.STAGE_CONSTANTS,
-1,
`${importVar}_namespace_cache`
)
);
return `/*#__PURE__*/ ${
asiSafe ? "" : asiSafe === false ? ";" : "Object"
}(${importVar}_namespace_cache || (${importVar}_namespace_cache = ${
RuntimeGlobals.createFakeNamespaceObject
}(${importVar}${exportsType === "default-only" ? "" : ", 2"})))`;
}
}
if (exportName.length > 0) {
const exportsInfo = moduleGraph.getExportsInfo(module);
const used = exportsInfo.getUsedName(exportName, runtime);
if (!used) {
const comment = Template.toNormalComment(`unused export ${propertyAccess(exportName)}`);
return `${comment} undefined`;
}
const comment = equals(used, exportName)
? ""
: `${Template.toNormalComment(propertyAccess(exportName))} `;
const access = `${importVar}${comment}${propertyAccess(used)}`;
if (isCall && callContext === false) {
return asiSafe
? `(0,${access})`
: asiSafe === false
? `;(0,${access})`
: `/*#__PURE__*/Object(${access})`;
}
return access;
}
return importVar;
}