Apply module bundling
[platform/framework/web/wrtjs.git] / node_modules / webpack / lib / container / RemoteModule.js
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
8 const { RawSource } = require("webpack-sources");
9 const Module = require("../Module");
10 const RuntimeGlobals = require("../RuntimeGlobals");
11 const makeSerializable = require("../util/makeSerializable");
12 const FallbackDependency = require("./FallbackDependency");
13 const 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
29 const TYPES = new Set(["remote", "share-init"]);
30 const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
31
32 class 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 `${this.layer ? `(${this.layer})/` : ""}webpack/container/remote/${
71                         this.request
72                 }`;
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                 if (this.externalRequests.length === 1) {
100                         this.addDependency(
101                                 new RemoteToExternalDependency(this.externalRequests[0])
102                         );
103                 } else {
104                         this.addDependency(new FallbackDependency(this.externalRequests));
105                 }
106
107                 callback();
108         }
109
110         /**
111          * @param {string=} type the source type for which the size should be estimated
112          * @returns {number} the estimated size of the module (must be non-zero)
113          */
114         size(type) {
115                 return 6;
116         }
117
118         /**
119          * @returns {Set<string>} types available (do not mutate)
120          */
121         getSourceTypes() {
122                 return TYPES;
123         }
124
125         /**
126          * @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
127          */
128         nameForCondition() {
129                 return this.request;
130         }
131
132         /**
133          * @param {CodeGenerationContext} context context for code generation
134          * @returns {CodeGenerationResult} result
135          */
136         codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
137                 const module = moduleGraph.getModule(this.dependencies[0]);
138                 const id = module && chunkGraph.getModuleId(module);
139                 const sources = new Map();
140                 sources.set("remote", new RawSource(""));
141                 const data = new Map();
142                 data.set("share-init", [
143                         {
144                                 shareScope: this.shareScope,
145                                 initStage: 20,
146                                 init: id === undefined ? "" : `initExternal(${JSON.stringify(id)});`
147                         }
148                 ]);
149                 return { sources, data, runtimeRequirements: RUNTIME_REQUIREMENTS };
150         }
151
152         serialize(context) {
153                 const { write } = context;
154                 write(this.request);
155                 write(this.externalRequests);
156                 write(this.internalRequest);
157                 write(this.shareScope);
158                 super.serialize(context);
159         }
160
161         static deserialize(context) {
162                 const { read } = context;
163                 const obj = new RemoteModule(read(), read(), read(), read());
164                 obj.deserialize(context);
165                 return obj;
166         }
167 }
168
169 makeSerializable(RemoteModule, "webpack/lib/container/RemoteModule");
170
171 module.exports = RemoteModule;