1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const InitFragment = require("../InitFragment");
|
9 | const makeSerializable = require("../util/makeSerializable");
|
10 | const ModuleDependency = require("./ModuleDependency");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const pathToString = path =>
|
27 | path !== null && path.length > 0
|
28 | ? path.map(part => `[${JSON.stringify(part)}]`).join("")
|
29 | : "";
|
30 |
|
31 | class ProvidedDependency extends ModuleDependency {
|
32 | constructor(request, identifier, path, range) {
|
33 | super(request);
|
34 | this.identifier = identifier;
|
35 | this.path = path;
|
36 | this.range = range;
|
37 | this._hashUpdate = undefined;
|
38 | }
|
39 |
|
40 | get type() {
|
41 | return "provided";
|
42 | }
|
43 |
|
44 | get category() {
|
45 | return "esm";
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | updateHash(hash, context) {
|
55 | if (this._hashUpdate === undefined) {
|
56 | this._hashUpdate =
|
57 | this.identifier + (this.path ? this.path.join(",") : "null");
|
58 | }
|
59 | hash.update(this._hashUpdate);
|
60 | }
|
61 |
|
62 | serialize(context) {
|
63 | const { write } = context;
|
64 | write(this.identifier);
|
65 | write(this.path);
|
66 | super.serialize(context);
|
67 | }
|
68 |
|
69 | deserialize(context) {
|
70 | const { read } = context;
|
71 | this.identifier = read();
|
72 | this.path = read();
|
73 | super.deserialize(context);
|
74 | }
|
75 | }
|
76 |
|
77 | makeSerializable(
|
78 | ProvidedDependency,
|
79 | "webpack/lib/dependencies/ProvidedDependency"
|
80 | );
|
81 |
|
82 | class ProvidedDependencyTemplate extends ModuleDependency.Template {
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | apply(
|
90 | dependency,
|
91 | source,
|
92 | {
|
93 | runtimeTemplate,
|
94 | moduleGraph,
|
95 | chunkGraph,
|
96 | initFragments,
|
97 | runtimeRequirements
|
98 | }
|
99 | ) {
|
100 | const dep = (dependency);
|
101 | initFragments.push(
|
102 | new InitFragment(
|
103 | `/* provided dependency */ var ${
|
104 | dep.identifier
|
105 | } = ${runtimeTemplate.moduleExports({
|
106 | module: moduleGraph.getModule(dep),
|
107 | chunkGraph,
|
108 | request: dep.request,
|
109 | runtimeRequirements
|
110 | })}${pathToString(dep.path)};\n`,
|
111 | InitFragment.STAGE_PROVIDES,
|
112 | 1,
|
113 | `provided ${dep.identifier}`
|
114 | )
|
115 | );
|
116 | source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
|
117 | }
|
118 | }
|
119 |
|
120 | ProvidedDependency.Template = ProvidedDependencyTemplate;
|
121 |
|
122 | module.exports = ProvidedDependency;
|