[6a3a178] | 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 DependencyTemplate = require("../DependencyTemplate");
|
---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 10 | const NullDependency = require("./NullDependency");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 13 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 14 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 15 |
|
---|
| 16 | class AMDRequireArrayDependency extends NullDependency {
|
---|
| 17 | constructor(depsArray, range) {
|
---|
| 18 | super();
|
---|
| 19 |
|
---|
| 20 | this.depsArray = depsArray;
|
---|
| 21 | this.range = range;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | get type() {
|
---|
| 25 | return "amd require array";
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | get category() {
|
---|
| 29 | return "amd";
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | serialize(context) {
|
---|
| 33 | const { write } = context;
|
---|
| 34 |
|
---|
| 35 | write(this.depsArray);
|
---|
| 36 | write(this.range);
|
---|
| 37 |
|
---|
| 38 | super.serialize(context);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | deserialize(context) {
|
---|
| 42 | const { read } = context;
|
---|
| 43 |
|
---|
| 44 | this.depsArray = read();
|
---|
| 45 | this.range = read();
|
---|
| 46 |
|
---|
| 47 | super.deserialize(context);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | makeSerializable(
|
---|
| 52 | AMDRequireArrayDependency,
|
---|
| 53 | "webpack/lib/dependencies/AMDRequireArrayDependency"
|
---|
| 54 | );
|
---|
| 55 |
|
---|
| 56 | AMDRequireArrayDependency.Template = class AMDRequireArrayDependencyTemplate extends (
|
---|
| 57 | DependencyTemplate
|
---|
| 58 | ) {
|
---|
| 59 | /**
|
---|
| 60 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 61 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 62 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 63 | * @returns {void}
|
---|
| 64 | */
|
---|
| 65 | apply(dependency, source, templateContext) {
|
---|
| 66 | const dep = /** @type {AMDRequireArrayDependency} */ (dependency);
|
---|
| 67 | const content = this.getContent(dep, templateContext);
|
---|
| 68 | source.replace(dep.range[0], dep.range[1] - 1, content);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | getContent(dep, templateContext) {
|
---|
| 72 | const requires = dep.depsArray.map(dependency => {
|
---|
| 73 | return this.contentForDependency(dependency, templateContext);
|
---|
| 74 | });
|
---|
| 75 | return `[${requires.join(", ")}]`;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | contentForDependency(
|
---|
| 79 | dep,
|
---|
| 80 | { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
|
---|
| 81 | ) {
|
---|
| 82 | if (typeof dep === "string") {
|
---|
| 83 | return dep;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | if (dep.localModule) {
|
---|
| 87 | return dep.localModule.variableName();
|
---|
| 88 | } else {
|
---|
| 89 | return runtimeTemplate.moduleExports({
|
---|
| 90 | module: moduleGraph.getModule(dep),
|
---|
| 91 | chunkGraph,
|
---|
| 92 | request: dep.request,
|
---|
| 93 | runtimeRequirements
|
---|
| 94 | });
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 | module.exports = AMDRequireArrayDependency;
|
---|