| 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 {
|
|---|
| 11 | JAVASCRIPT_TYPES,
|
|---|
| 12 | REMOTE_AND_SHARE_INIT_TYPES
|
|---|
| 13 | } = require("../ModuleSourceTypeConstants");
|
|---|
| 14 | const { WEBPACK_MODULE_TYPE_REMOTE } = require("../ModuleTypeConstants");
|
|---|
| 15 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 16 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 17 | const FallbackDependency = require("./FallbackDependency");
|
|---|
| 18 | const RemoteToExternalDependency = require("./RemoteToExternalDependency");
|
|---|
| 19 |
|
|---|
| 20 | /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
|---|
| 21 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 22 | /** @typedef {import("../Module").BuildCallback} BuildCallback */
|
|---|
| 23 | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
|---|
| 24 | /** @typedef {import("../Module").CodeGenerationResultData} CodeGenerationResultData */
|
|---|
| 25 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
|---|
| 26 | /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
|
|---|
| 27 | /** @typedef {import("../Module").LibIdent} LibIdent */
|
|---|
| 28 | /** @typedef {import("../Module").NameForCondition} NameForCondition */
|
|---|
| 29 | /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
|
|---|
| 30 | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
|---|
| 31 | /** @typedef {import("../Module").Sources} Sources */
|
|---|
| 32 | /** @typedef {import("../Module").SourceTypes} SourceTypes */
|
|---|
| 33 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 34 | /** @typedef {import("../Module").ExportsType} ExportsType */
|
|---|
| 35 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
|---|
| 36 | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
|---|
| 37 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 38 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 39 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 40 | /** @typedef {import("../Module").BasicSourceTypes} BasicSourceTypes */
|
|---|
| 41 |
|
|---|
| 42 | const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
|
|---|
| 43 |
|
|---|
| 44 | /** @typedef {string[]} ExternalRequests */
|
|---|
| 45 |
|
|---|
| 46 | class RemoteModule extends Module {
|
|---|
| 47 | /**
|
|---|
| 48 | * Creates an instance of RemoteModule.
|
|---|
| 49 | * @param {string} request request string
|
|---|
| 50 | * @param {ExternalRequests} externalRequests list of external requests to containers
|
|---|
| 51 | * @param {string} internalRequest name of exposed module in container
|
|---|
| 52 | * @param {string} shareScope the used share scope name
|
|---|
| 53 | */
|
|---|
| 54 | constructor(request, externalRequests, internalRequest, shareScope) {
|
|---|
| 55 | super(WEBPACK_MODULE_TYPE_REMOTE);
|
|---|
| 56 | /** @type {string} */
|
|---|
| 57 | this.request = request;
|
|---|
| 58 | /** @type {ExternalRequests} */
|
|---|
| 59 | this.externalRequests = externalRequests;
|
|---|
| 60 | /** @type {string} */
|
|---|
| 61 | this.internalRequest = internalRequest;
|
|---|
| 62 | /** @type {string} */
|
|---|
| 63 | this.shareScope = shareScope;
|
|---|
| 64 | /** @type {string} */
|
|---|
| 65 | this._identifier = `remote (${shareScope}) ${this.externalRequests.join(
|
|---|
| 66 | " "
|
|---|
| 67 | )} ${this.internalRequest}`;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Returns the unique identifier used to reference this module.
|
|---|
| 72 | * @returns {string} a unique identifier of the module
|
|---|
| 73 | */
|
|---|
| 74 | identifier() {
|
|---|
| 75 | return this._identifier;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Returns a human-readable identifier for this module.
|
|---|
| 80 | * @param {RequestShortener} requestShortener the request shortener
|
|---|
| 81 | * @returns {string} a user readable identifier of the module
|
|---|
| 82 | */
|
|---|
| 83 | readableIdentifier(requestShortener) {
|
|---|
| 84 | return `remote ${this.request}`;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * Gets the library identifier.
|
|---|
| 89 | * @param {LibIdentOptions} options options
|
|---|
| 90 | * @returns {LibIdent | null} an identifier for library inclusion
|
|---|
| 91 | */
|
|---|
| 92 | libIdent(options) {
|
|---|
| 93 | return `${this.layer ? `(${this.layer})/` : ""}webpack/container/remote/${
|
|---|
| 94 | this.request
|
|---|
| 95 | }`;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Checks whether the module needs to be rebuilt for the current build state.
|
|---|
| 100 | * @param {NeedBuildContext} context context info
|
|---|
| 101 | * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
|
|---|
| 102 | * @returns {void}
|
|---|
| 103 | */
|
|---|
| 104 | needBuild(context, callback) {
|
|---|
| 105 | callback(null, !this.buildInfo);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Builds the module using the provided compilation context.
|
|---|
| 110 | * @param {WebpackOptions} options webpack options
|
|---|
| 111 | * @param {Compilation} compilation the compilation
|
|---|
| 112 | * @param {ResolverWithOptions} resolver the resolver
|
|---|
| 113 | * @param {InputFileSystem} fs the file system
|
|---|
| 114 | * @param {BuildCallback} callback callback function
|
|---|
| 115 | * @returns {void}
|
|---|
| 116 | */
|
|---|
| 117 | build(options, compilation, resolver, fs, callback) {
|
|---|
| 118 | this.buildMeta = {};
|
|---|
| 119 | this.buildInfo = {
|
|---|
| 120 | strict: true
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | this.clearDependenciesAndBlocks();
|
|---|
| 124 | if (this.externalRequests.length === 1) {
|
|---|
| 125 | this.addDependency(
|
|---|
| 126 | new RemoteToExternalDependency(this.externalRequests[0])
|
|---|
| 127 | );
|
|---|
| 128 | } else {
|
|---|
| 129 | this.addDependency(new FallbackDependency(this.externalRequests));
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | callback();
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Returns the estimated size for the requested source type.
|
|---|
| 137 | * @param {string=} type the source type for which the size should be estimated
|
|---|
| 138 | * @returns {number} the estimated size of the module (must be non-zero)
|
|---|
| 139 | */
|
|---|
| 140 | size(type) {
|
|---|
| 141 | return 6;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Returns the source types this module can generate.
|
|---|
| 146 | * @returns {SourceTypes} types available (do not mutate)
|
|---|
| 147 | */
|
|---|
| 148 | getSourceTypes() {
|
|---|
| 149 | return REMOTE_AND_SHARE_INIT_TYPES;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * Basic source types are high-level categories like javascript, css, webassembly, etc.
|
|---|
| 154 | * We only have built-in knowledge about the javascript basic type here; other basic types may be
|
|---|
| 155 | * added or changed over time by generators and do not need to be handled or detected here.
|
|---|
| 156 | *
|
|---|
| 157 | * Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
|
|---|
| 158 | * from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
|
|---|
| 159 | * @returns {BasicSourceTypes} types available (do not mutate)
|
|---|
| 160 | */
|
|---|
| 161 | getSourceBasicTypes() {
|
|---|
| 162 | return JAVASCRIPT_TYPES;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * Returns export type.
|
|---|
| 167 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 168 | * @param {boolean | undefined} strict the importing module is strict
|
|---|
| 169 | * @returns {ExportsType} export type
|
|---|
| 170 | * "namespace": Exports is already a namespace object. namespace = exports.
|
|---|
| 171 | * "dynamic": Check at runtime if __esModule is set. When set: namespace = { ...exports, default: exports }. When not set: namespace = { default: exports }.
|
|---|
| 172 | * "default-only": Provide a namespace object with only default export. namespace = { default: exports }
|
|---|
| 173 | * "default-with-named": Provide a namespace object with named and default export. namespace = { ...exports, default: exports }
|
|---|
| 174 | */
|
|---|
| 175 | getExportsType(moduleGraph, strict) {
|
|---|
| 176 | return "dynamic";
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /**
|
|---|
| 180 | * Returns the path used when matching this module against rule conditions.
|
|---|
| 181 | * @returns {NameForCondition | null} absolute path which should be used for condition matching (usually the resource path)
|
|---|
| 182 | */
|
|---|
| 183 | nameForCondition() {
|
|---|
| 184 | return this.request;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | /**
|
|---|
| 188 | * Generates code and runtime requirements for this module.
|
|---|
| 189 | * @param {CodeGenerationContext} context context for code generation
|
|---|
| 190 | * @returns {CodeGenerationResult} result
|
|---|
| 191 | */
|
|---|
| 192 | codeGeneration({ moduleGraph, chunkGraph }) {
|
|---|
| 193 | const module = moduleGraph.getModule(this.dependencies[0]);
|
|---|
| 194 | const id = module && chunkGraph.getModuleId(module);
|
|---|
| 195 | /** @type {Sources} */
|
|---|
| 196 | const sources = new Map();
|
|---|
| 197 | sources.set("remote", new RawSource(""));
|
|---|
| 198 | /** @type {CodeGenerationResultData} */
|
|---|
| 199 | const data = new Map();
|
|---|
| 200 | data.set("share-init", [
|
|---|
| 201 | {
|
|---|
| 202 | shareScope: this.shareScope,
|
|---|
| 203 | initStage: 20,
|
|---|
| 204 | init: id === undefined ? "" : `initExternal(${JSON.stringify(id)});`
|
|---|
| 205 | }
|
|---|
| 206 | ]);
|
|---|
| 207 | return { sources, data, runtimeRequirements: RUNTIME_REQUIREMENTS };
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * Serializes this instance into the provided serializer context.
|
|---|
| 212 | * @param {ObjectSerializerContext} context context
|
|---|
| 213 | */
|
|---|
| 214 | serialize(context) {
|
|---|
| 215 | const { write } = context;
|
|---|
| 216 | write(this.request);
|
|---|
| 217 | write(this.externalRequests);
|
|---|
| 218 | write(this.internalRequest);
|
|---|
| 219 | write(this.shareScope);
|
|---|
| 220 | super.serialize(context);
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /**
|
|---|
| 224 | * Restores this instance from the provided deserializer context.
|
|---|
| 225 | * @param {ObjectDeserializerContext} context context
|
|---|
| 226 | * @returns {RemoteModule} deserialized module
|
|---|
| 227 | */
|
|---|
| 228 | static deserialize(context) {
|
|---|
| 229 | const { read } = context;
|
|---|
| 230 | const obj = new RemoteModule(read(), read(), read(), read());
|
|---|
| 231 | obj.deserialize(context);
|
|---|
| 232 | return obj;
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | makeSerializable(RemoteModule, "webpack/lib/container/RemoteModule");
|
|---|
| 237 |
|
|---|
| 238 | module.exports = RemoteModule;
|
|---|