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 ContextDependency = require("./ContextDependency");
|
---|
9 |
|
---|
10 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
11 | /** @typedef {import("../Dependency")} Dependency */
|
---|
12 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
13 |
|
---|
14 | class ContextDependencyTemplateAsRequireCall extends ContextDependency.Template {
|
---|
15 | /**
|
---|
16 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
17 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
18 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
19 | * @returns {void}
|
---|
20 | */
|
---|
21 | apply(
|
---|
22 | dependency,
|
---|
23 | source,
|
---|
24 | { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
|
---|
25 | ) {
|
---|
26 | const dep = /** @type {ContextDependency} */ (dependency);
|
---|
27 | const moduleExports = runtimeTemplate.moduleExports({
|
---|
28 | module: moduleGraph.getModule(dep),
|
---|
29 | chunkGraph,
|
---|
30 | request: dep.request,
|
---|
31 | runtimeRequirements
|
---|
32 | });
|
---|
33 |
|
---|
34 | if (moduleGraph.getModule(dep)) {
|
---|
35 | if (dep.valueRange) {
|
---|
36 | if (Array.isArray(dep.replaces)) {
|
---|
37 | for (let i = 0; i < dep.replaces.length; i++) {
|
---|
38 | const rep = dep.replaces[i];
|
---|
39 | source.replace(rep.range[0], rep.range[1] - 1, rep.value);
|
---|
40 | }
|
---|
41 | }
|
---|
42 | source.replace(dep.valueRange[1], dep.range[1] - 1, ")");
|
---|
43 | source.replace(
|
---|
44 | dep.range[0],
|
---|
45 | dep.valueRange[0] - 1,
|
---|
46 | `${moduleExports}(`
|
---|
47 | );
|
---|
48 | } else {
|
---|
49 | source.replace(dep.range[0], dep.range[1] - 1, moduleExports);
|
---|
50 | }
|
---|
51 | } else {
|
---|
52 | source.replace(dep.range[0], dep.range[1] - 1, moduleExports);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | module.exports = ContextDependencyTemplateAsRequireCall;
|
---|