|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var path = require("path"); |
6 | | -var async = require("async"); |
| 5 | +"use strict"; |
7 | 6 |
|
8 | | -function LibManifestPlugin(options) { |
9 | | - this.options = options; |
| 7 | +const path = require("path"); |
| 8 | +const asyncLib = require("async"); |
| 9 | + |
| 10 | +class LibManifestPlugin { |
| 11 | + constructor(options) { |
| 12 | + this.options = options; |
| 13 | + } |
| 14 | + |
| 15 | + apply(compiler) { |
| 16 | + compiler.plugin("emit", (compilation, callback) => { |
| 17 | + asyncLib.forEach(compilation.chunks, (chunk, callback) => { |
| 18 | + if(!chunk.isInitial()) { |
| 19 | + callback(); |
| 20 | + return; |
| 21 | + } |
| 22 | + const targetPath = compilation.getPath(this.options.path, { |
| 23 | + hash: compilation.hash, |
| 24 | + chunk |
| 25 | + }); |
| 26 | + const name = this.options.name && compilation.getPath(this.options.name, { |
| 27 | + hash: compilation.hash, |
| 28 | + chunk |
| 29 | + }); |
| 30 | + const manifest = { |
| 31 | + name, |
| 32 | + type: this.options.type, |
| 33 | + content: chunk.modules.reduce((obj, module) => { |
| 34 | + if(module.libIdent) { |
| 35 | + const ident = module.libIdent({ |
| 36 | + context: this.options.context || compiler.options.context |
| 37 | + }); |
| 38 | + if(ident) { |
| 39 | + obj[ident] = { |
| 40 | + id: module.id, |
| 41 | + meta: module.meta, |
| 42 | + exports: Array.isArray(module.providedExports) ? module.providedExports : undefined |
| 43 | + }; |
| 44 | + } |
| 45 | + } |
| 46 | + return obj; |
| 47 | + }, {}) |
| 48 | + }; |
| 49 | + const content = new Buffer(JSON.stringify(manifest, null, 2), "utf8"); //eslint-disable-line |
| 50 | + compiler.outputFileSystem.mkdirp(path.dirname(targetPath), err => { |
| 51 | + if(err) return callback(err); |
| 52 | + compiler.outputFileSystem.writeFile(targetPath, content, callback); |
| 53 | + }); |
| 54 | + }, callback); |
| 55 | + }); |
| 56 | + } |
10 | 57 | } |
11 | 58 | module.exports = LibManifestPlugin; |
12 | | -LibManifestPlugin.prototype.apply = function(compiler) { |
13 | | - compiler.plugin("emit", function(compilation, callback) { |
14 | | - async.forEach(compilation.chunks, function(chunk, callback) { |
15 | | - if(!chunk.isInitial()) { |
16 | | - callback(); |
17 | | - return; |
18 | | - } |
19 | | - var targetPath = compilation.getPath(this.options.path, { |
20 | | - hash: compilation.hash, |
21 | | - chunk: chunk |
22 | | - }); |
23 | | - var name = this.options.name && compilation.getPath(this.options.name, { |
24 | | - hash: compilation.hash, |
25 | | - chunk: chunk |
26 | | - }); |
27 | | - var manifest = { |
28 | | - name: name, |
29 | | - type: this.options.type, |
30 | | - content: chunk.modules.reduce(function(obj, module) { |
31 | | - if(module.libIdent) { |
32 | | - var ident = module.libIdent({ |
33 | | - context: this.options.context || compiler.options.context |
34 | | - }); |
35 | | - if(ident) { |
36 | | - obj[ident] = { |
37 | | - id: module.id, |
38 | | - meta: module.meta, |
39 | | - exports: Array.isArray(module.providedExports) ? module.providedExports : undefined |
40 | | - }; |
41 | | - } |
42 | | - } |
43 | | - return obj; |
44 | | - }.bind(this), {}) |
45 | | - }; |
46 | | - var content = new Buffer(JSON.stringify(manifest, null, 2), "utf8"); //eslint-disable-line |
47 | | - compiler.outputFileSystem.mkdirp(path.dirname(targetPath), function(err) { |
48 | | - if(err) return callback(err); |
49 | | - compiler.outputFileSystem.writeFile(targetPath, content, callback); |
50 | | - }); |
51 | | - }.bind(this), callback); |
52 | | - }.bind(this)); |
53 | | -}; |
|
0 commit comments