webpack/lib/LibManifestPlugin.js

59 lines
1.5 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
*/
2017-04-05 20:39:23 +08:00
"use strict";
2015-05-17 00:27:59 +08:00
2017-04-05 20:39:23 +08:00
const path = require("path");
2017-04-05 21:03:47 +08:00
const asyncLib = require("async");
2017-04-05 20:39:23 +08:00
class LibManifestPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.plugin("emit", (compilation, callback) => {
2017-04-05 21:03:47 +08:00
asyncLib.forEach(compilation.chunks, (chunk, callback) => {
2017-04-05 20:39:23 +08:00
if(!chunk.isInitial()) {
callback();
return;
}
const targetPath = compilation.getPath(this.options.path, {
hash: compilation.hash,
chunk
});
const name = this.options.name && compilation.getPath(this.options.name, {
hash: compilation.hash,
chunk
});
const manifest = {
name,
type: this.options.type,
content: chunk.modules.reduce((obj, module) => {
if(module.libIdent) {
const ident = module.libIdent({
context: this.options.context || compiler.options.context
});
if(ident) {
obj[ident] = {
id: module.id,
meta: module.meta,
exports: Array.isArray(module.providedExports) ? module.providedExports : undefined
};
}
}
return obj;
}, {})
};
2017-04-29 03:34:38 +08:00
const content = new Buffer(JSON.stringify(manifest), "utf8"); //eslint-disable-line
2017-04-05 20:39:23 +08:00
compiler.outputFileSystem.mkdirp(path.dirname(targetPath), err => {
if(err) return callback(err);
compiler.outputFileSystem.writeFile(targetPath, content, callback);
});
}, callback);
});
}
2015-05-17 00:27:59 +08:00
}
module.exports = LibManifestPlugin;