1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Florent Cailhol @ooflorent
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const Dependency = require("../Dependency");
|
---|
9 | const InitFragment = require("../InitFragment");
|
---|
10 | const makeSerializable = require("../util/makeSerializable");
|
---|
11 | const ModuleDependency = require("./ModuleDependency");
|
---|
12 |
|
---|
13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
14 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
15 | /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
---|
16 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
17 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
18 | /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
|
---|
19 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
20 | /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
|
---|
21 | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
---|
22 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
23 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
24 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
25 | /** @typedef {import("../util/Hash")} Hash */
|
---|
26 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * @param {string[]|null} path the property path array
|
---|
30 | * @returns {string} the converted path
|
---|
31 | */
|
---|
32 | const pathToString = path =>
|
---|
33 | path !== null && path.length > 0
|
---|
34 | ? path.map(part => `[${JSON.stringify(part)}]`).join("")
|
---|
35 | : "";
|
---|
36 |
|
---|
37 | class ProvidedDependency extends ModuleDependency {
|
---|
38 | /**
|
---|
39 | * @param {string} request request
|
---|
40 | * @param {string} identifier identifier
|
---|
41 | * @param {string[]} ids ids
|
---|
42 | * @param {Range} range range
|
---|
43 | */
|
---|
44 | constructor(request, identifier, ids, range) {
|
---|
45 | super(request);
|
---|
46 | this.identifier = identifier;
|
---|
47 | this.ids = ids;
|
---|
48 | this.range = range;
|
---|
49 | this._hashUpdate = undefined;
|
---|
50 | }
|
---|
51 |
|
---|
52 | get type() {
|
---|
53 | return "provided";
|
---|
54 | }
|
---|
55 |
|
---|
56 | get category() {
|
---|
57 | return "esm";
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Returns list of exports referenced by this dependency
|
---|
62 | * @param {ModuleGraph} moduleGraph module graph
|
---|
63 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
64 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
65 | */
|
---|
66 | getReferencedExports(moduleGraph, runtime) {
|
---|
67 | const ids = this.ids;
|
---|
68 | if (ids.length === 0) return Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
69 | return [ids];
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Update the hash
|
---|
74 | * @param {Hash} hash hash to be updated
|
---|
75 | * @param {UpdateHashContext} context context
|
---|
76 | * @returns {void}
|
---|
77 | */
|
---|
78 | updateHash(hash, context) {
|
---|
79 | if (this._hashUpdate === undefined) {
|
---|
80 | this._hashUpdate = this.identifier + (this.ids ? this.ids.join(",") : "");
|
---|
81 | }
|
---|
82 | hash.update(this._hashUpdate);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * @param {ObjectSerializerContext} context context
|
---|
87 | */
|
---|
88 | serialize(context) {
|
---|
89 | const { write } = context;
|
---|
90 | write(this.identifier);
|
---|
91 | write(this.ids);
|
---|
92 | super.serialize(context);
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * @param {ObjectDeserializerContext} context context
|
---|
97 | */
|
---|
98 | deserialize(context) {
|
---|
99 | const { read } = context;
|
---|
100 | this.identifier = read();
|
---|
101 | this.ids = read();
|
---|
102 | super.deserialize(context);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | makeSerializable(
|
---|
107 | ProvidedDependency,
|
---|
108 | "webpack/lib/dependencies/ProvidedDependency"
|
---|
109 | );
|
---|
110 |
|
---|
111 | class ProvidedDependencyTemplate extends ModuleDependency.Template {
|
---|
112 | /**
|
---|
113 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
114 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
115 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
116 | * @returns {void}
|
---|
117 | */
|
---|
118 | apply(
|
---|
119 | dependency,
|
---|
120 | source,
|
---|
121 | {
|
---|
122 | runtime,
|
---|
123 | runtimeTemplate,
|
---|
124 | moduleGraph,
|
---|
125 | chunkGraph,
|
---|
126 | initFragments,
|
---|
127 | runtimeRequirements
|
---|
128 | }
|
---|
129 | ) {
|
---|
130 | const dep = /** @type {ProvidedDependency} */ (dependency);
|
---|
131 | const connection =
|
---|
132 | /** @type {ModuleGraphConnection} */
|
---|
133 | (moduleGraph.getConnection(dep));
|
---|
134 | const exportsInfo = moduleGraph.getExportsInfo(connection.module);
|
---|
135 | const usedName = exportsInfo.getUsedName(dep.ids, runtime);
|
---|
136 | initFragments.push(
|
---|
137 | new InitFragment(
|
---|
138 | `/* provided dependency */ var ${
|
---|
139 | dep.identifier
|
---|
140 | } = ${runtimeTemplate.moduleExports({
|
---|
141 | module: moduleGraph.getModule(dep),
|
---|
142 | chunkGraph,
|
---|
143 | request: dep.request,
|
---|
144 | runtimeRequirements
|
---|
145 | })}${pathToString(/** @type {string[]} */ (usedName))};\n`,
|
---|
146 | InitFragment.STAGE_PROVIDES,
|
---|
147 | 1,
|
---|
148 | `provided ${dep.identifier}`
|
---|
149 | )
|
---|
150 | );
|
---|
151 | source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | ProvidedDependency.Template = ProvidedDependencyTemplate;
|
---|
156 |
|
---|
157 | module.exports = ProvidedDependency;
|
---|