|
1 |
| -/* |
2 |
| - * File is originally a direct copy from https://github.com/danethurber/webpack-manifest-plugin |
3 |
| - * at version 1.1.0. |
4 |
| - * |
5 |
| - * The library is licensed as MIT. |
6 |
| - * |
7 |
| - * There was then one change made in the code below for |
8 |
| - * this library. |
9 |
| - */ |
10 |
| - |
11 |
| -var path = require('path'); |
12 |
| -var fse = require('fs-extra'); |
13 |
| -var _ = require('lodash'); |
14 |
| - |
15 |
| -function ManifestPlugin(opts) { |
16 |
| - this.opts = _.assign({ |
17 |
| - basePath: '', |
18 |
| - publicPath: '', |
19 |
| - fileName: 'manifest.json', |
20 |
| - stripSrc: null, |
21 |
| - transformExtensions: /^(gz|map)$/i, |
22 |
| - writeToFileEmit: false, |
23 |
| - cache: null |
24 |
| - }, opts || {}); |
25 |
| -} |
26 |
| - |
27 |
| -ManifestPlugin.prototype.getFileType = function(str) { |
28 |
| - str = str.replace(/\?.*/, ''); |
29 |
| - var split = str.split('.'); |
30 |
| - var ext = split.pop(); |
31 |
| - if (this.opts.transformExtensions.test(ext)) { |
32 |
| - ext = split.pop() + '.' + ext; |
33 |
| - } |
34 |
| - return ext; |
35 |
| -}; |
36 |
| - |
37 |
| -ManifestPlugin.prototype.apply = function(compiler) { |
38 |
| - var outputName = this.opts.fileName; |
39 |
| - var cache = this.opts.cache || {}; |
40 |
| - var moduleAssets = {}; |
41 |
| - |
42 |
| - compiler.plugin("compilation", function (compilation) { |
43 |
| - compilation.plugin('module-asset', function (module, file) { |
44 |
| - moduleAssets[file] = path.join( |
45 |
| - path.dirname(file), |
46 |
| - path.basename(module.userRequest) |
47 |
| - ); |
48 |
| - /* *** MODIFICATION START *** */ |
49 |
| - // for Windows, we want the keys to use /, not \ |
50 |
| - // (and path.join will obviously use \ in Windows) |
51 |
| - if (process.platform === 'win32') { |
52 |
| - moduleAssets[file] = moduleAssets[file].replace(/\\/g, '/'); |
53 |
| - } |
54 |
| - /* *** MODIFICATION END *** */ |
55 |
| - }); |
56 |
| - }); |
57 |
| - |
58 |
| - compiler.plugin('emit', function(compilation, compileCallback) { |
59 |
| - var stats = compilation.getStats().toJson(); |
60 |
| - var manifest = {}; |
61 |
| - |
62 |
| - _.merge(manifest, compilation.chunks.reduce(function(memo, chunk) { |
63 |
| - var chunkName = chunk.name ? chunk.name.replace(this.opts.stripSrc, '') : null; |
64 |
| - |
65 |
| - // Map original chunk name to output files. |
66 |
| - // For nameless chunks, just map the files directly. |
67 |
| - return chunk.files.reduce(function(memo, file) { |
68 |
| - // Don't add hot updates to manifest |
69 |
| - if (file.indexOf('hot-update') >= 0) { |
70 |
| - return memo; |
71 |
| - } |
72 |
| - if (chunkName) { |
73 |
| - memo[chunkName + '.' + this.getFileType(file)] = file; |
74 |
| - } else { |
75 |
| - memo[file] = file; |
76 |
| - } |
77 |
| - return memo; |
78 |
| - }.bind(this), memo); |
79 |
| - }.bind(this), {})); |
80 |
| - |
81 |
| - // module assets don't show up in assetsByChunkName. |
82 |
| - // we're getting them this way; |
83 |
| - _.merge(manifest, stats.assets.reduce(function(memo, asset) { |
84 |
| - var name = moduleAssets[asset.name]; |
85 |
| - if (name) { |
86 |
| - memo[name] = asset.name; |
87 |
| - } |
88 |
| - return memo; |
89 |
| - }, {})); |
90 |
| - |
91 |
| - /* *** MODIFICATION START *** */ |
92 |
| - if (this.opts.basePath && this.opts.publicPath) { |
93 |
| - // A modification made to the plugin for Encore |
94 |
| - manifest = _.reduce(manifest, function(memo, value, key) { |
95 |
| - memo[this.opts.basePath + key] = this.opts.publicPath + value; |
96 |
| - return memo; |
97 |
| - }.bind(this), {}); |
98 |
| - /* *** MODIFICATION END *** */ |
99 |
| - } else if (this.opts.basePath) { |
100 |
| - // Append optional basepath onto all references. |
101 |
| - // This allows output path to be reflected in the manifest. |
102 |
| - manifest = _.reduce(manifest, function(memo, value, key) { |
103 |
| - memo[this.opts.basePath + key] = this.opts.basePath + value; |
104 |
| - return memo; |
105 |
| - }.bind(this), {}); |
106 |
| - } else if (this.opts.publicPath) { |
107 |
| - // Similar to basePath but only affects the value (similar to how |
108 |
| - // output.publicPath turns require('foo/bar') into '/public/foo/bar', see |
109 |
| - // https://github.com/webpack/docs/wiki/configuration#outputpublicpath |
110 |
| - manifest = _.reduce(manifest, function(memo, value, key) { |
111 |
| - memo[key] = this.opts.publicPath + value; |
112 |
| - return memo; |
113 |
| - }.bind(this), {}); |
114 |
| - } |
115 |
| - |
116 |
| - Object.keys(manifest).sort().forEach(function(key) { |
117 |
| - cache[key] = manifest[key]; |
118 |
| - }); |
119 |
| - |
120 |
| - var json = JSON.stringify(cache, null, 2); |
121 |
| - |
122 |
| - compilation.assets[outputName] = { |
123 |
| - source: function() { |
124 |
| - return json; |
125 |
| - }, |
126 |
| - size: function() { |
127 |
| - return json.length; |
128 |
| - } |
129 |
| - }; |
130 |
| - |
131 |
| - if (this.opts.writeToFileEmit) { |
132 |
| - var outputFolder = compilation.options.output.path; |
133 |
| - var outputFile = path.join(outputFolder, this.opts.fileName); |
134 |
| - |
135 |
| - fse.outputFileSync(outputFile, json); |
136 |
| - } |
137 |
| - |
138 |
| - compileCallback(); |
139 |
| - }.bind(this)); |
140 |
| -}; |
141 |
| - |
142 |
| -module.exports = ManifestPlugin; |
| 1 | +module.exports = require('webpack-manifest-plugin'); |
0 commit comments