| 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("./LocalModule")} LocalModule */
|
|---|
| 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 | /** @type {Record<string, { definition: string, content: string, requests: string[] }>} */
|
|---|
| 21 | const DEFINITIONS = {
|
|---|
| 22 | f: {
|
|---|
| 23 | definition: "var __WEBPACK_AMD_DEFINE_RESULT__;",
|
|---|
| 24 | content: `!(__WEBPACK_AMD_DEFINE_RESULT__ = (#).call(exports, ${RuntimeGlobals.require}, exports, module),
|
|---|
| 25 | __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`,
|
|---|
| 26 | requests: [
|
|---|
| 27 | RuntimeGlobals.require,
|
|---|
| 28 | RuntimeGlobals.exports,
|
|---|
| 29 | RuntimeGlobals.module
|
|---|
| 30 | ]
|
|---|
| 31 | },
|
|---|
| 32 | o: {
|
|---|
| 33 | definition: "",
|
|---|
| 34 | content: "!(module.exports = #)",
|
|---|
| 35 | requests: [RuntimeGlobals.module]
|
|---|
| 36 | },
|
|---|
| 37 | of: {
|
|---|
| 38 | definition:
|
|---|
| 39 | "var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_RESULT__;",
|
|---|
| 40 | content: `!(__WEBPACK_AMD_DEFINE_FACTORY__ = (#),
|
|---|
| 41 | __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
|
|---|
| 42 | (__WEBPACK_AMD_DEFINE_FACTORY__.call(exports, ${RuntimeGlobals.require}, exports, module)) :
|
|---|
| 43 | __WEBPACK_AMD_DEFINE_FACTORY__),
|
|---|
| 44 | __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`,
|
|---|
| 45 | requests: [
|
|---|
| 46 | RuntimeGlobals.require,
|
|---|
| 47 | RuntimeGlobals.exports,
|
|---|
| 48 | RuntimeGlobals.module
|
|---|
| 49 | ]
|
|---|
| 50 | },
|
|---|
| 51 | af: {
|
|---|
| 52 | definition:
|
|---|
| 53 | "var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;",
|
|---|
| 54 | content: `!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = (#).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
|
|---|
| 55 | __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`,
|
|---|
| 56 | requests: [RuntimeGlobals.exports, RuntimeGlobals.module]
|
|---|
| 57 | },
|
|---|
| 58 | ao: {
|
|---|
| 59 | definition: "",
|
|---|
| 60 | content: "!(#, module.exports = #)",
|
|---|
| 61 | requests: [RuntimeGlobals.module]
|
|---|
| 62 | },
|
|---|
| 63 | aof: {
|
|---|
| 64 | definition:
|
|---|
| 65 | "var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;",
|
|---|
| 66 | content: `!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_FACTORY__ = (#),
|
|---|
| 67 | __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
|
|---|
| 68 | (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
|
|---|
| 69 | __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))`,
|
|---|
| 70 | requests: [RuntimeGlobals.exports, RuntimeGlobals.module]
|
|---|
| 71 | },
|
|---|
| 72 | lf: {
|
|---|
| 73 | definition: "var XXX, XXXmodule;",
|
|---|
| 74 | content: `!(XXXmodule = { id: YYY, exports: {}, loaded: false }, XXX = (#).call(XXXmodule.exports, ${RuntimeGlobals.require}, XXXmodule.exports, XXXmodule), XXXmodule.loaded = true, XXX === undefined && (XXX = XXXmodule.exports))`,
|
|---|
| 75 | requests: [RuntimeGlobals.require, RuntimeGlobals.module]
|
|---|
| 76 | },
|
|---|
| 77 | lo: {
|
|---|
| 78 | definition: "var XXX;",
|
|---|
| 79 | content: "!(XXX = #)",
|
|---|
| 80 | requests: []
|
|---|
| 81 | },
|
|---|
| 82 | lof: {
|
|---|
| 83 | definition: "var XXX, XXXfactory, XXXmodule;",
|
|---|
| 84 | content: `!(XXXfactory = (#), (typeof XXXfactory === 'function' ? ((XXXmodule = { id: YYY, exports: {}, loaded: false }), (XXX = XXXfactory.call(XXXmodule.exports, ${RuntimeGlobals.require}, XXXmodule.exports, XXXmodule)), (XXXmodule.loaded = true), XXX === undefined && (XXX = XXXmodule.exports)) : XXX = XXXfactory))`,
|
|---|
| 85 | requests: [RuntimeGlobals.require, RuntimeGlobals.module]
|
|---|
| 86 | },
|
|---|
| 87 | laf: {
|
|---|
| 88 | definition: "var __WEBPACK_AMD_DEFINE_ARRAY__, XXX, XXXexports;",
|
|---|
| 89 | content:
|
|---|
| 90 | "!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, XXX = (#).apply(XXXexports = {}, __WEBPACK_AMD_DEFINE_ARRAY__), XXX === undefined && (XXX = XXXexports))",
|
|---|
| 91 | requests: []
|
|---|
| 92 | },
|
|---|
| 93 | lao: {
|
|---|
| 94 | definition: "var XXX;",
|
|---|
| 95 | content: "!(#, XXX = #)",
|
|---|
| 96 | requests: []
|
|---|
| 97 | },
|
|---|
| 98 | laof: {
|
|---|
| 99 | definition: "var XXXarray, XXXfactory, XXXexports, XXX;",
|
|---|
| 100 | content: `!(XXXarray = #, XXXfactory = (#),
|
|---|
| 101 | (typeof XXXfactory === 'function' ?
|
|---|
| 102 | ((XXX = XXXfactory.apply(XXXexports = {}, XXXarray)), XXX === undefined && (XXX = XXXexports)) :
|
|---|
| 103 | (XXX = XXXfactory)
|
|---|
| 104 | ))`,
|
|---|
| 105 | requests: []
|
|---|
| 106 | }
|
|---|
| 107 | };
|
|---|
| 108 |
|
|---|
| 109 | class AMDDefineDependency extends NullDependency {
|
|---|
| 110 | /**
|
|---|
| 111 | * Creates an instance of AMDDefineDependency.
|
|---|
| 112 | * @param {Range} range range
|
|---|
| 113 | * @param {Range | null} arrayRange array range
|
|---|
| 114 | * @param {Range | null} functionRange function range
|
|---|
| 115 | * @param {Range | null} objectRange object range
|
|---|
| 116 | * @param {string | null} namedModule true, when define is called with a name
|
|---|
| 117 | */
|
|---|
| 118 | constructor(range, arrayRange, functionRange, objectRange, namedModule) {
|
|---|
| 119 | super();
|
|---|
| 120 | this.range = range;
|
|---|
| 121 | this.arrayRange = arrayRange;
|
|---|
| 122 | this.functionRange = functionRange;
|
|---|
| 123 | this.objectRange = objectRange;
|
|---|
| 124 | this.namedModule = namedModule;
|
|---|
| 125 | /** @type {LocalModule | null} */
|
|---|
| 126 | this.localModule = null;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | get type() {
|
|---|
| 130 | return "amd define";
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Serializes this instance into the provided serializer context.
|
|---|
| 135 | * @param {ObjectSerializerContext} context context
|
|---|
| 136 | */
|
|---|
| 137 | serialize(context) {
|
|---|
| 138 | const { write } = context;
|
|---|
| 139 | write(this.range);
|
|---|
| 140 | write(this.arrayRange);
|
|---|
| 141 | write(this.functionRange);
|
|---|
| 142 | write(this.objectRange);
|
|---|
| 143 | write(this.namedModule);
|
|---|
| 144 | write(this.localModule);
|
|---|
| 145 | super.serialize(context);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Restores this instance from the provided deserializer context.
|
|---|
| 150 | * @param {ObjectDeserializerContext} context context
|
|---|
| 151 | */
|
|---|
| 152 | deserialize(context) {
|
|---|
| 153 | const { read } = context;
|
|---|
| 154 | this.range = read();
|
|---|
| 155 | this.arrayRange = read();
|
|---|
| 156 | this.functionRange = read();
|
|---|
| 157 | this.objectRange = read();
|
|---|
| 158 | this.namedModule = read();
|
|---|
| 159 | this.localModule = read();
|
|---|
| 160 | super.deserialize(context);
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | makeSerializable(
|
|---|
| 165 | AMDDefineDependency,
|
|---|
| 166 | "webpack/lib/dependencies/AMDDefineDependency"
|
|---|
| 167 | );
|
|---|
| 168 |
|
|---|
| 169 | AMDDefineDependency.Template = class AMDDefineDependencyTemplate extends (
|
|---|
| 170 | NullDependency.Template
|
|---|
| 171 | ) {
|
|---|
| 172 | /**
|
|---|
| 173 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 174 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 175 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 176 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 177 | * @returns {void}
|
|---|
| 178 | */
|
|---|
| 179 | apply(dependency, source, { runtimeRequirements }) {
|
|---|
| 180 | const dep = /** @type {AMDDefineDependency} */ (dependency);
|
|---|
| 181 | const branch = this.branch(dep);
|
|---|
| 182 | const { definition, content, requests } = DEFINITIONS[branch];
|
|---|
| 183 | for (const req of requests) {
|
|---|
| 184 | runtimeRequirements.add(req);
|
|---|
| 185 | }
|
|---|
| 186 | this.replace(dep, source, definition, content);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /**
|
|---|
| 190 | * Returns variable name.
|
|---|
| 191 | * @param {AMDDefineDependency} dependency dependency
|
|---|
| 192 | * @returns {string | false | null} variable name
|
|---|
| 193 | */
|
|---|
| 194 | localModuleVar(dependency) {
|
|---|
| 195 | return (
|
|---|
| 196 | dependency.localModule &&
|
|---|
| 197 | dependency.localModule.used &&
|
|---|
| 198 | dependency.localModule.variableName()
|
|---|
| 199 | );
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * Returns branch.
|
|---|
| 204 | * @param {AMDDefineDependency} dependency dependency
|
|---|
| 205 | * @returns {string} branch
|
|---|
| 206 | */
|
|---|
| 207 | branch(dependency) {
|
|---|
| 208 | const localModuleVar = this.localModuleVar(dependency) ? "l" : "";
|
|---|
| 209 | const arrayRange = dependency.arrayRange ? "a" : "";
|
|---|
| 210 | const objectRange = dependency.objectRange ? "o" : "";
|
|---|
| 211 | const functionRange = dependency.functionRange ? "f" : "";
|
|---|
| 212 | return localModuleVar + arrayRange + objectRange + functionRange;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /**
|
|---|
| 216 | * Processes the provided dependency.
|
|---|
| 217 | * @param {AMDDefineDependency} dependency dependency
|
|---|
| 218 | * @param {ReplaceSource} source source
|
|---|
| 219 | * @param {string} definition definition
|
|---|
| 220 | * @param {string} text text
|
|---|
| 221 | */
|
|---|
| 222 | replace(dependency, source, definition, text) {
|
|---|
| 223 | const localModuleVar = this.localModuleVar(dependency);
|
|---|
| 224 | if (localModuleVar) {
|
|---|
| 225 | text = text.replace(/XXX/g, localModuleVar.replace(/\$/g, "$$$$"));
|
|---|
| 226 | definition = definition.replace(
|
|---|
| 227 | /XXX/g,
|
|---|
| 228 | localModuleVar.replace(/\$/g, "$$$$")
|
|---|
| 229 | );
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | if (dependency.namedModule) {
|
|---|
| 233 | text = text.replace(/YYY/g, JSON.stringify(dependency.namedModule));
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | const texts = text.split("#");
|
|---|
| 237 |
|
|---|
| 238 | if (definition) source.insert(0, definition);
|
|---|
| 239 |
|
|---|
| 240 | let current = dependency.range[0];
|
|---|
| 241 | if (dependency.arrayRange) {
|
|---|
| 242 | source.replace(
|
|---|
| 243 | current,
|
|---|
| 244 | dependency.arrayRange[0] - 1,
|
|---|
| 245 | /** @type {string} */ (texts.shift())
|
|---|
| 246 | );
|
|---|
| 247 | current = dependency.arrayRange[1];
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | if (dependency.objectRange) {
|
|---|
| 251 | source.replace(
|
|---|
| 252 | current,
|
|---|
| 253 | dependency.objectRange[0] - 1,
|
|---|
| 254 | /** @type {string} */ (texts.shift())
|
|---|
| 255 | );
|
|---|
| 256 | current = dependency.objectRange[1];
|
|---|
| 257 | } else if (dependency.functionRange) {
|
|---|
| 258 | source.replace(
|
|---|
| 259 | current,
|
|---|
| 260 | dependency.functionRange[0] - 1,
|
|---|
| 261 | /** @type {string} */ (texts.shift())
|
|---|
| 262 | );
|
|---|
| 263 | current = dependency.functionRange[1];
|
|---|
| 264 | }
|
|---|
| 265 | source.replace(
|
|---|
| 266 | current,
|
|---|
| 267 | dependency.range[1] - 1,
|
|---|
| 268 | /** @type {string} */ (texts.shift())
|
|---|
| 269 | );
|
|---|
| 270 | if (texts.length > 0) throw new Error("Implementation error");
|
|---|
| 271 | }
|
|---|
| 272 | };
|
|---|
| 273 |
|
|---|
| 274 | module.exports = AMDDefineDependency;
|
|---|