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 InitFragment = require("../InitFragment");
|
---|
9 | const makeSerializable = require("../util/makeSerializable");
|
---|
10 | const propertyAccess = require("../util/propertyAccess");
|
---|
11 | const { handleDependencyBase } = require("./CommonJsDependencyHelpers");
|
---|
12 | const NullDependency = require("./NullDependency");
|
---|
13 |
|
---|
14 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
15 | /** @typedef {import("../Dependency")} Dependency */
|
---|
16 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
---|
17 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
18 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
19 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
20 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
21 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
22 | /** @typedef {import("./CommonJsDependencyHelpers").CommonJSDependencyBaseKeywords} CommonJSDependencyBaseKeywords */
|
---|
23 |
|
---|
24 | const EMPTY_OBJECT = {};
|
---|
25 |
|
---|
26 | class CommonJsExportsDependency extends NullDependency {
|
---|
27 | /**
|
---|
28 | * @param {Range} range range
|
---|
29 | * @param {Range | null} valueRange value range
|
---|
30 | * @param {CommonJSDependencyBaseKeywords} base base
|
---|
31 | * @param {string[]} names names
|
---|
32 | */
|
---|
33 | constructor(range, valueRange, base, names) {
|
---|
34 | super();
|
---|
35 | this.range = range;
|
---|
36 | this.valueRange = valueRange;
|
---|
37 | this.base = base;
|
---|
38 | this.names = names;
|
---|
39 | }
|
---|
40 |
|
---|
41 | get type() {
|
---|
42 | return "cjs exports";
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Returns the exported names
|
---|
47 | * @param {ModuleGraph} moduleGraph module graph
|
---|
48 | * @returns {ExportsSpec | undefined} export names
|
---|
49 | */
|
---|
50 | getExports(moduleGraph) {
|
---|
51 | const name = this.names[0];
|
---|
52 | return {
|
---|
53 | exports: [
|
---|
54 | {
|
---|
55 | name,
|
---|
56 | // we can't mangle names that are in an empty object
|
---|
57 | // because one could access the prototype property
|
---|
58 | // when export isn't set yet
|
---|
59 | canMangle: !(name in EMPTY_OBJECT)
|
---|
60 | }
|
---|
61 | ],
|
---|
62 | dependencies: undefined
|
---|
63 | };
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @param {ObjectSerializerContext} context context
|
---|
68 | */
|
---|
69 | serialize(context) {
|
---|
70 | const { write } = context;
|
---|
71 | write(this.range);
|
---|
72 | write(this.valueRange);
|
---|
73 | write(this.base);
|
---|
74 | write(this.names);
|
---|
75 | super.serialize(context);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @param {ObjectDeserializerContext} context context
|
---|
80 | */
|
---|
81 | deserialize(context) {
|
---|
82 | const { read } = context;
|
---|
83 | this.range = read();
|
---|
84 | this.valueRange = read();
|
---|
85 | this.base = read();
|
---|
86 | this.names = read();
|
---|
87 | super.deserialize(context);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | makeSerializable(
|
---|
92 | CommonJsExportsDependency,
|
---|
93 | "webpack/lib/dependencies/CommonJsExportsDependency"
|
---|
94 | );
|
---|
95 |
|
---|
96 | CommonJsExportsDependency.Template = class CommonJsExportsDependencyTemplate extends (
|
---|
97 | NullDependency.Template
|
---|
98 | ) {
|
---|
99 | /**
|
---|
100 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
101 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
102 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
103 | * @returns {void}
|
---|
104 | */
|
---|
105 | apply(
|
---|
106 | dependency,
|
---|
107 | source,
|
---|
108 | { module, moduleGraph, initFragments, runtimeRequirements, runtime }
|
---|
109 | ) {
|
---|
110 | const dep = /** @type {CommonJsExportsDependency} */ (dependency);
|
---|
111 | const used = moduleGraph
|
---|
112 | .getExportsInfo(module)
|
---|
113 | .getUsedName(dep.names, runtime);
|
---|
114 |
|
---|
115 | const [type, base] = handleDependencyBase(
|
---|
116 | dep.base,
|
---|
117 | module,
|
---|
118 | runtimeRequirements
|
---|
119 | );
|
---|
120 |
|
---|
121 | switch (type) {
|
---|
122 | case "expression":
|
---|
123 | if (!used) {
|
---|
124 | initFragments.push(
|
---|
125 | new InitFragment(
|
---|
126 | "var __webpack_unused_export__;\n",
|
---|
127 | InitFragment.STAGE_CONSTANTS,
|
---|
128 | 0,
|
---|
129 | "__webpack_unused_export__"
|
---|
130 | )
|
---|
131 | );
|
---|
132 | source.replace(
|
---|
133 | dep.range[0],
|
---|
134 | dep.range[1] - 1,
|
---|
135 | "__webpack_unused_export__"
|
---|
136 | );
|
---|
137 | return;
|
---|
138 | }
|
---|
139 | source.replace(
|
---|
140 | dep.range[0],
|
---|
141 | dep.range[1] - 1,
|
---|
142 | `${base}${propertyAccess(used)}`
|
---|
143 | );
|
---|
144 | return;
|
---|
145 | case "Object.defineProperty":
|
---|
146 | if (!used) {
|
---|
147 | initFragments.push(
|
---|
148 | new InitFragment(
|
---|
149 | "var __webpack_unused_export__;\n",
|
---|
150 | InitFragment.STAGE_CONSTANTS,
|
---|
151 | 0,
|
---|
152 | "__webpack_unused_export__"
|
---|
153 | )
|
---|
154 | );
|
---|
155 | source.replace(
|
---|
156 | dep.range[0],
|
---|
157 | /** @type {Range} */ (dep.valueRange)[0] - 1,
|
---|
158 | "__webpack_unused_export__ = ("
|
---|
159 | );
|
---|
160 | source.replace(
|
---|
161 | /** @type {Range} */ (dep.valueRange)[1],
|
---|
162 | dep.range[1] - 1,
|
---|
163 | ")"
|
---|
164 | );
|
---|
165 | return;
|
---|
166 | }
|
---|
167 | source.replace(
|
---|
168 | dep.range[0],
|
---|
169 | /** @type {Range} */ (dep.valueRange)[0] - 1,
|
---|
170 | `Object.defineProperty(${base}${propertyAccess(
|
---|
171 | used.slice(0, -1)
|
---|
172 | )}, ${JSON.stringify(used[used.length - 1])}, (`
|
---|
173 | );
|
---|
174 | source.replace(
|
---|
175 | /** @type {Range} */ (dep.valueRange)[1],
|
---|
176 | dep.range[1] - 1,
|
---|
177 | "))"
|
---|
178 | );
|
---|
179 | }
|
---|
180 | }
|
---|
181 | };
|
---|
182 |
|
---|
183 | module.exports = CommonJsExportsDependency;
|
---|