webpack/lib/dependencies/HarmonyCompatibilityDepende...

92 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const { UsageState } = require("../ExportsInfo");
2018-07-30 01:26:57 +08:00
const InitFragment = require("../InitFragment");
const RuntimeGlobals = require("../RuntimeGlobals");
2018-10-09 20:30:59 +08:00
const makeSerializable = require("../util/makeSerializable");
const NullDependency = require("./NullDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
2018-07-30 23:08:51 +08:00
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2023-06-17 02:24:34 +08:00
/** @typedef {import("../Module").BuildMeta} BuildMeta */
class HarmonyCompatibilityDependency extends NullDependency {
get type() {
return "harmony export header";
}
}
2018-10-09 20:30:59 +08:00
makeSerializable(
HarmonyCompatibilityDependency,
"webpack/lib/dependencies/HarmonyCompatibilityDependency"
);
2020-11-26 17:52:55 +08:00
HarmonyCompatibilityDependency.Template = class HarmonyExportDependencyTemplate extends (
NullDependency.Template
) {
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
apply(
dependency,
source,
{
module,
runtimeTemplate,
moduleGraph,
initFragments,
runtimeRequirements,
2020-09-11 15:06:24 +08:00
runtime,
concatenationScope
}
) {
2020-09-11 15:06:24 +08:00
if (concatenationScope) return;
const exportsInfo = moduleGraph.getExportsInfo(module);
if (
exportsInfo.getReadOnlyExportInfo("__esModule").getUsed(runtime) !==
UsageState.Unused
) {
const content = runtimeTemplate.defineEsModuleFlagStatement({
2018-11-17 01:18:44 +08:00
exportsArgument: module.exportsArgument,
runtimeRequirements
});
initFragments.push(
2018-07-30 16:15:18 +08:00
new InitFragment(
content,
InitFragment.STAGE_HARMONY_EXPORTS,
0,
"harmony compatibility"
)
);
}
2019-06-05 17:15:25 +08:00
if (moduleGraph.isAsync(module)) {
runtimeRequirements.add(RuntimeGlobals.module);
2021-01-29 18:12:11 +08:00
runtimeRequirements.add(RuntimeGlobals.asyncModule);
initFragments.push(
2019-06-05 20:17:15 +08:00
new InitFragment(
2021-03-13 22:21:13 +08:00
runtimeTemplate.supportsArrowFunction()
? `${RuntimeGlobals.asyncModule}(${module.moduleArgument}, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {\n`
: `${RuntimeGlobals.asyncModule}(${module.moduleArgument}, async function (__webpack_handle_async_dependencies__, __webpack_async_result__) { try {\n`,
2019-06-05 20:17:15 +08:00
InitFragment.STAGE_ASYNC_BOUNDARY,
0,
undefined,
`\n__webpack_async_result__();\n} catch(e) { __webpack_async_result__(e); } }${
2023-06-17 02:24:34 +08:00
/** @type {BuildMeta} */ (module.buildMeta).async ? ", 1" : ""
});`
2019-06-05 20:17:15 +08:00
)
);
}
}
2017-01-11 17:51:58 +08:00
};
module.exports = HarmonyCompatibilityDependency;