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 makeSerializable = require("../util/makeSerializable");
|
---|
9 | const NullDependency = require("./NullDependency");
|
---|
10 |
|
---|
11 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
12 | /** @typedef {import("../Dependency")} Dependency */
|
---|
13 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
14 |
|
---|
15 | class LocalModuleDependency extends NullDependency {
|
---|
16 | constructor(localModule, range, callNew) {
|
---|
17 | super();
|
---|
18 |
|
---|
19 | this.localModule = localModule;
|
---|
20 | this.range = range;
|
---|
21 | this.callNew = callNew;
|
---|
22 | }
|
---|
23 |
|
---|
24 | serialize(context) {
|
---|
25 | const { write } = context;
|
---|
26 |
|
---|
27 | write(this.localModule);
|
---|
28 | write(this.range);
|
---|
29 | write(this.callNew);
|
---|
30 |
|
---|
31 | super.serialize(context);
|
---|
32 | }
|
---|
33 |
|
---|
34 | deserialize(context) {
|
---|
35 | const { read } = context;
|
---|
36 |
|
---|
37 | this.localModule = read();
|
---|
38 | this.range = read();
|
---|
39 | this.callNew = read();
|
---|
40 |
|
---|
41 | super.deserialize(context);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | makeSerializable(
|
---|
46 | LocalModuleDependency,
|
---|
47 | "webpack/lib/dependencies/LocalModuleDependency"
|
---|
48 | );
|
---|
49 |
|
---|
50 | LocalModuleDependency.Template = class LocalModuleDependencyTemplate extends (
|
---|
51 | NullDependency.Template
|
---|
52 | ) {
|
---|
53 | /**
|
---|
54 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
55 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
56 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
57 | * @returns {void}
|
---|
58 | */
|
---|
59 | apply(dependency, source, templateContext) {
|
---|
60 | const dep = /** @type {LocalModuleDependency} */ (dependency);
|
---|
61 | if (!dep.range) return;
|
---|
62 | const moduleInstance = dep.callNew
|
---|
63 | ? `new (function () { return ${dep.localModule.variableName()}; })()`
|
---|
64 | : dep.localModule.variableName();
|
---|
65 | source.replace(dep.range[0], dep.range[1] - 1, moduleInstance);
|
---|
66 | }
|
---|
67 | };
|
---|
68 |
|
---|
69 | module.exports = LocalModuleDependency;
|
---|