-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
index.js
116 lines (104 loc) · 3.14 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
let SizeLimitError = require('size-limit/size-limit-error')
let { readdir } = require('fs').promises
let { nanoid } = require('nanoid/non-secure')
let { tmpdir } = require('os')
let { join } = require('path')
let rm = require('size-limit/rm')
let convertConfig = require('./convert-config')
let runWebpack = require('./run-webpack')
let getConfig = require('./get-config')
const WEBPACK_EMPTY_PROJECT = 0
const WEBPACK_EMPTY_PROJECT_GZIP = 20
const WEBPACK_EMPTY_PROJECT_IMPORT = 37
const WEBPACK_EMPTY_PROJECT_IMPORT_GZIP = 57
function getFiles(stats, check) {
let entries = {}
if (check.entry) {
for (let i of check.entry) {
if (stats.entrypoints[i]) {
entries[i] = stats.entrypoints[i]
} else {
throw new SizeLimitError('unknownEntry', i)
}
}
} else {
entries = stats.entrypoints
}
return Object.keys(entries)
.reduce((assets, i) => assets.concat(entries[i].assets), [])
.map(({ name }) => {
if (check.webpackConfig.output && check.webpackConfig.output.path) {
return join(check.webpackConfig.output.path, name)
} else {
return join(process.cwd(), 'dist', name)
}
})
}
async function isDirNotEmpty(dir) {
try {
let files = await readdir(dir)
return !!files.length
} catch (e) {
if (e.code === 'ENOENT') return false
throw e
}
}
let self = {
name: '@size-limit/webpack',
async before(config) {
if (config.saveBundle) {
if (config.cleanDir) {
await rm(config.saveBundle)
} else {
let notEmpty = await isDirNotEmpty(config.saveBundle)
if (notEmpty) {
throw new SizeLimitError('bundleDirNotEmpty', config.saveBundle)
}
}
}
},
async step20(config, check) {
if (check.webpack === false) return
check.webpackOutput = config.saveBundle
if (!check.webpackOutput) {
check.webpackOutput = join(tmpdir(), `size-limit-${nanoid()}`)
}
if (check.config) {
check.webpackConfig = require(check.config)
convertConfig(check.webpackConfig, config.configPath)
} else {
check.webpackConfig = await getConfig(config, check, check.webpackOutput)
if (check.modifyWebpackConfig) {
check.webpackConfig = check.modifyWebpackConfig(check.webpackConfig)
}
}
},
wait40: 'Adding to empty webpack project',
async step40(config, check) {
if (check.webpackConfig && check.webpack !== false) {
check.bundles = getFiles(await runWebpack(check), check)
}
},
async step61(config, check) {
if (check.bundles) {
if (typeof check.size === 'undefined') {
throw new SizeLimitError('missedPlugin', 'file')
}
if (check.import && check.gzip === false) {
check.size -= WEBPACK_EMPTY_PROJECT_IMPORT
} else if (check.import) {
check.size -= WEBPACK_EMPTY_PROJECT_IMPORT_GZIP
} else if (check.gzip === false) {
check.size -= WEBPACK_EMPTY_PROJECT
} else {
check.size -= WEBPACK_EMPTY_PROJECT_GZIP
}
}
},
async finally(config, check) {
if (check.webpackOutput && !config.saveBundle) {
await rm(check.webpackOutput)
}
}
}
module.exports = [self]