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