123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- "use strict";
- const asyncLib = require("neo-async");
- const EntryDependency = require("./dependencies/EntryDependency");
- const { someInIterable } = require("./util/IterableHelpers");
- const { compareModulesById } = require("./util/comparators");
- const { dirname, mkdirp } = require("./util/fs");
- class LibManifestPlugin {
-
- constructor(options) {
- this.options = options;
- }
-
- apply(compiler) {
- compiler.hooks.emit.tapAsync(
- {
- name: "LibManifestPlugin",
- stage: 110
- },
- (compilation, callback) => {
- const moduleGraph = compilation.moduleGraph;
-
- const usedPaths = new Set();
- asyncLib.forEach(
- Array.from(compilation.chunks),
- (chunk, callback) => {
- if (!chunk.canBeInitial()) {
- callback();
- return;
- }
- const chunkGraph = compilation.chunkGraph;
- const targetPath = compilation.getPath(this.options.path, {
- chunk
- });
- if (usedPaths.has(targetPath)) {
- callback(new Error(`each chunk must have a unique path`));
- return;
- }
- usedPaths.add(targetPath);
- const name =
- this.options.name &&
- compilation.getPath(this.options.name, {
- chunk,
- contentHashType: "javascript"
- });
- 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({
- context:
- this.options.context ||
- (compiler.options.context),
- associatedObjectForCache: compiler.root
- });
- if (ident) {
- const exportsInfo = moduleGraph.getExportsInfo(module);
- const providedExports = exportsInfo.getProvidedExports();
-
- const data = {
- id: chunkGraph.getModuleId(module),
- buildMeta: (module.buildMeta),
- exports: Array.isArray(providedExports)
- ? providedExports
- : undefined
- };
- content[ident] = data;
- }
- }
- const manifest = {
- name,
- type: this.options.type,
- content
- };
-
- const manifestContent = this.options.format
- ? JSON.stringify(manifest, null, 2)
- : JSON.stringify(manifest);
- const buffer = Buffer.from(manifestContent, "utf8");
- const intermediateFileSystem =
- (
- compiler.intermediateFileSystem
- );
- mkdirp(
- intermediateFileSystem,
- dirname(intermediateFileSystem, targetPath),
- err => {
- if (err) return callback(err);
- intermediateFileSystem.writeFile(targetPath, buffer, callback);
- }
- );
- },
- callback
- );
- }
- );
- }
- }
- module.exports = LibManifestPlugin;
|