webpack/lib/LibManifestPlugin.js

122 lines
3.2 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");
const { compareModulesById } = require("./util/comparators");
const { dirname, mkdirp } = require("./util/fs");
2017-04-05 20:39:23 +08:00
/** @typedef {import("./Compiler")} Compiler */
/**
* @typedef {Object} ManifestModuleData
* @property {string | number} id
* @property {Object} buildMeta
* @property {boolean | string[]} exports
*/
2017-04-05 20:39:23 +08:00
class LibManifestPlugin {
constructor(options) {
this.options = options;
}
/**
* @param {Compiler} compiler webpack compiler
* @returns {void}
*/
2017-04-05 20:39:23 +08:00
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.emit.tapAsync(
"LibManifestPlugin",
(compilation, callback) => {
const moduleGraph = compilation.moduleGraph;
2018-02-25 09:00:20 +08:00
asyncLib.forEach(
2018-09-06 22:59:11 +08:00
Array.from(compilation.chunks),
2018-02-25 09:00:20 +08:00
(chunk, callback) => {
if (!chunk.isOnlyInitial()) {
callback();
return;
}
const chunkGraph = compilation.chunkGraph;
2018-02-25 09:00:20 +08:00
const targetPath = compilation.getPath(this.options.path, {
chunk
});
const name =
this.options.name &&
compilation.getPath(this.options.name, {
chunk
2017-04-05 20:39:23 +08:00
});
2018-02-25 09:00:20 +08:00
const manifest = {
name,
type: this.options.type,
content: Array.from(
chunkGraph.getOrderedChunkModulesIterable(
chunk,
compareModulesById(chunkGraph)
),
module => {
if (
this.options.entryOnly &&
!moduleGraph
.getIncomingConnections(module)
.some(c => c.dependency instanceof EntryDependency)
) {
return;
}
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();
/** @type {ManifestModuleData} */
const data = {
id: chunkGraph.getModuleId(module),
buildMeta: module.buildMeta,
exports: Array.isArray(providedExports)
? providedExports
: undefined
};
return {
ident,
data
};
}
2018-05-22 19:24:40 +08:00
}
)
2018-02-25 09:00:20 +08:00
.filter(Boolean)
.reduce((obj, item) => {
obj[item.ident] = item.data;
return obj;
}, Object.create(null))
};
// Apply formatting to content if format flag is true;
const manifestContent = this.options.format
? JSON.stringify(manifest, null, 2)
: JSON.stringify(manifest);
const content = Buffer.from(manifestContent, "utf8");
mkdirp(
compiler.intermediateFileSystem,
dirname(compiler.intermediateFileSystem, targetPath),
err => {
if (err) return callback(err);
compiler.intermediateFileSystem.writeFile(
targetPath,
content,
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;