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