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