[79a0317] | 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 RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 10 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 11 | const propertyAccess = require("../util/propertyAccess");
|
---|
| 12 | const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
|
---|
| 13 | const NullDependency = require("./NullDependency");
|
---|
| 14 |
|
---|
| 15 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 16 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 17 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
---|
| 18 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 19 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 20 | /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
---|
| 21 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 23 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 24 |
|
---|
| 25 | class HarmonyExportExpressionDependency extends NullDependency {
|
---|
| 26 | /**
|
---|
| 27 | * @param {Range} range range
|
---|
| 28 | * @param {Range} rangeStatement range statement
|
---|
| 29 | * @param {string} prefix prefix
|
---|
| 30 | * @param {string | { range: Range, prefix: string, suffix: string }} [declarationId] declaration id
|
---|
| 31 | */
|
---|
| 32 | constructor(range, rangeStatement, prefix, declarationId) {
|
---|
| 33 | super();
|
---|
| 34 | this.range = range;
|
---|
| 35 | this.rangeStatement = rangeStatement;
|
---|
| 36 | this.prefix = prefix;
|
---|
| 37 | this.declarationId = declarationId;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | get type() {
|
---|
| 41 | return "harmony export expression";
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Returns the exported names
|
---|
| 46 | * @param {ModuleGraph} moduleGraph module graph
|
---|
| 47 | * @returns {ExportsSpec | undefined} export names
|
---|
| 48 | */
|
---|
| 49 | getExports(moduleGraph) {
|
---|
| 50 | return {
|
---|
| 51 | exports: ["default"],
|
---|
| 52 | priority: 1,
|
---|
| 53 | terminalBinding: true,
|
---|
| 54 | dependencies: undefined
|
---|
| 55 | };
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
| 60 | * @returns {ConnectionState} how this dependency connects the module to referencing modules
|
---|
| 61 | */
|
---|
| 62 | getModuleEvaluationSideEffectsState(moduleGraph) {
|
---|
| 63 | // The expression/declaration is already covered by SideEffectsFlagPlugin
|
---|
| 64 | return false;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * @param {ObjectSerializerContext} context context
|
---|
| 69 | */
|
---|
| 70 | serialize(context) {
|
---|
| 71 | const { write } = context;
|
---|
| 72 | write(this.range);
|
---|
| 73 | write(this.rangeStatement);
|
---|
| 74 | write(this.prefix);
|
---|
| 75 | write(this.declarationId);
|
---|
| 76 | super.serialize(context);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * @param {ObjectDeserializerContext} context context
|
---|
| 81 | */
|
---|
| 82 | deserialize(context) {
|
---|
| 83 | const { read } = context;
|
---|
| 84 | this.range = read();
|
---|
| 85 | this.rangeStatement = read();
|
---|
| 86 | this.prefix = read();
|
---|
| 87 | this.declarationId = read();
|
---|
| 88 | super.deserialize(context);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | makeSerializable(
|
---|
| 93 | HarmonyExportExpressionDependency,
|
---|
| 94 | "webpack/lib/dependencies/HarmonyExportExpressionDependency"
|
---|
| 95 | );
|
---|
| 96 |
|
---|
| 97 | HarmonyExportExpressionDependency.Template = class HarmonyExportDependencyTemplate extends (
|
---|
| 98 | NullDependency.Template
|
---|
| 99 | ) {
|
---|
| 100 | /**
|
---|
| 101 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 102 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 103 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 104 | * @returns {void}
|
---|
| 105 | */
|
---|
| 106 | apply(
|
---|
| 107 | dependency,
|
---|
| 108 | source,
|
---|
| 109 | {
|
---|
| 110 | module,
|
---|
| 111 | moduleGraph,
|
---|
| 112 | runtimeTemplate,
|
---|
| 113 | runtimeRequirements,
|
---|
| 114 | initFragments,
|
---|
| 115 | runtime,
|
---|
| 116 | concatenationScope
|
---|
| 117 | }
|
---|
| 118 | ) {
|
---|
| 119 | const dep = /** @type {HarmonyExportExpressionDependency} */ (dependency);
|
---|
| 120 | const { declarationId } = dep;
|
---|
| 121 | const exportsName = module.exportsArgument;
|
---|
| 122 | if (declarationId) {
|
---|
| 123 | let name;
|
---|
| 124 | if (typeof declarationId === "string") {
|
---|
| 125 | name = declarationId;
|
---|
| 126 | } else {
|
---|
| 127 | name = ConcatenationScope.DEFAULT_EXPORT;
|
---|
| 128 | source.replace(
|
---|
| 129 | declarationId.range[0],
|
---|
| 130 | declarationId.range[1] - 1,
|
---|
| 131 | `${declarationId.prefix}${name}${declarationId.suffix}`
|
---|
| 132 | );
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | if (concatenationScope) {
|
---|
| 136 | concatenationScope.registerExport("default", name);
|
---|
| 137 | } else {
|
---|
| 138 | const used = moduleGraph
|
---|
| 139 | .getExportsInfo(module)
|
---|
| 140 | .getUsedName("default", runtime);
|
---|
| 141 | if (used) {
|
---|
| 142 | const map = new Map();
|
---|
| 143 | map.set(used, `/* export default binding */ ${name}`);
|
---|
| 144 | initFragments.push(new HarmonyExportInitFragment(exportsName, map));
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | source.replace(
|
---|
| 149 | dep.rangeStatement[0],
|
---|
| 150 | dep.range[0] - 1,
|
---|
| 151 | `/* harmony default export */ ${dep.prefix}`
|
---|
| 152 | );
|
---|
| 153 | } else {
|
---|
| 154 | /** @type {string} */
|
---|
| 155 | let content;
|
---|
| 156 | const name = ConcatenationScope.DEFAULT_EXPORT;
|
---|
| 157 | if (runtimeTemplate.supportsConst()) {
|
---|
| 158 | content = `/* harmony default export */ const ${name} = `;
|
---|
| 159 | if (concatenationScope) {
|
---|
| 160 | concatenationScope.registerExport("default", name);
|
---|
| 161 | } else {
|
---|
| 162 | const used = moduleGraph
|
---|
| 163 | .getExportsInfo(module)
|
---|
| 164 | .getUsedName("default", runtime);
|
---|
| 165 | if (used) {
|
---|
| 166 | runtimeRequirements.add(RuntimeGlobals.exports);
|
---|
| 167 | const map = new Map();
|
---|
| 168 | map.set(used, name);
|
---|
| 169 | initFragments.push(new HarmonyExportInitFragment(exportsName, map));
|
---|
| 170 | } else {
|
---|
| 171 | content = `/* unused harmony default export */ var ${name} = `;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | } else if (concatenationScope) {
|
---|
| 175 | content = `/* harmony default export */ var ${name} = `;
|
---|
| 176 | concatenationScope.registerExport("default", name);
|
---|
| 177 | } else {
|
---|
| 178 | const used = moduleGraph
|
---|
| 179 | .getExportsInfo(module)
|
---|
| 180 | .getUsedName("default", runtime);
|
---|
| 181 | if (used) {
|
---|
| 182 | runtimeRequirements.add(RuntimeGlobals.exports);
|
---|
| 183 | // This is a little bit incorrect as TDZ is not correct, but we can't use const.
|
---|
| 184 | content = `/* harmony default export */ ${exportsName}${propertyAccess(
|
---|
| 185 | typeof used === "string" ? [used] : used
|
---|
| 186 | )} = `;
|
---|
| 187 | } else {
|
---|
| 188 | content = `/* unused harmony default export */ var ${name} = `;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | if (dep.range) {
|
---|
| 193 | source.replace(
|
---|
| 194 | dep.rangeStatement[0],
|
---|
| 195 | dep.range[0] - 1,
|
---|
| 196 | `${content}(${dep.prefix}`
|
---|
| 197 | );
|
---|
| 198 | source.replace(dep.range[1], dep.rangeStatement[1] - 0.5, ");");
|
---|
| 199 | return;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | source.replace(dep.rangeStatement[0], dep.rangeStatement[1] - 1, content);
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | };
|
---|
| 206 |
|
---|
| 207 | module.exports = HarmonyExportExpressionDependency;
|
---|