Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (51 loc) · 2.02 KB
/
Copy pathindex.js
File metadata and controls
57 lines (51 loc) · 2.02 KB
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
const path = require('path')
const jsonFormat = require('json-format')
const loaderUtils = require('loader-utils')
class EmitJsonFileWebpackPlugin {
constructor(files, options) {
if (!Array.isArray(files) && typeof files !== 'function') {
throw new Error('[emit-json-file-webpack-plugin] arguments must be an array or function that returns an array')
}
this.files = files || []
this.options = options || {}
}
apply(compiler) {
const files = ( typeof this.files === 'function' ) ? this.files() : this.files
const filename = this.options.filename || '[name].json'
compiler.plugin('emit', function(compilation, cb) {
files.forEach(file => {
if (!file.content || (!file.path && !file.linkedTo)) {
compilation.errors.push(new Error(`[emit-json-file-webpack-plugin] file arguments must contain 'content' and either 'path' or 'linkedTo'`))
return
}
const formatted = jsonFormat(file.content)
const linkedTo = file.linkedTo || false;
if ( linkedTo ) {
compilation.chunks.forEach( chunk => {
chunk.forEachModule(function(module) {
module.fileDependencies.forEach(function(filepath) {
if ( filepath == linkedTo ) {
const getPath = format => compilation.getPath(format, {
chunk,
}).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function () {
return loaderUtils.getHashDigest(formatted, arguments[1], arguments[2], parseInt(arguments[3], 10));
})
file.path = getPath(filename)
if (!chunk.files.includes(file.path)) {
chunk.files.push(file.path)
}
}
})
})
})
}
compilation.assets[file.path] = {
size: () => formatted.length,
source: () => formatted
}
})
cb()
})
}
}
module.exports = EmitJsonFileWebpackPlugin