-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathindex.js
144 lines (124 loc) · 3.57 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
const nodePath = require('path')
const fs = require('fs')
const makeDir = require('make-dir')
const name = '@loadable/webpack-plugin'
class LoadablePlugin {
constructor({
filename = 'loadable-stats.json',
path,
writeToDisk,
outputAsset = true,
chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__',
} = {}) {
this.opts = { filename, writeToDisk, outputAsset, path, chunkLoadingGlobal }
// The Webpack compiler instance
this.compiler = null
}
handleEmit = compilation => {
const stats = compilation.getStats().toJson({
all: false,
assets: true,
cachedAssets: true,
chunks: false,
chunkGroups: true,
chunkGroupChildren: true,
hash: true,
ids: true,
outputPath: true,
publicPath: true,
})
stats.generator = 'loadable-components'
// we don't need all chunk information, only a type
stats.chunks = [...compilation.chunks].map(chunk => {
return {
id: chunk.id,
files: [...chunk.files],
}
})
// update namedChunkGroups with integrity from webpack-subresource-integrity if available
Object.values(stats.namedChunkGroups).forEach(namedChunkGroup => {
namedChunkGroup.assets.forEach(namedChunkGroupAsset => {
if (!namedChunkGroupAsset.integrity) {
const asset =
stats.assets.find(a => a.name === namedChunkGroupAsset.name) || {}
if (asset.integrity) {
namedChunkGroupAsset.integrity = asset.integrity
}
}
})
})
const result = JSON.stringify(stats, null, 2)
if (this.opts.writeToDisk) {
this.writeAssetsFile(result)
}
if (this.opts.outputAsset) {
return {
source() {
return result
},
size() {
return result.length
},
}
}
return null
}
/**
* 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
const version = 'jsonpFunction' in compiler.options.output ? 4 : 5
// Add a custom chunk loading callback
if (version === 4) {
compiler.options.output.jsonpFunction = this.opts.chunkLoadingGlobal
} else {
compiler.options.output.chunkLoadingGlobal = this.opts.chunkLoadingGlobal
}
if (this.opts.outputAsset || this.opts.writeToDisk) {
if (version === 4) {
// webpack 4
compiler.hooks.emit.tap(name, compilation => {
const asset = this.handleEmit(compilation)
if (asset) {
compilation.assets[this.opts.filename] = asset
}
})
} else {
// webpack 5
compiler.hooks.make.tap(name, compilation => {
compilation.hooks.processAssets.tap(
{
name,
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
() => {
const asset = this.handleEmit(compilation)
if (asset) {
compilation.emitAsset(this.opts.filename, asset)
}
},
)
})
}
}
}
}
module.exports = LoadablePlugin
module.exports.default = LoadablePlugin