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");
|
2018-07-30 23:08:51 +08:00
|
|
|
const path = require("path");
|
2018-08-14 22:40:37 +08:00
|
|
|
const EntryDependency = require("./dependencies/EntryDependency");
|
2018-08-14 17:18:22 +08:00
|
|
|
const { compareModulesById } = require("./util/comparators");
|
2017-04-05 20:39:23 +08:00
|
|
|
|
2018-08-23 01:23:48 +08:00
|
|
|
/** @typedef {import("./Compiler")} Compiler */
|
|
|
|
|
2017-04-05 20:39:23 +08:00
|
|
|
class LibManifestPlugin {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:23:48 +08:00
|
|
|
/**
|
|
|
|
* @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) => {
|
2018-08-16 22:11:20 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-08-14 17:18:22 +08:00
|
|
|
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,
|
2018-08-14 17:18:22 +08:00
|
|
|
content: Array.from(
|
|
|
|
chunkGraph.getOrderedChunkModulesIterable(
|
|
|
|
chunk,
|
2018-08-28 17:50:33 +08:00
|
|
|
compareModulesById(chunkGraph)
|
2018-08-14 17:18:22 +08:00
|
|
|
),
|
|
|
|
module => {
|
|
|
|
if (
|
|
|
|
this.options.entryOnly &&
|
2018-08-22 17:49:27 +08:00
|
|
|
!moduleGraph
|
2018-08-14 17:18:22 +08:00
|
|
|
.getIncomingConnections(module)
|
2018-08-14 22:40:37 +08:00
|
|
|
.some(c => c.dependency instanceof EntryDependency)
|
2018-08-14 17:18:22 +08:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ident = module.libIdent({
|
|
|
|
context: this.options.context || compiler.options.context
|
|
|
|
});
|
|
|
|
if (ident) {
|
|
|
|
return {
|
|
|
|
ident,
|
|
|
|
data: {
|
2018-08-28 17:56:48 +08:00
|
|
|
id: chunkGraph.getModuleId(module),
|
2018-08-14 17:18:22 +08:00
|
|
|
buildMeta: module.buildMeta
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-05-22 19:24:40 +08:00
|
|
|
}
|
2018-08-14 17:18:22 +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))
|
|
|
|
};
|
2018-10-03 23:48:29 +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 content = Buffer.from(manifestContent, "utf8");
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.outputFileSystem.mkdirp(path.dirname(targetPath), err => {
|
|
|
|
if (err) return callback(err);
|
|
|
|
compiler.outputFileSystem.writeFile(
|
|
|
|
targetPath,
|
|
|
|
content,
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2017-04-05 20:39:23 +08:00
|
|
|
}
|
2015-05-17 00:27:59 +08:00
|
|
|
}
|
|
|
|
module.exports = LibManifestPlugin;
|