UNPKG

5.49 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
4*/
5
6"use strict";
7
8const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
9const Module = require("../Module");
10const RuntimeGlobals = require("../RuntimeGlobals");
11const makeSerializable = require("../util/makeSerializable");
12const ProvideForSharedDependency = require("./ProvideForSharedDependency");
13
14/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
15/** @typedef {import("../Chunk")} Chunk */
16/** @typedef {import("../ChunkGraph")} ChunkGraph */
17/** @typedef {import("../ChunkGroup")} ChunkGroup */
18/** @typedef {import("../Compilation")} Compilation */
19/** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
20/** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
21/** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
22/** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
23/** @typedef {import("../RequestShortener")} RequestShortener */
24/** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
25/** @typedef {import("../WebpackError")} WebpackError */
26/** @typedef {import("../util/Hash")} Hash */
27/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
28
29const TYPES = new Set(["share-init"]);
30
31class ProvideSharedModule extends Module {
32 /**
33 * @param {string} shareScope shared scope name
34 * @param {string} name shared key
35 * @param {string | false} version version
36 * @param {string} request request to the provided module
37 * @param {boolean} eager include the module in sync way
38 */
39 constructor(shareScope, name, version, request, eager) {
40 super("provide-module");
41 this._shareScope = shareScope;
42 this._name = name;
43 this._version = version;
44 this._request = request;
45 this._eager = eager;
46 }
47
48 /**
49 * @returns {string} a unique identifier of the module
50 */
51 identifier() {
52 return `provide module (${this._shareScope}) ${this._name}@${this._version} = ${this._request}`;
53 }
54
55 /**
56 * @param {RequestShortener} requestShortener the request shortener
57 * @returns {string} a user readable identifier of the module
58 */
59 readableIdentifier(requestShortener) {
60 return `provide shared module (${this._shareScope}) ${this._name}@${
61 this._version
62 } = ${requestShortener.shorten(this._request)}`;
63 }
64
65 /**
66 * @param {LibIdentOptions} options options
67 * @returns {string | null} an identifier for library inclusion
68 */
69 libIdent(options) {
70 return `${this.layer ? `(${this.layer})/` : ""}webpack/sharing/provide/${
71 this._shareScope
72 }/${this._name}`;
73 }
74
75 /**
76 * @param {NeedBuildContext} context context info
77 * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
78 * @returns {void}
79 */
80 needBuild(context, callback) {
81 callback(null, !this.buildInfo);
82 }
83
84 /**
85 * @param {WebpackOptions} options webpack options
86 * @param {Compilation} compilation the compilation
87 * @param {ResolverWithOptions} resolver the resolver
88 * @param {InputFileSystem} fs the file system
89 * @param {function(WebpackError=): void} callback callback function
90 * @returns {void}
91 */
92 build(options, compilation, resolver, fs, callback) {
93 this.buildMeta = {};
94 this.buildInfo = {
95 strict: true
96 };
97
98 this.clearDependenciesAndBlocks();
99 const dep = new ProvideForSharedDependency(this._request);
100 if (this._eager) {
101 this.addDependency(dep);
102 } else {
103 const block = new AsyncDependenciesBlock({});
104 block.addDependency(dep);
105 this.addBlock(block);
106 }
107
108 callback();
109 }
110
111 /**
112 * @param {string=} type the source type for which the size should be estimated
113 * @returns {number} the estimated size of the module (must be non-zero)
114 */
115 size(type) {
116 return 42;
117 }
118
119 /**
120 * @returns {Set<string>} types available (do not mutate)
121 */
122 getSourceTypes() {
123 return TYPES;
124 }
125
126 /**
127 * @param {CodeGenerationContext} context context for code generation
128 * @returns {CodeGenerationResult} result
129 */
130 codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
131 const runtimeRequirements = new Set([RuntimeGlobals.initializeSharing]);
132 const code = `register(${JSON.stringify(this._name)}, ${JSON.stringify(
133 this._version || "0"
134 )}, ${
135 this._eager
136 ? runtimeTemplate.syncModuleFactory({
137 dependency: this.dependencies[0],
138 chunkGraph,
139 request: this._request,
140 runtimeRequirements
141 })
142 : runtimeTemplate.asyncModuleFactory({
143 block: this.blocks[0],
144 chunkGraph,
145 request: this._request,
146 runtimeRequirements
147 })
148 }${this._eager ? ", 1" : ""});`;
149 const sources = new Map();
150 const data = new Map();
151 data.set("share-init", [
152 {
153 shareScope: this._shareScope,
154 initStage: 10,
155 init: code
156 }
157 ]);
158 return { sources, data, runtimeRequirements };
159 }
160
161 serialize(context) {
162 const { write } = context;
163 write(this._shareScope);
164 write(this._name);
165 write(this._version);
166 write(this._request);
167 write(this._eager);
168 super.serialize(context);
169 }
170
171 static deserialize(context) {
172 const { read } = context;
173 const obj = new ProvideSharedModule(read(), read(), read(), read(), read());
174 obj.deserialize(context);
175 return obj;
176 }
177}
178
179makeSerializable(
180 ProvideSharedModule,
181 "webpack/lib/sharing/ProvideSharedModule"
182);
183
184module.exports = ProvideSharedModule;