webpack/lib/LibManifestPlugin.js

145 lines
4.4 KiB
JavaScript
Raw Normal View History

2015-05-17 00:27:59 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
2017-04-05 20:39:23 +08:00
"use strict";
2015-05-17 00:27:59 +08:00
2018-02-11 12:27:09 +08:00
const asyncLib = require("neo-async");
const EntryDependency = require("./dependencies/EntryDependency");
2021-02-04 21:10:27 +08:00
const { someInIterable } = require("./util/IterableHelpers");
const { compareModulesById } = require("./util/comparators");
const { dirname, mkdirp } = require("./util/fs");
2017-04-05 20:39:23 +08:00
2024-08-06 11:08:48 +08:00
/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
/** @typedef {import("./Compiler")} Compiler */
2024-03-05 22:40:46 +08:00
/** @typedef {import("./Compiler").IntermediateFileSystem} IntermediateFileSystem */
2023-06-17 01:13:03 +08:00
/** @typedef {import("./Module").BuildMeta} BuildMeta */
/**
2024-06-11 21:09:50 +08:00
* @typedef {object} ManifestModuleData
* @property {string | number} id
2023-06-17 01:13:03 +08:00
* @property {BuildMeta} buildMeta
* @property {boolean | string[] | undefined} exports
*/
/**
2024-06-11 21:09:50 +08:00
* @typedef {object} LibManifestPluginOptions
2023-06-17 01:13:03 +08:00
* @property {string=} context Context of requests in the manifest file (defaults to the webpack context).
* @property {boolean=} entryOnly If true, only entry points will be exposed (default: true).
* @property {boolean=} format If true, manifest json file (output) will be formatted.
* @property {string=} name Name of the exposed dll function (external name, use value of 'output.library').
* @property {string} path Absolute path to the manifest json file (output).
* @property {string=} type Type of the dll bundle (external type, use value of 'output.libraryTarget').
*/
2017-04-05 20:39:23 +08:00
class LibManifestPlugin {
2023-06-17 01:13:03 +08:00
/**
* @param {LibManifestPluginOptions} options the options
*/
2017-04-05 20:39:23 +08:00
constructor(options) {
this.options = options;
}
/**
2020-04-23 16:48:36 +08:00
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
2017-04-05 20:39:23 +08:00
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.emit.tapAsync(
{
name: "LibManifestPlugin",
stage: 110
},
2018-02-25 09:00:20 +08:00
(compilation, callback) => {
const moduleGraph = compilation.moduleGraph;
// store used paths to detect issue and output an error. #18200
const usedPaths = new Set();
2024-08-02 23:42:44 +08:00
asyncLib.each(
2018-09-06 22:59:11 +08:00
Array.from(compilation.chunks),
2018-02-25 09:00:20 +08:00
(chunk, callback) => {
if (!chunk.canBeInitial()) {
2018-02-25 09:00:20 +08:00
callback();
return;
}
const chunkGraph = compilation.chunkGraph;
2018-02-25 09:00:20 +08:00
const targetPath = compilation.getPath(this.options.path, {
chunk
});
if (usedPaths.has(targetPath)) {
2024-07-31 12:23:44 +08:00
callback(new Error("each chunk must have a unique path"));
return;
}
usedPaths.add(targetPath);
2018-02-25 09:00:20 +08:00
const name =
this.options.name &&
compilation.getPath(this.options.name, {
chunk,
contentHashType: "javascript"
2017-04-05 20:39:23 +08:00
});
const content = Object.create(null);
for (const module of chunkGraph.getOrderedChunkModulesIterable(
chunk,
compareModulesById(chunkGraph)
)) {
if (
this.options.entryOnly &&
!someInIterable(
moduleGraph.getIncomingConnections(module),
c => c.dependency instanceof EntryDependency
)
) {
continue;
}
const ident = module.libIdent({
2023-06-17 01:13:03 +08:00
context:
this.options.context ||
/** @type {string} */ (compiler.options.context),
associatedObjectForCache: compiler.root
});
if (ident) {
const exportsInfo = moduleGraph.getExportsInfo(module);
const providedExports = exportsInfo.getProvidedExports();
/** @type {ManifestModuleData} */
const data = {
2024-08-06 11:08:48 +08:00
id: /** @type {ModuleId} */ (chunkGraph.getModuleId(module)),
2023-06-17 01:13:03 +08:00
buildMeta: /** @type {BuildMeta} */ (module.buildMeta),
exports: Array.isArray(providedExports)
? providedExports
: undefined
};
content[ident] = data;
}
}
2018-02-25 09:00:20 +08:00
const manifest = {
name,
type: this.options.type,
content
2018-02-25 09:00:20 +08:00
};
// Apply formatting to content if format flag is true;
const manifestContent = this.options.format
? JSON.stringify(manifest, null, 2)
: JSON.stringify(manifest);
const buffer = Buffer.from(manifestContent, "utf8");
2024-03-05 22:40:46 +08:00
const intermediateFileSystem =
/** @type {IntermediateFileSystem} */ (
compiler.intermediateFileSystem
);
mkdirp(
2024-03-05 22:40:46 +08:00
intermediateFileSystem,
dirname(intermediateFileSystem, targetPath),
err => {
if (err) return callback(err);
2024-03-05 22:40:46 +08:00
intermediateFileSystem.writeFile(targetPath, buffer, callback);
}
);
2018-02-25 09:00:20 +08:00
},
callback
);
}
);
2017-04-05 20:39:23 +08:00
}
2015-05-17 00:27:59 +08:00
}
module.exports = LibManifestPlugin;