[79a0317] | 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 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
| 16 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 17 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 18 | /** @typedef {import("./AMDRequireItemDependency")} AMDRequireItemDependency */
|
---|
| 19 | /** @typedef {import("./LocalModuleDependency")} LocalModuleDependency */
|
---|
| 20 |
|
---|
| 21 | class AMDRequireArrayDependency extends NullDependency {
|
---|
| 22 | /**
|
---|
| 23 | * @param {(string | LocalModuleDependency | AMDRequireItemDependency)[]} depsArray deps array
|
---|
| 24 | * @param {Range} range range
|
---|
| 25 | */
|
---|
| 26 | constructor(depsArray, range) {
|
---|
| 27 | super();
|
---|
| 28 |
|
---|
| 29 | this.depsArray = depsArray;
|
---|
| 30 | this.range = range;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | get type() {
|
---|
| 34 | return "amd require array";
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | get category() {
|
---|
| 38 | return "amd";
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @param {ObjectSerializerContext} context context
|
---|
| 43 | */
|
---|
| 44 | serialize(context) {
|
---|
| 45 | const { write } = context;
|
---|
| 46 |
|
---|
| 47 | write(this.depsArray);
|
---|
| 48 | write(this.range);
|
---|
| 49 |
|
---|
| 50 | super.serialize(context);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * @param {ObjectDeserializerContext} context context
|
---|
| 55 | */
|
---|
| 56 | deserialize(context) {
|
---|
| 57 | const { read } = context;
|
---|
| 58 |
|
---|
| 59 | this.depsArray = read();
|
---|
| 60 | this.range = read();
|
---|
| 61 |
|
---|
| 62 | super.deserialize(context);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | makeSerializable(
|
---|
| 67 | AMDRequireArrayDependency,
|
---|
| 68 | "webpack/lib/dependencies/AMDRequireArrayDependency"
|
---|
| 69 | );
|
---|
| 70 |
|
---|
| 71 | AMDRequireArrayDependency.Template = class AMDRequireArrayDependencyTemplate extends (
|
---|
| 72 | DependencyTemplate
|
---|
| 73 | ) {
|
---|
| 74 | /**
|
---|
| 75 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 76 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 77 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 78 | * @returns {void}
|
---|
| 79 | */
|
---|
| 80 | apply(dependency, source, templateContext) {
|
---|
| 81 | const dep = /** @type {AMDRequireArrayDependency} */ (dependency);
|
---|
| 82 | const content = this.getContent(dep, templateContext);
|
---|
| 83 | source.replace(dep.range[0], dep.range[1] - 1, content);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * @param {AMDRequireArrayDependency} dep the dependency for which the template should be applied
|
---|
| 88 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 89 | * @returns {string} content
|
---|
| 90 | */
|
---|
| 91 | getContent(dep, templateContext) {
|
---|
| 92 | const requires = dep.depsArray.map(dependency =>
|
---|
| 93 | this.contentForDependency(dependency, templateContext)
|
---|
| 94 | );
|
---|
| 95 | return `[${requires.join(", ")}]`;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /**
|
---|
| 99 | * @param {TODO} dep the dependency for which the template should be applied
|
---|
| 100 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 101 | * @returns {string} content
|
---|
| 102 | */
|
---|
| 103 | contentForDependency(
|
---|
| 104 | dep,
|
---|
| 105 | { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
|
---|
| 106 | ) {
|
---|
| 107 | if (typeof dep === "string") {
|
---|
| 108 | return dep;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (dep.localModule) {
|
---|
| 112 | return dep.localModule.variableName();
|
---|
| 113 | }
|
---|
| 114 | return runtimeTemplate.moduleExports({
|
---|
| 115 | module: moduleGraph.getModule(dep),
|
---|
| 116 | chunkGraph,
|
---|
| 117 | request: dep.request,
|
---|
| 118 | runtimeRequirements
|
---|
| 119 | });
|
---|
| 120 | }
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | module.exports = AMDRequireArrayDependency;
|
---|