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