UNPKG

5.26 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 { RawSource } = require("webpack-sources");
9const Module = require("../Module");
10const RuntimeGlobals = require("../RuntimeGlobals");
11const makeSerializable = require("../util/makeSerializable");
12const FallbackDependency = require("./FallbackDependency");
13const RemoteToExternalDependency = require("./RemoteToExternalDependency");
14
15/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
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(["remote", "share-init"]);
30const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
31
32class RemoteModule extends Module {
33 /**
34 * @param {string} request request string
35 * @param {string[]} externalRequests list of external requests to containers
36 * @param {string} internalRequest name of exposed module in container
37 * @param {string} shareScope the used share scope name
38 */
39 constructor(request, externalRequests, internalRequest, shareScope) {
40 super("remote-module");
41 this.request = request;
42 this.externalRequests = externalRequests;
43 this.internalRequest = internalRequest;
44 this.shareScope = shareScope;
45 this._identifier = `remote (${shareScope}) ${this.externalRequests.join(
46 " "
47 )} ${this.internalRequest}`;
48 }
49
50 /**
51 * @returns {string} a unique identifier of the module
52 */
53 identifier() {
54 return this._identifier;
55 }
56
57 /**
58 * @param {RequestShortener} requestShortener the request shortener
59 * @returns {string} a user readable identifier of the module
60 */
61 readableIdentifier(requestShortener) {
62 return `remote ${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 `webpack/container/remote/${this.request}`;
71 }
72
73 /**
74 * @param {NeedBuildContext} context context info
75 * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
76 * @returns {void}
77 */
78 needBuild(context, callback) {
79 callback(null, !this.buildInfo);
80 }
81
82 /**
83 * @param {WebpackOptions} options webpack options
84 * @param {Compilation} compilation the compilation
85 * @param {ResolverWithOptions} resolver the resolver
86 * @param {InputFileSystem} fs the file system
87 * @param {function(WebpackError=): void} callback callback function
88 * @returns {void}
89 */
90 build(options, compilation, resolver, fs, callback) {
91 this.buildMeta = {};
92 this.buildInfo = {
93 strict: true
94 };
95
96 this.clearDependenciesAndBlocks();
97 if (this.externalRequests.length === 1) {
98 this.addDependency(
99 new RemoteToExternalDependency(this.externalRequests[0])
100 );
101 } else {
102 this.addDependency(new FallbackDependency(this.externalRequests));
103 }
104
105 callback();
106 }
107
108 /**
109 * @param {string=} type the source type for which the size should be estimated
110 * @returns {number} the estimated size of the module (must be non-zero)
111 */
112 size(type) {
113 return 6;
114 }
115
116 /**
117 * @returns {Set<string>} types available (do not mutate)
118 */
119 getSourceTypes() {
120 return TYPES;
121 }
122
123 /**
124 * @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
125 */
126 nameForCondition() {
127 return this.request;
128 }
129
130 /**
131 * @param {CodeGenerationContext} context context for code generation
132 * @returns {CodeGenerationResult} result
133 */
134 codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
135 const module = moduleGraph.getModule(this.dependencies[0]);
136 const id = module && chunkGraph.getModuleId(module);
137 const sources = new Map();
138 sources.set("remote", new RawSource(""));
139 const data = new Map();
140 data.set("share-init", [
141 {
142 shareScope: this.shareScope,
143 initStage: 20,
144 init: id === undefined ? "" : `initExternal(${JSON.stringify(id)});`
145 }
146 ]);
147 return { sources, data, runtimeRequirements: RUNTIME_REQUIREMENTS };
148 }
149
150 serialize(context) {
151 const { write } = context;
152 write(this.request);
153 write(this.externalRequests);
154 write(this.internalRequest);
155 write(this.shareScope);
156 super.serialize(context);
157 }
158
159 static deserialize(context) {
160 const { read } = context;
161 const obj = new RemoteModule(read(), read(), read(), read());
162 obj.deserialize(context);
163 return obj;
164 }
165}
166
167makeSerializable(RemoteModule, "webpack/lib/container/RemoteModule");
168
169module.exports = RemoteModule;