UNPKG

3.21 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Florent Cailhol @ooflorent
4*/
5
6"use strict";
7
8const InitFragment = require("../InitFragment");
9const makeSerializable = require("../util/makeSerializable");
10const ModuleDependency = require("./ModuleDependency");
11
12/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
13/** @typedef {import("../ChunkGraph")} ChunkGraph */
14/** @typedef {import("../Dependency")} Dependency */
15/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
16/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
17/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
18/** @typedef {import("../ModuleGraph")} ModuleGraph */
19/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
20/** @typedef {import("../util/Hash")} Hash */
21
22/**
23 * @param {string[]|null} path the property path array
24 * @returns {string} the converted path
25 */
26const pathToString = path =>
27 path !== null && path.length > 0
28 ? path.map(part => `[${JSON.stringify(part)}]`).join("")
29 : "";
30
31class 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 * Update the hash
50 * @param {Hash} hash hash to be updated
51 * @param {UpdateHashContext} context context
52 * @returns {void}
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
77makeSerializable(
78 ProvidedDependency,
79 "webpack/lib/dependencies/ProvidedDependency"
80);
81
82class ProvidedDependencyTemplate extends ModuleDependency.Template {
83 /**
84 * @param {Dependency} dependency the dependency for which the template should be applied
85 * @param {ReplaceSource} source the current replace source which can be modified
86 * @param {DependencyTemplateContext} templateContext the context object
87 * @returns {void}
88 */
89 apply(
90 dependency,
91 source,
92 {
93 runtimeTemplate,
94 moduleGraph,
95 chunkGraph,
96 initFragments,
97 runtimeRequirements
98 }
99 ) {
100 const dep = /** @type {ProvidedDependency} */ (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
120ProvidedDependency.Template = ProvidedDependencyTemplate;
121
122module.exports = ProvidedDependency;