2020-02-20 03:25:49 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { ConcatSource } = require("webpack-sources");
|
2020-07-28 00:09:48 +08:00
|
|
|
const { UsageState } = require("../ExportsInfo");
|
2023-05-27 00:06:16 +08:00
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
2020-02-20 03:25:49 +08:00
|
|
|
const propertyAccess = require("../util/propertyAccess");
|
2020-07-28 00:09:48 +08:00
|
|
|
const { getEntryRuntime } = require("../util/runtime");
|
2020-02-20 03:25:49 +08:00
|
|
|
const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
|
|
|
|
|
|
|
|
/** @typedef {import("webpack-sources").Source} Source */
|
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
|
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
|
2021-02-05 06:17:20 +08:00
|
|
|
/** @typedef {import("../Chunk")} Chunk */
|
2020-02-20 03:25:49 +08:00
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
/** @typedef {import("../Module")} Module */
|
2021-02-11 02:14:50 +08:00
|
|
|
/** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
|
2020-02-20 03:25:49 +08:00
|
|
|
/** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
|
|
|
|
|
|
|
|
/**
|
2024-06-11 21:09:50 +08:00
|
|
|
* @typedef {object} ExportPropertyLibraryPluginParsed
|
2020-02-20 03:25:49 +08:00
|
|
|
* @property {string | string[]} export
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2024-06-11 21:09:50 +08:00
|
|
|
* @typedef {object} ExportPropertyLibraryPluginOptions
|
2020-02-20 03:25:49 +08:00
|
|
|
* @property {LibraryType} type
|
|
|
|
* @property {boolean} nsObjectUsed the namespace object is used
|
2024-07-09 20:02:25 +08:00
|
|
|
* @property {boolean} runtimeExportsUsed runtime exports are used
|
2025-04-06 22:53:09 +08:00
|
|
|
* @property {boolean} renderStartupUsed render startup is used
|
2020-02-20 03:25:49 +08:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @typedef {ExportPropertyLibraryPluginParsed} T
|
|
|
|
* @extends {AbstractLibraryPlugin<ExportPropertyLibraryPluginParsed>}
|
|
|
|
*/
|
|
|
|
class ExportPropertyLibraryPlugin extends AbstractLibraryPlugin {
|
|
|
|
/**
|
|
|
|
* @param {ExportPropertyLibraryPluginOptions} options options
|
|
|
|
*/
|
2025-04-06 22:53:09 +08:00
|
|
|
constructor({ type, nsObjectUsed, runtimeExportsUsed, renderStartupUsed }) {
|
2020-02-20 03:25:49 +08:00
|
|
|
super({
|
|
|
|
pluginName: "ExportPropertyLibraryPlugin",
|
|
|
|
type
|
|
|
|
});
|
|
|
|
this.nsObjectUsed = nsObjectUsed;
|
2024-07-09 20:02:25 +08:00
|
|
|
this.runtimeExportsUsed = runtimeExportsUsed;
|
2025-04-06 22:53:09 +08:00
|
|
|
this.renderStartupUsed = renderStartupUsed;
|
2020-02-20 03:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {LibraryOptions} library normalized library option
|
|
|
|
* @returns {T | false} preprocess as needed by overriding
|
|
|
|
*/
|
|
|
|
parseOptions(library) {
|
|
|
|
return {
|
2024-10-02 05:18:10 +08:00
|
|
|
export: /** @type {string | string[]} */ (library.export)
|
2020-02-20 03:25:49 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Module} module the exporting entry module
|
2020-07-28 00:09:48 +08:00
|
|
|
* @param {string} entryName the name of the entrypoint
|
2020-02-20 03:25:49 +08:00
|
|
|
* @param {LibraryContext<T>} libraryContext context
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-07-28 00:09:48 +08:00
|
|
|
finishEntryModule(
|
|
|
|
module,
|
|
|
|
entryName,
|
|
|
|
{ options, compilation, compilation: { moduleGraph } }
|
|
|
|
) {
|
|
|
|
const runtime = getEntryRuntime(compilation, entryName);
|
2020-02-20 03:25:49 +08:00
|
|
|
if (options.export) {
|
|
|
|
const exportsInfo = moduleGraph.getExportInfo(
|
|
|
|
module,
|
|
|
|
Array.isArray(options.export) ? options.export[0] : options.export
|
|
|
|
);
|
2020-07-28 00:09:48 +08:00
|
|
|
exportsInfo.setUsed(UsageState.Used, runtime);
|
2020-02-20 03:25:49 +08:00
|
|
|
exportsInfo.canMangleUse = false;
|
|
|
|
} else {
|
|
|
|
const exportsInfo = moduleGraph.getExportsInfo(module);
|
|
|
|
if (this.nsObjectUsed) {
|
2020-07-28 00:09:48 +08:00
|
|
|
exportsInfo.setUsedInUnknownWay(runtime);
|
2020-02-20 03:25:49 +08:00
|
|
|
} else {
|
2020-07-28 00:09:48 +08:00
|
|
|
exportsInfo.setAllKnownExportsUsed(runtime);
|
2020-02-20 03:25:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
moduleGraph.addExtraReason(module, "used as library export");
|
|
|
|
}
|
2021-02-05 06:17:20 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Chunk} chunk the chunk
|
|
|
|
* @param {Set<string>} set runtime requirements
|
|
|
|
* @param {LibraryContext<T>} libraryContext context
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2024-07-09 20:02:25 +08:00
|
|
|
runtimeRequirements(chunk, set, libraryContext) {
|
|
|
|
if (this.runtimeExportsUsed) {
|
|
|
|
set.add(RuntimeGlobals.exports);
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 06:17:20 +08:00
|
|
|
|
2020-02-20 03:25:49 +08:00
|
|
|
/**
|
|
|
|
* @param {Source} source source
|
2021-02-11 02:14:50 +08:00
|
|
|
* @param {Module} module module
|
|
|
|
* @param {StartupRenderContext} renderContext render context
|
2020-02-20 03:25:49 +08:00
|
|
|
* @param {LibraryContext<T>} libraryContext context
|
|
|
|
* @returns {Source} source with library export
|
|
|
|
*/
|
2021-02-11 02:14:50 +08:00
|
|
|
renderStartup(source, module, renderContext, { options }) {
|
2025-04-06 22:53:09 +08:00
|
|
|
if (!this.renderStartupUsed) return source;
|
2020-02-20 03:25:49 +08:00
|
|
|
if (!options.export) return source;
|
2023-05-26 23:34:26 +08:00
|
|
|
const postfix = `${RuntimeGlobals.exports} = ${
|
|
|
|
RuntimeGlobals.exports
|
|
|
|
}${propertyAccess(
|
2020-02-20 03:25:49 +08:00
|
|
|
Array.isArray(options.export) ? options.export : [options.export]
|
2021-02-11 02:14:50 +08:00
|
|
|
)};\n`;
|
2020-02-20 03:25:49 +08:00
|
|
|
return new ConcatSource(source, postfix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ExportPropertyLibraryPlugin;
|