source: frontend/node_modules/webpack/lib/dependencies/ProvidedDependency.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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