UNPKG

2.76 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { UsageState } = require("../ExportsInfo");
9const InitFragment = require("../InitFragment");
10const RuntimeGlobals = require("../RuntimeGlobals");
11const makeSerializable = require("../util/makeSerializable");
12const NullDependency = require("./NullDependency");
13
14/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
15/** @typedef {import("../Dependency")} Dependency */
16/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
17/** @typedef {import("../Module")} Module */
18
19class HarmonyCompatibilityDependency extends NullDependency {
20 get type() {
21 return "harmony export header";
22 }
23}
24
25makeSerializable(
26 HarmonyCompatibilityDependency,
27 "webpack/lib/dependencies/HarmonyCompatibilityDependency"
28);
29
30HarmonyCompatibilityDependency.Template = class HarmonyExportDependencyTemplate extends (
31 NullDependency.Template
32) {
33 /**
34 * @param {Dependency} dependency the dependency for which the template should be applied
35 * @param {ReplaceSource} source the current replace source which can be modified
36 * @param {DependencyTemplateContext} templateContext the context object
37 * @returns {void}
38 */
39 apply(
40 dependency,
41 source,
42 {
43 module,
44 runtimeTemplate,
45 moduleGraph,
46 initFragments,
47 runtimeRequirements,
48 runtime,
49 concatenationScope
50 }
51 ) {
52 if (concatenationScope) return;
53 const exportsInfo = moduleGraph.getExportsInfo(module);
54 if (
55 exportsInfo.getReadOnlyExportInfo("__esModule").getUsed(runtime) !==
56 UsageState.Unused
57 ) {
58 const content = runtimeTemplate.defineEsModuleFlagStatement({
59 exportsArgument: module.exportsArgument,
60 runtimeRequirements
61 });
62 initFragments.push(
63 new InitFragment(
64 content,
65 InitFragment.STAGE_HARMONY_EXPORTS,
66 0,
67 "harmony compatibility"
68 )
69 );
70 }
71 if (moduleGraph.isAsync(module)) {
72 runtimeRequirements.add(RuntimeGlobals.module);
73 runtimeRequirements.add(RuntimeGlobals.asyncModule);
74 initFragments.push(
75 new InitFragment(
76 runtimeTemplate.supportsArrowFunction()
77 ? `${RuntimeGlobals.asyncModule}(${module.moduleArgument}, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {\n`
78 : `${RuntimeGlobals.asyncModule}(${module.moduleArgument}, async function (__webpack_handle_async_dependencies__, __webpack_async_result__) { try {\n`,
79 InitFragment.STAGE_ASYNC_BOUNDARY,
80 0,
81 undefined,
82 `\n__webpack_async_result__();\n} catch(e) { __webpack_async_result__(e); } }${
83 module.buildMeta.async ? ", 1" : ""
84 });`
85 )
86 );
87 }
88 }
89};
90
91module.exports = HarmonyCompatibilityDependency;