| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const asyncLib = require("neo-async");
|
|---|
| 9 | const EntryDependency = require("../dependencies/EntryDependency");
|
|---|
| 10 | const { someInIterable } = require("../util/IterableHelpers");
|
|---|
| 11 | const { compareModulesById } = require("../util/comparators");
|
|---|
| 12 | const { dirname, mkdirp } = require("../util/fs");
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
|
|---|
| 15 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 16 | /** @typedef {import("../Compiler").IntermediateFileSystem} IntermediateFileSystem */
|
|---|
| 17 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
|---|
| 18 | /** @typedef {import("../ExportsInfo").ExportInfoName} ExportInfoName */
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * Defines the manifest module data type used by this module.
|
|---|
| 22 | * @typedef {object} ManifestModuleData
|
|---|
| 23 | * @property {ModuleId} id
|
|---|
| 24 | * @property {BuildMeta=} buildMeta
|
|---|
| 25 | * @property {ExportInfoName[]=} exports
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Defines the lib manifest plugin options type used by this module.
|
|---|
| 30 | * @typedef {object} LibManifestPluginOptions
|
|---|
| 31 | * @property {string=} context Context of requests in the manifest file (defaults to the webpack context).
|
|---|
| 32 | * @property {boolean=} entryOnly If true, only entry points will be exposed (default: true).
|
|---|
| 33 | * @property {boolean=} format If true, manifest json file (output) will be formatted.
|
|---|
| 34 | * @property {string=} name Name of the exposed dll function (external name, use value of 'output.library').
|
|---|
| 35 | * @property {string} path Absolute path to the manifest json file (output).
|
|---|
| 36 | * @property {string=} type Type of the dll bundle (external type, use value of 'output.libraryTarget').
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | const PLUGIN_NAME = "LibManifestPlugin";
|
|---|
| 40 |
|
|---|
| 41 | class LibManifestPlugin {
|
|---|
| 42 | /**
|
|---|
| 43 | * Creates an instance of LibManifestPlugin.
|
|---|
| 44 | * @param {LibManifestPluginOptions} options the options
|
|---|
| 45 | */
|
|---|
| 46 | constructor(options) {
|
|---|
| 47 | this.options = options;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 52 | * @param {Compiler} compiler the compiler instance
|
|---|
| 53 | * @returns {void}
|
|---|
| 54 | */
|
|---|
| 55 | apply(compiler) {
|
|---|
| 56 | compiler.hooks.emit.tapAsync(
|
|---|
| 57 | { name: PLUGIN_NAME, stage: 110 },
|
|---|
| 58 | (compilation, callback) => {
|
|---|
| 59 | const moduleGraph = compilation.moduleGraph;
|
|---|
| 60 | // store used paths to detect issue and output an error. #18200
|
|---|
| 61 | /** @type {Set<string>} */
|
|---|
| 62 | const usedPaths = new Set();
|
|---|
| 63 | asyncLib.each(
|
|---|
| 64 | [...compilation.chunks],
|
|---|
| 65 | (chunk, callback) => {
|
|---|
| 66 | if (!chunk.canBeInitial()) {
|
|---|
| 67 | callback();
|
|---|
| 68 | return;
|
|---|
| 69 | }
|
|---|
| 70 | const chunkGraph = compilation.chunkGraph;
|
|---|
| 71 | const targetPath = compilation.getPath(this.options.path, {
|
|---|
| 72 | chunk
|
|---|
| 73 | });
|
|---|
| 74 | if (usedPaths.has(targetPath)) {
|
|---|
| 75 | callback(new Error("each chunk must have a unique path"));
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 | usedPaths.add(targetPath);
|
|---|
| 79 | const name =
|
|---|
| 80 | this.options.name &&
|
|---|
| 81 | compilation.getPath(this.options.name, {
|
|---|
| 82 | chunk,
|
|---|
| 83 | contentHashType: "javascript"
|
|---|
| 84 | });
|
|---|
| 85 | const content = Object.create(null);
|
|---|
| 86 | for (const module of chunkGraph.getOrderedChunkModulesIterable(
|
|---|
| 87 | chunk,
|
|---|
| 88 | compareModulesById(chunkGraph)
|
|---|
| 89 | )) {
|
|---|
| 90 | if (
|
|---|
| 91 | this.options.entryOnly &&
|
|---|
| 92 | !someInIterable(
|
|---|
| 93 | moduleGraph.getIncomingConnections(module),
|
|---|
| 94 | (c) => c.dependency instanceof EntryDependency
|
|---|
| 95 | )
|
|---|
| 96 | ) {
|
|---|
| 97 | continue;
|
|---|
| 98 | }
|
|---|
| 99 | const ident = module.libIdent({
|
|---|
| 100 | context: this.options.context || compiler.context,
|
|---|
| 101 | associatedObjectForCache: compiler.root
|
|---|
| 102 | });
|
|---|
| 103 | if (ident) {
|
|---|
| 104 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
|---|
| 105 | const providedExports = exportsInfo.getProvidedExports();
|
|---|
| 106 | /** @type {ManifestModuleData} */
|
|---|
| 107 | const data = {
|
|---|
| 108 | id: /** @type {ModuleId} */ (chunkGraph.getModuleId(module)),
|
|---|
| 109 | buildMeta: /** @type {BuildMeta} */ (module.buildMeta),
|
|---|
| 110 | exports: Array.isArray(providedExports)
|
|---|
| 111 | ? providedExports
|
|---|
| 112 | : undefined
|
|---|
| 113 | };
|
|---|
| 114 | content[ident] = data;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | const manifest = {
|
|---|
| 118 | name,
|
|---|
| 119 | type: this.options.type,
|
|---|
| 120 | content
|
|---|
| 121 | };
|
|---|
| 122 | // Apply formatting to content if format flag is true;
|
|---|
| 123 | const manifestContent = this.options.format
|
|---|
| 124 | ? JSON.stringify(manifest, null, 2)
|
|---|
| 125 | : JSON.stringify(manifest);
|
|---|
| 126 | const buffer = Buffer.from(manifestContent, "utf8");
|
|---|
| 127 | const intermediateFileSystem =
|
|---|
| 128 | /** @type {IntermediateFileSystem} */ (
|
|---|
| 129 | compiler.intermediateFileSystem
|
|---|
| 130 | );
|
|---|
| 131 | mkdirp(
|
|---|
| 132 | intermediateFileSystem,
|
|---|
| 133 | dirname(intermediateFileSystem, targetPath),
|
|---|
| 134 | (err) => {
|
|---|
| 135 | if (err) return callback(err);
|
|---|
| 136 | intermediateFileSystem.writeFile(targetPath, buffer, callback);
|
|---|
| 137 | }
|
|---|
| 138 | );
|
|---|
| 139 | },
|
|---|
| 140 | callback
|
|---|
| 141 | );
|
|---|
| 142 | }
|
|---|
| 143 | );
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | module.exports = LibManifestPlugin;
|
|---|