webpack/lib/sharing/ProvideSharedModule.js

193 lines
5.8 KiB
JavaScript
Raw Normal View History

2020-05-23 22:08:51 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
*/
"use strict";
const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
const Module = require("../Module");
2024-11-01 04:19:07 +08:00
const { SHARED_INIT_TYPES } = require("../ModuleSourceTypesConstants");
const { WEBPACK_MODULE_TYPE_PROVIDE } = require("../ModuleTypeConstants");
2020-05-23 22:08:51 +08:00
const RuntimeGlobals = require("../RuntimeGlobals");
const makeSerializable = require("../util/makeSerializable");
const ProvideForSharedDependency = require("./ProvideForSharedDependency");
2020-05-23 22:08:51 +08:00
2025-08-20 18:50:12 +08:00
/** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
2020-05-23 22:08:51 +08:00
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Module").BuildCallback} BuildCallback */
2020-05-23 22:08:51 +08:00
/** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
/** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
/** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
2025-09-11 08:10:10 +08:00
/** @typedef {import("../Module").LibIdent} LibIdent */
/** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
2020-05-23 22:08:51 +08:00
/** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
2024-02-17 01:39:12 +08:00
/** @typedef {import("../Module").SourceTypes} SourceTypes */
2020-05-23 22:08:51 +08:00
/** @typedef {import("../RequestShortener")} RequestShortener */
/** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
2023-04-12 02:57:43 +08:00
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
2020-05-23 22:08:51 +08:00
/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
class ProvideSharedModule extends Module {
2020-05-23 22:08:51 +08:00
/**
* @param {string} shareScope shared scope name
* @param {string} name shared key
* @param {string | false} version version
2020-05-23 22:08:51 +08:00
* @param {string} request request to the provided module
* @param {boolean} eager include the module in sync way
2020-05-23 22:08:51 +08:00
*/
constructor(shareScope, name, version, request, eager) {
super(WEBPACK_MODULE_TYPE_PROVIDE);
2020-05-23 22:08:51 +08:00
this._shareScope = shareScope;
this._name = name;
this._version = version;
this._request = request;
this._eager = eager;
2020-05-23 22:08:51 +08:00
}
/**
* @returns {string} a unique identifier of the module
*/
identifier() {
return `provide module (${this._shareScope}) ${this._name}@${this._version} = ${this._request}`;
2020-05-23 22:08:51 +08:00
}
/**
* @param {RequestShortener} requestShortener the request shortener
* @returns {string} a user readable identifier of the module
*/
readableIdentifier(requestShortener) {
return `provide shared module (${this._shareScope}) ${this._name}@${
this._version
} = ${requestShortener.shorten(this._request)}`;
2020-05-23 22:08:51 +08:00
}
/**
* @param {LibIdentOptions} options options
2025-09-11 08:10:10 +08:00
* @returns {LibIdent | null} an identifier for library inclusion
2020-05-23 22:08:51 +08:00
*/
libIdent(options) {
return `${this.layer ? `(${this.layer})/` : ""}webpack/sharing/provide/${
this._shareScope
}/${this._name}`;
2020-05-23 22:08:51 +08:00
}
/**
* @param {NeedBuildContext} context context info
* @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
2020-05-23 22:08:51 +08:00
* @returns {void}
*/
needBuild(context, callback) {
callback(null, !this.buildInfo);
}
/**
* @param {WebpackOptions} options webpack options
* @param {Compilation} compilation the compilation
* @param {ResolverWithOptions} resolver the resolver
* @param {InputFileSystem} fs the file system
* @param {BuildCallback} callback callback function
2020-05-23 22:08:51 +08:00
* @returns {void}
*/
build(options, compilation, resolver, fs, callback) {
this.buildMeta = {};
this.buildInfo = {
strict: true
};
this.clearDependenciesAndBlocks();
const dep = new ProvideForSharedDependency(this._request);
if (this._eager) {
this.addDependency(dep);
} else {
const block = new AsyncDependenciesBlock({});
block.addDependency(dep);
this.addBlock(block);
}
2020-05-23 22:08:51 +08:00
callback();
}
/**
* @param {string=} type the source type for which the size should be estimated
* @returns {number} the estimated size of the module (must be non-zero)
*/
size(type) {
return 42;
}
/**
2024-02-17 01:39:12 +08:00
* @returns {SourceTypes} types available (do not mutate)
2020-05-23 22:08:51 +08:00
*/
getSourceTypes() {
2024-11-01 04:19:07 +08:00
return SHARED_INIT_TYPES;
2020-05-23 22:08:51 +08:00
}
/**
* @param {CodeGenerationContext} context context for code generation
* @returns {CodeGenerationResult} result
*/
2025-07-08 22:46:17 +08:00
codeGeneration({ runtimeTemplate, chunkGraph }) {
2020-05-23 22:08:51 +08:00
const runtimeRequirements = new Set([RuntimeGlobals.initializeSharing]);
const code = `register(${JSON.stringify(this._name)}, ${JSON.stringify(
this._version || "0"
)}, ${
this._eager
? runtimeTemplate.syncModuleFactory({
dependency: this.dependencies[0],
chunkGraph,
request: this._request,
runtimeRequirements
2024-01-14 09:41:34 +08:00
})
: runtimeTemplate.asyncModuleFactory({
block: this.blocks[0],
chunkGraph,
request: this._request,
runtimeRequirements
2024-01-14 09:41:34 +08:00
})
}${this._eager ? ", 1" : ""});`;
2020-05-23 22:08:51 +08:00
const sources = new Map();
const data = new Map();
data.set("share-init", [
{
shareScope: this._shareScope,
initStage: 10,
2020-05-23 22:08:51 +08:00
init: code
}
]);
return { sources, data, runtimeRequirements };
}
2023-04-12 02:57:43 +08:00
/**
* @param {ObjectSerializerContext} context context
*/
2020-05-23 22:08:51 +08:00
serialize(context) {
const { write } = context;
write(this._shareScope);
write(this._name);
write(this._version);
write(this._request);
write(this._eager);
2020-05-23 22:08:51 +08:00
super.serialize(context);
}
2023-06-13 01:24:59 +08:00
/**
* @param {ObjectDeserializerContext} context context
* @returns {ProvideSharedModule} deserialize fallback dependency
*/
2020-05-23 22:08:51 +08:00
static deserialize(context) {
const { read } = context;
const obj = new ProvideSharedModule(read(), read(), read(), read(), read());
2020-05-23 22:08:51 +08:00
obj.deserialize(context);
return obj;
}
}
makeSerializable(
ProvideSharedModule,
"webpack/lib/sharing/ProvideSharedModule"
);
2020-05-23 22:08:51 +08:00
module.exports = ProvideSharedModule;