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 RuntimeGlobals = require("../RuntimeGlobals");
|
---|
9 | const makeSerializable = require("../util/makeSerializable");
|
---|
10 | const NullDependency = require("./NullDependency");
|
---|
11 |
|
---|
12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
13 | /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
---|
14 | /** @typedef {import("../Dependency")} Dependency */
|
---|
15 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
16 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
17 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
19 |
|
---|
20 | class AMDRequireDependency extends NullDependency {
|
---|
21 | /**
|
---|
22 | * @param {Range} outerRange outer range
|
---|
23 | * @param {Range} arrayRange array range
|
---|
24 | * @param {Range | null} functionRange function range
|
---|
25 | * @param {Range | null} errorCallbackRange error callback range
|
---|
26 | */
|
---|
27 | constructor(outerRange, arrayRange, functionRange, errorCallbackRange) {
|
---|
28 | super();
|
---|
29 |
|
---|
30 | this.outerRange = outerRange;
|
---|
31 | this.arrayRange = arrayRange;
|
---|
32 | this.functionRange = functionRange;
|
---|
33 | this.errorCallbackRange = errorCallbackRange;
|
---|
34 | this.functionBindThis = false;
|
---|
35 | this.errorCallbackBindThis = false;
|
---|
36 | }
|
---|
37 |
|
---|
38 | get category() {
|
---|
39 | return "amd";
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @param {ObjectSerializerContext} context context
|
---|
44 | */
|
---|
45 | serialize(context) {
|
---|
46 | const { write } = context;
|
---|
47 |
|
---|
48 | write(this.outerRange);
|
---|
49 | write(this.arrayRange);
|
---|
50 | write(this.functionRange);
|
---|
51 | write(this.errorCallbackRange);
|
---|
52 | write(this.functionBindThis);
|
---|
53 | write(this.errorCallbackBindThis);
|
---|
54 |
|
---|
55 | super.serialize(context);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * @param {ObjectDeserializerContext} context context
|
---|
60 | */
|
---|
61 | deserialize(context) {
|
---|
62 | const { read } = context;
|
---|
63 |
|
---|
64 | this.outerRange = read();
|
---|
65 | this.arrayRange = read();
|
---|
66 | this.functionRange = read();
|
---|
67 | this.errorCallbackRange = read();
|
---|
68 | this.functionBindThis = read();
|
---|
69 | this.errorCallbackBindThis = read();
|
---|
70 |
|
---|
71 | super.deserialize(context);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | makeSerializable(
|
---|
76 | AMDRequireDependency,
|
---|
77 | "webpack/lib/dependencies/AMDRequireDependency"
|
---|
78 | );
|
---|
79 |
|
---|
80 | AMDRequireDependency.Template = class AMDRequireDependencyTemplate extends (
|
---|
81 | NullDependency.Template
|
---|
82 | ) {
|
---|
83 | /**
|
---|
84 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
85 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
86 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
87 | * @returns {void}
|
---|
88 | */
|
---|
89 | apply(
|
---|
90 | dependency,
|
---|
91 | source,
|
---|
92 | { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
|
---|
93 | ) {
|
---|
94 | const dep = /** @type {AMDRequireDependency} */ (dependency);
|
---|
95 | const depBlock = /** @type {AsyncDependenciesBlock} */ (
|
---|
96 | moduleGraph.getParentBlock(dep)
|
---|
97 | );
|
---|
98 | const promise = runtimeTemplate.blockPromise({
|
---|
99 | chunkGraph,
|
---|
100 | block: depBlock,
|
---|
101 | message: "AMD require",
|
---|
102 | runtimeRequirements
|
---|
103 | });
|
---|
104 |
|
---|
105 | // has array range but no function range
|
---|
106 | if (dep.arrayRange && !dep.functionRange) {
|
---|
107 | const startBlock = `${promise}.then(function() {`;
|
---|
108 | const endBlock = `;})['catch'](${RuntimeGlobals.uncaughtErrorHandler})`;
|
---|
109 | runtimeRequirements.add(RuntimeGlobals.uncaughtErrorHandler);
|
---|
110 |
|
---|
111 | source.replace(dep.outerRange[0], dep.arrayRange[0] - 1, startBlock);
|
---|
112 |
|
---|
113 | source.replace(dep.arrayRange[1], dep.outerRange[1] - 1, endBlock);
|
---|
114 |
|
---|
115 | return;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // has function range but no array range
|
---|
119 | if (dep.functionRange && !dep.arrayRange) {
|
---|
120 | const startBlock = `${promise}.then((`;
|
---|
121 | const endBlock = `).bind(exports, ${RuntimeGlobals.require}, exports, module))['catch'](${RuntimeGlobals.uncaughtErrorHandler})`;
|
---|
122 | runtimeRequirements.add(RuntimeGlobals.uncaughtErrorHandler);
|
---|
123 |
|
---|
124 | source.replace(dep.outerRange[0], dep.functionRange[0] - 1, startBlock);
|
---|
125 |
|
---|
126 | source.replace(dep.functionRange[1], dep.outerRange[1] - 1, endBlock);
|
---|
127 |
|
---|
128 | return;
|
---|
129 | }
|
---|
130 |
|
---|
131 | // has array range, function range, and errorCallbackRange
|
---|
132 | if (dep.arrayRange && dep.functionRange && dep.errorCallbackRange) {
|
---|
133 | const startBlock = `${promise}.then(function() { `;
|
---|
134 | const errorRangeBlock = `}${
|
---|
135 | dep.functionBindThis ? ".bind(this)" : ""
|
---|
136 | })['catch'](`;
|
---|
137 | const endBlock = `${dep.errorCallbackBindThis ? ".bind(this)" : ""})`;
|
---|
138 |
|
---|
139 | source.replace(dep.outerRange[0], dep.arrayRange[0] - 1, startBlock);
|
---|
140 |
|
---|
141 | source.insert(dep.arrayRange[0], "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
|
---|
142 |
|
---|
143 | source.replace(dep.arrayRange[1], dep.functionRange[0] - 1, "; (");
|
---|
144 |
|
---|
145 | source.insert(
|
---|
146 | dep.functionRange[1],
|
---|
147 | ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__);"
|
---|
148 | );
|
---|
149 |
|
---|
150 | source.replace(
|
---|
151 | dep.functionRange[1],
|
---|
152 | dep.errorCallbackRange[0] - 1,
|
---|
153 | errorRangeBlock
|
---|
154 | );
|
---|
155 |
|
---|
156 | source.replace(
|
---|
157 | dep.errorCallbackRange[1],
|
---|
158 | dep.outerRange[1] - 1,
|
---|
159 | endBlock
|
---|
160 | );
|
---|
161 |
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | // has array range, function range, but no errorCallbackRange
|
---|
166 | if (dep.arrayRange && dep.functionRange) {
|
---|
167 | const startBlock = `${promise}.then(function() { `;
|
---|
168 | const endBlock = `}${
|
---|
169 | dep.functionBindThis ? ".bind(this)" : ""
|
---|
170 | })['catch'](${RuntimeGlobals.uncaughtErrorHandler})`;
|
---|
171 | runtimeRequirements.add(RuntimeGlobals.uncaughtErrorHandler);
|
---|
172 |
|
---|
173 | source.replace(dep.outerRange[0], dep.arrayRange[0] - 1, startBlock);
|
---|
174 |
|
---|
175 | source.insert(dep.arrayRange[0], "var __WEBPACK_AMD_REQUIRE_ARRAY__ = ");
|
---|
176 |
|
---|
177 | source.replace(dep.arrayRange[1], dep.functionRange[0] - 1, "; (");
|
---|
178 |
|
---|
179 | source.insert(
|
---|
180 | dep.functionRange[1],
|
---|
181 | ").apply(null, __WEBPACK_AMD_REQUIRE_ARRAY__);"
|
---|
182 | );
|
---|
183 |
|
---|
184 | source.replace(dep.functionRange[1], dep.outerRange[1] - 1, endBlock);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | };
|
---|
188 |
|
---|
189 | module.exports = AMDRequireDependency;
|
---|