webpack/lib/dependencies/HarmonyCompatibilityDepende...

89 lines
2.4 KiB
JavaScript
Raw 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 */
/** @typedef {import("../Module")} Module */
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);
const used = exportsInfo.isUsed(runtime);
if (used) runtimeRequirements.add(RuntimeGlobals.exports);
initFragments.push(
2019-06-05 20:17:15 +08:00
new InitFragment(
`${module.moduleArgument}.exports = (async () => {\n`,
InitFragment.STAGE_ASYNC_BOUNDARY,
0,
undefined,
used ? `\nreturn ${module.exportsArgument};\n})();` : "\n})();"
2019-06-05 20:17:15 +08:00
)
);
}
}
2017-01-11 17:51:58 +08:00
};
module.exports = HarmonyCompatibilityDependency;