| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const ConcatenationScope = require("../ConcatenationScope");
|
|---|
| 9 | const InitFragment = require("../InitFragment");
|
|---|
| 10 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 11 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 12 | const { propertyAccess } = require("../util/property");
|
|---|
| 13 | const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
|
|---|
| 14 | const NullDependency = require("./NullDependency");
|
|---|
| 15 |
|
|---|
| 16 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 17 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 18 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
|---|
| 19 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 20 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 21 | /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
|---|
| 22 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 23 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 24 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 25 | /** @typedef {import("./HarmonyExportInitFragment").ExportMap} ExportMap */
|
|---|
| 26 |
|
|---|
| 27 | class HarmonyExportExpressionDependency extends NullDependency {
|
|---|
| 28 | /**
|
|---|
| 29 | * Creates an instance of HarmonyExportExpressionDependency.
|
|---|
| 30 | * @param {Range} range range
|
|---|
| 31 | * @param {Range} rangeStatement range statement
|
|---|
| 32 | * @param {string} prefix prefix
|
|---|
| 33 | * @param {string | { id?: string | undefined, range: Range, prefix: string, suffix: string }=} declarationId declaration id
|
|---|
| 34 | */
|
|---|
| 35 | constructor(range, rangeStatement, prefix, declarationId) {
|
|---|
| 36 | super();
|
|---|
| 37 | this.range = range;
|
|---|
| 38 | this.rangeStatement = rangeStatement;
|
|---|
| 39 | this.prefix = prefix;
|
|---|
| 40 | this.declarationId = declarationId;
|
|---|
| 41 | this.isAnonymousDefault = false;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | get type() {
|
|---|
| 45 | return "harmony export expression";
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Returns the exported names
|
|---|
| 50 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 51 | * @returns {ExportsSpec | undefined} export names
|
|---|
| 52 | */
|
|---|
| 53 | getExports(moduleGraph) {
|
|---|
| 54 | return {
|
|---|
| 55 | exports: ["default"],
|
|---|
| 56 | priority: 1,
|
|---|
| 57 | terminalBinding: true,
|
|---|
| 58 | dependencies: undefined
|
|---|
| 59 | };
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Gets module evaluation side effects state.
|
|---|
| 64 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 65 | * @returns {ConnectionState} how this dependency connects the module to referencing modules
|
|---|
| 66 | */
|
|---|
| 67 | getModuleEvaluationSideEffectsState(moduleGraph) {
|
|---|
| 68 | // The expression/declaration is already covered by SideEffectsFlagPlugin
|
|---|
| 69 | return false;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Serializes this instance into the provided serializer context.
|
|---|
| 74 | * @param {ObjectSerializerContext} context context
|
|---|
| 75 | */
|
|---|
| 76 | serialize(context) {
|
|---|
| 77 | const { write } = context;
|
|---|
| 78 | write(this.range);
|
|---|
| 79 | write(this.rangeStatement);
|
|---|
| 80 | write(this.prefix);
|
|---|
| 81 | write(this.declarationId);
|
|---|
| 82 | write(this.isAnonymousDefault);
|
|---|
| 83 | super.serialize(context);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /**
|
|---|
| 87 | * Restores this instance from the provided deserializer context.
|
|---|
| 88 | * @param {ObjectDeserializerContext} context context
|
|---|
| 89 | */
|
|---|
| 90 | deserialize(context) {
|
|---|
| 91 | const { read } = context;
|
|---|
| 92 | this.range = read();
|
|---|
| 93 | this.rangeStatement = read();
|
|---|
| 94 | this.prefix = read();
|
|---|
| 95 | this.declarationId = read();
|
|---|
| 96 | this.isAnonymousDefault = read();
|
|---|
| 97 | super.deserialize(context);
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | makeSerializable(
|
|---|
| 102 | HarmonyExportExpressionDependency,
|
|---|
| 103 | "webpack/lib/dependencies/HarmonyExportExpressionDependency"
|
|---|
| 104 | );
|
|---|
| 105 |
|
|---|
| 106 | HarmonyExportExpressionDependency.Template = class HarmonyExportDependencyTemplate extends (
|
|---|
| 107 | NullDependency.Template
|
|---|
| 108 | ) {
|
|---|
| 109 | /**
|
|---|
| 110 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 111 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 112 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 113 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 114 | * @returns {void}
|
|---|
| 115 | */
|
|---|
| 116 | apply(
|
|---|
| 117 | dependency,
|
|---|
| 118 | source,
|
|---|
| 119 | {
|
|---|
| 120 | module,
|
|---|
| 121 | moduleGraph,
|
|---|
| 122 | runtimeTemplate,
|
|---|
| 123 | runtimeRequirements,
|
|---|
| 124 | initFragments,
|
|---|
| 125 | runtime,
|
|---|
| 126 | concatenationScope
|
|---|
| 127 | }
|
|---|
| 128 | ) {
|
|---|
| 129 | const dep = /** @type {HarmonyExportExpressionDependency} */ (dependency);
|
|---|
| 130 | const { declarationId } = dep;
|
|---|
| 131 | const exportsName = module.exportsArgument;
|
|---|
| 132 | if (declarationId) {
|
|---|
| 133 | /** @type {string} */
|
|---|
| 134 | let name;
|
|---|
| 135 | if (typeof declarationId === "string") {
|
|---|
| 136 | name = declarationId;
|
|---|
| 137 | } else {
|
|---|
| 138 | name = ConcatenationScope.DEFAULT_EXPORT;
|
|---|
| 139 | source.replace(
|
|---|
| 140 | declarationId.range[0],
|
|---|
| 141 | declarationId.range[1] - 1,
|
|---|
| 142 | `${declarationId.prefix}${name}${declarationId.suffix}`
|
|---|
| 143 | );
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | const used = concatenationScope
|
|---|
| 147 | ? undefined
|
|---|
| 148 | : moduleGraph.getExportsInfo(module).getUsedName("default", runtime);
|
|---|
| 149 |
|
|---|
| 150 | if (concatenationScope) {
|
|---|
| 151 | concatenationScope.registerExport("default", name);
|
|---|
| 152 | } else if (used) {
|
|---|
| 153 | /** @type {ExportMap} */
|
|---|
| 154 | const map = new Map();
|
|---|
| 155 | map.set(used, `/* export default binding */ ${name}`);
|
|---|
| 156 | initFragments.push(new HarmonyExportInitFragment(exportsName, map));
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | source.replace(
|
|---|
| 160 | dep.rangeStatement[0],
|
|---|
| 161 | dep.range[0] - 1,
|
|---|
| 162 | `/* harmony default export */ ${dep.prefix}`
|
|---|
| 163 | );
|
|---|
| 164 |
|
|---|
| 165 | if (
|
|---|
| 166 | typeof declarationId !== "string" &&
|
|---|
| 167 | dep.isAnonymousDefault &&
|
|---|
| 168 | (concatenationScope || used)
|
|---|
| 169 | ) {
|
|---|
| 170 | // Fix .name for anonymous default export function declarations
|
|---|
| 171 | // see test/test262-cases/test/language/module-code/instn-named-bndng-dflt-fun-anon.js cspell:disable-line
|
|---|
| 172 | runtimeRequirements.add(RuntimeGlobals.setAnonymousDefaultName);
|
|---|
| 173 | initFragments.push(
|
|---|
| 174 | new InitFragment(
|
|---|
| 175 | `${RuntimeGlobals.setAnonymousDefaultName}(${name});\n`,
|
|---|
| 176 | InitFragment.STAGE_HARMONY_EXPORTS,
|
|---|
| 177 | 2
|
|---|
| 178 | )
|
|---|
| 179 | );
|
|---|
| 180 | }
|
|---|
| 181 | } else {
|
|---|
| 182 | /** @type {string} */
|
|---|
| 183 | let content;
|
|---|
| 184 | let name = ConcatenationScope.DEFAULT_EXPORT;
|
|---|
| 185 | let defaultIsUsed = Boolean(concatenationScope);
|
|---|
| 186 | if (runtimeTemplate.supportsConst()) {
|
|---|
| 187 | content = `/* harmony default export */ const ${name} = `;
|
|---|
| 188 | if (concatenationScope) {
|
|---|
| 189 | concatenationScope.registerExport("default", name);
|
|---|
| 190 | } else {
|
|---|
| 191 | const used = moduleGraph
|
|---|
| 192 | .getExportsInfo(module)
|
|---|
| 193 | .getUsedName("default", runtime);
|
|---|
| 194 | if (used) {
|
|---|
| 195 | defaultIsUsed = true;
|
|---|
| 196 | runtimeRequirements.add(RuntimeGlobals.exports);
|
|---|
| 197 | /** @type {ExportMap} */
|
|---|
| 198 | const map = new Map();
|
|---|
| 199 | map.set(used, name);
|
|---|
| 200 | initFragments.push(new HarmonyExportInitFragment(exportsName, map));
|
|---|
| 201 | } else {
|
|---|
| 202 | content = `/* unused harmony default export */ var ${name} = `;
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | } else if (concatenationScope) {
|
|---|
| 206 | content = `/* harmony default export */ var ${name} = `;
|
|---|
| 207 | concatenationScope.registerExport("default", name);
|
|---|
| 208 | } else {
|
|---|
| 209 | const used = moduleGraph
|
|---|
| 210 | .getExportsInfo(module)
|
|---|
| 211 | .getUsedName("default", runtime);
|
|---|
| 212 | if (used) {
|
|---|
| 213 | defaultIsUsed = true;
|
|---|
| 214 | runtimeRequirements.add(RuntimeGlobals.exports);
|
|---|
| 215 | // This is a little bit incorrect as TDZ is not correct, but we can't use const.
|
|---|
| 216 | // No local `__WEBPACK_DEFAULT_EXPORT__` binding is created in this path,
|
|---|
| 217 | // so the anonymous-default `.name` fix-up below must reference the actual
|
|---|
| 218 | // assignment target instead. See issue #20793.
|
|---|
| 219 | name = `${exportsName}${propertyAccess(
|
|---|
| 220 | typeof used === "string" ? [used] : used
|
|---|
| 221 | )}`;
|
|---|
| 222 | content = `/* harmony default export */ ${name} = `;
|
|---|
| 223 | } else {
|
|---|
| 224 | content = `/* unused harmony default export */ var ${name} = `;
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | if (dep.range) {
|
|---|
| 229 | source.replace(
|
|---|
| 230 | dep.rangeStatement[0],
|
|---|
| 231 | dep.range[0] - 1,
|
|---|
| 232 | `${content}(${dep.prefix}`
|
|---|
| 233 | );
|
|---|
| 234 | if (dep.isAnonymousDefault && defaultIsUsed) {
|
|---|
| 235 | // Fix .name for anonymous default export expressions
|
|---|
| 236 | // see test/test262-cases/test/language/module-code/eval-export-dflt-cls-anon.js cspell:disable-line
|
|---|
| 237 | runtimeRequirements.add(RuntimeGlobals.setAnonymousDefaultName);
|
|---|
| 238 | source.replace(
|
|---|
| 239 | dep.range[1],
|
|---|
| 240 | dep.rangeStatement[1] - 0.5,
|
|---|
| 241 | `);\n${RuntimeGlobals.setAnonymousDefaultName}(${name});`
|
|---|
| 242 | );
|
|---|
| 243 | } else {
|
|---|
| 244 | source.replace(dep.range[1], dep.rangeStatement[1] - 0.5, ");");
|
|---|
| 245 | }
|
|---|
| 246 | return;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | source.replace(dep.rangeStatement[0], dep.rangeStatement[1] - 1, content);
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 | };
|
|---|
| 253 |
|
|---|
| 254 | module.exports = HarmonyExportExpressionDependency;
|
|---|