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