forked from ztoben/assets-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (85 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var merge = require('lodash.merge')
var getAssetKind = require('./lib/getAssetKind')
var isHMRUpdate = require('./lib/isHMRUpdate')
var isSourceMap = require('./lib/isSourceMap')
var createQueuedWriter = require('./lib/output/createQueuedWriter')
var createOutputWriter = require('./lib/output/createOutputWriter')
function AssetsWebpackPlugin (options) {
this.options = merge({}, {
path: '.',
filename: 'webpack-assets.json',
prettyPrint: false,
update: false,
fullPath: true
}, options)
this.writer = createQueuedWriter(createOutputWriter(this.options))
}
AssetsWebpackPlugin.prototype = {
constructor: AssetsWebpackPlugin,
apply: function (compiler) {
var self = this
compiler.plugin('after-emit', function (compilation, callback) {
var options = compiler.options
var stats = compilation.getStats().toJson({
hash: true,
publicPath: true,
assets: true,
chunks: false,
modules: false,
source: false,
errorDetails: false,
timings: false
})
// publicPath with resolved [hash] placeholder
var assetPath = (stats.publicPath && self.options.fullPath) ? stats.publicPath : ''
// assetsByChunkName contains a hash with the bundle names and the produced files
// e.g. { one: 'one-bundle.js', two: 'two-bundle.js' }
// in some cases (when using a plugin or source maps) it might contain an array of produced files
// e.g. {
// main:
// [ 'index-bundle-42b6e1ec4fa8c5f0303e.js',
// 'index-bundle-42b6e1ec4fa8c5f0303e.js.map' ]
// }
var assetsByChunkName = stats.assetsByChunkName
var output = Object.keys(assetsByChunkName).reduce(function (chunkMap, chunkName) {
var assets = assetsByChunkName[chunkName]
if (!Array.isArray(assets)) {
assets = [assets]
}
chunkMap[chunkName] = assets.reduce(function (typeMap, asset) {
if (isHMRUpdate(options, asset) || isSourceMap(options, asset)) {
return typeMap
}
var typeName = getAssetKind(options, asset)
typeMap[typeName] = assetPath + asset
return typeMap
}, {})
return chunkMap
}, {})
var manifestName = self.options.includeManifest === true ? 'manifest' : self.options.includeManifest
if (manifestName) {
var manifestEntry = output[manifestName]
if (manifestEntry) {
var manifestAssetKey = manifestEntry.js.substr(assetPath.length)
var parentSource = compilation.assets[manifestAssetKey]
var entryText = parentSource.source()
if (!entryText) {
throw new Error('Could not locate manifest function in source', parentSource)
}
// use _value if the uglify plugin was applied
manifestEntry.text = entryText
}
}
if (self.options.metadata) {
output.metadata = self.options.metadata
}
self.writer(output, function (err) {
if (err) {
compilation.errors.push(err)
}
callback()
})
})
}
}
module.exports = AssetsWebpackPlugin