-
-
Notifications
You must be signed in to change notification settings - Fork 381
/
index.js
91 lines (77 loc) · 2.15 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
const nodePath = require('path')
const fs = require('fs')
const makeDir = require('make-dir')
class LoadablePlugin {
constructor({
filename = 'loadable-stats.json',
path,
writeToDisk,
outputAsset = true,
} = {}) {
this.opts = { filename, writeToDisk, outputAsset, path }
// The Webpack compiler instance
this.compiler = null
}
handleEmit = (hookCompiler, callback) => {
const stats = hookCompiler.getStats().toJson({
hash: true,
publicPath: true,
assets: true,
chunks: false,
modules: false,
source: false,
errorDetails: false,
timings: false,
})
const result = JSON.stringify(stats, null, 2)
if (this.opts.outputAsset) {
hookCompiler.assets[this.opts.filename] = {
source() {
return result
},
size() {
return result.length
},
}
}
if (this.opts.writeToDisk) {
this.writeAssetsFile(result)
}
callback()
}
/**
* Write Assets Manifest file
* @method writeAssetsFile
*/
writeAssetsFile = manifest => {
const outputFolder =
this.opts.writeToDisk.filename || this.compiler.options.output.path
const outputFile = nodePath.resolve(outputFolder, this.opts.filename)
try {
if (!fs.existsSync(outputFolder)) {
makeDir.sync(outputFolder)
}
} catch (err) {
if (err.code !== 'EEXIST') {
throw err
}
}
fs.writeFileSync(outputFile, manifest)
}
apply(compiler) {
this.compiler = compiler
// Check if webpack version 4 or 5
if ('jsonpFunction' in compiler.options.output) {
// Add a custom output.jsonpFunction: __LOADABLE_LOADED_CHUNKS__
compiler.options.output.jsonpFunction = '__LOADABLE_LOADED_CHUNKS__'
} else {
// Add a custom output.chunkLoadingGlobal: __LOADABLE_LOADED_CHUNKS__
compiler.options.output.chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__'
}
if (this.opts.outputAsset || this.opts.writeToDisk) {
compiler.hooks.emit.tapAsync('@loadable/webpack-plugin', this.handleEmit)
}
}
}
module.exports = LoadablePlugin
module.exports.default = LoadablePlugin