-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathindex.js
216 lines (181 loc) · 7.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const getAssetKind = require('./lib/getAssetKind')
const isHMRUpdate = require('./lib/isHMRUpdate')
const isSourceMap = require('./lib/isSourceMap')
const getDynamicImportedChildAssets = require('./lib/getDynamicImportedChildAssets')
const createQueuedWriter = require('./lib/output/createQueuedWriter')
const createOutputWriter = require('./lib/output/createOutputWriter')
function AssetsWebpackPlugin (options) {
this.options = _.merge({}, {
filename: 'webpack-assets.json',
prettyPrint: false,
update: false,
fullPath: true,
manifestFirst: true,
useCompilerPath: false,
fileTypes: ['js', 'css'],
includeAllFileTypes: true,
includeFilesWithoutChunk: false,
includeAuxiliaryAssets: false,
includeDynamicImportedAssets: false,
keepInMemory: false,
integrity: false,
removeFullPathAutoPrefix: false
}, options)
}
AssetsWebpackPlugin.prototype = {
constructor: AssetsWebpackPlugin,
apply: function (compiler) {
const self = this
self.options.path = path.resolve(
self.options.useCompilerPath
? (compiler.options.output.path || '.')
: (self.options.path || '.')
)
self.writer = createQueuedWriter(createOutputWriter(self.options))
const emitPlugin = (compilation, callback) => {
const options = compiler.options
const stats = compilation.getStats().toJson({
hash: true,
publicPath: true,
assets: true,
chunks: false,
modules: false,
source: false,
errorDetails: false,
timings: false
})
let 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' ]
// }
// starting with webpack 5, the public path is automatically determined when possible and the path is prefaced
// with `/auto/`, the `removeAutoPrefix` option can be set to turn this off
if (self.options.removeFullPathAutoPrefix) {
if (assetPath.startsWith('auto')) {
assetPath = assetPath.substring(4)
}
}
const seenAssets = {}
let chunks
if (self.options.entrypoints) {
chunks = Object.keys(stats.entrypoints)
if (self.options.includeFilesWithoutChunk) {
chunks.push('') // push "unnamed" chunk
}
} else {
chunks = Object.keys(stats.assetsByChunkName)
chunks.push('') // push "unnamed" chunk
}
const output = chunks.reduce(function (chunkMap, chunkName) {
let assets
if (self.options.entrypoints) {
assets = chunkName ? stats.entrypoints[chunkName].assets : stats.assets
} else {
assets = chunkName ? stats.assetsByChunkName[chunkName] : stats.assets
}
if (self.options.includeAuxiliaryAssets && chunkName && stats.entrypoints[chunkName].auxiliaryAssets) {
assets = [...assets, ...stats.entrypoints[chunkName].auxiliaryAssets]
}
if (self.options.includeDynamicImportedAssets && chunkName && stats.entrypoints[chunkName].children) {
const dynamicImportedChildAssets = getDynamicImportedChildAssets(options, stats.entrypoints[chunkName].children)
assets = [...assets, ...dynamicImportedChildAssets]
}
if (!Array.isArray(assets)) {
assets = [assets]
}
let added = false
const typeMap = assets.reduce(function (typeMap, obj) {
const asset = obj.name || obj
if (isHMRUpdate(options, asset) || isSourceMap(options, asset) || (!chunkName && seenAssets[asset])) {
return typeMap
}
const typeName = getAssetKind(options, asset)
if (self.options.includeAllFileTypes || self.options.fileTypes.includes(typeName)) {
const combinedPath = assetPath && assetPath.slice(-1) !== '/' ? `${assetPath}/${asset}` : assetPath + asset
const type = typeof typeMap[typeName]
const compilationAsset = compilation.assets[asset]
const integrity = compilationAsset && compilationAsset.integrity
const loadingBehavior = obj.loadingBehavior
if (type === 'undefined') {
typeMap[typeName] = combinedPath
if (self.options.integrity && integrity) {
typeMap[typeName + 'Integrity'] = integrity
}
} else {
if (type === 'string') {
typeMap[typeName] = [typeMap[typeName]]
}
if (self.options.includeDynamicImportedAssets && loadingBehavior) {
const typeNameWithLoadingBehavior = typeName + ':' + loadingBehavior
typeMap[typeNameWithLoadingBehavior] = typeMap[typeNameWithLoadingBehavior] || []
typeMap[typeNameWithLoadingBehavior].push(combinedPath)
} else {
typeMap[typeName].push(combinedPath)
}
}
added = true
seenAssets[asset] = true
}
return typeMap
}, {})
if (added) {
chunkMap[chunkName] = typeMap
}
return chunkMap
}, {})
let manifestNames = self.options.includeManifest === true ? ['manifest'] : self.options.includeManifest
if (typeof manifestNames === 'string') {
manifestNames = [manifestNames]
}
if (manifestNames) {
for (let i = 0; i < manifestNames.length; i++) {
const manifestName = manifestNames[i]
const manifestEntry = output[manifestName]
if (manifestEntry) {
let js = manifestEntry.js || manifestEntry.mjs
if (!Array.isArray(js)) {
js = [js]
}
const manifestAssetKey = js[js.length - 1].substr(assetPath.length)
const parentSource = compilation.assets[manifestAssetKey]
const entryText = parentSource.source()
if (!entryText) {
throw new Error('Could not locate manifest function in source', parentSource)
}
manifestEntry.text = entryText
}
}
}
if (self.options.metadata) {
output.metadata = self.options.metadata
}
if (!compiler.outputFileSystem.readFile) {
compiler.outputFileSystem.readFile = fs.readFile.bind(fs)
}
if (!compiler.outputFileSystem.join) {
compiler.outputFileSystem.join = path.join.bind(path)
}
self.writer(compiler.outputFileSystem, output, function (err) {
if (err) {
compilation.errors.push(err)
}
callback()
})
}
if (compiler.hooks) {
const plugin = { name: 'AssetsWebpackPlugin' }
compiler.hooks.emit.tapAsync(plugin, emitPlugin)
} else {
compiler.plugin('after-emit', emitPlugin)
}
}
}
module.exports = AssetsWebpackPlugin