-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
index.js
132 lines (120 loc) · 3.77 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
import { nanoid } from 'nanoid/non-secure'
import { readdir, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { SizeLimitError } from 'size-limit'
import { convertConfig } from './convert-config.js'
import { getConfig } from './get-config.js'
import { runWebpack } from './run-webpack.js'
const WEBPACK_EMPTY_PROJECT = 0
const WEBPACK_EMPTY_PROJECT_GZIP = 20
const WEBPACK_EMPTY_PROJECT_BROTLI = 1
const WEBPACK_EMPTY_PROJECT_IMPORT = 37
const WEBPACK_EMPTY_PROJECT_IMPORT_GZIP = 57
const WEBPACK_EMPTY_PROJECT_IMPORT_BROTLI = 41
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
}
}
async function loadConfig(config) {
return typeof config === 'function' ? config() : config
}
export default [
{
async before(config) {
if (config.saveBundle) {
if (config.cleanDir) {
await rm(config.saveBundle, { force: true, recursive: true })
} else {
let notEmpty = await isDirNotEmpty(config.saveBundle)
if (notEmpty) {
throw new SizeLimitError('bundleDirNotEmpty', config.saveBundle)
}
}
}
},
async finally(config, check) {
if (check.webpackOutput && !config.saveBundle) {
await rm(check.webpackOutput, { force: true, recursive: true })
}
},
name: '@size-limit/webpack',
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) {
let configModule = await import(check.config)
check.webpackConfig = await loadConfig(configModule.default)
convertConfig(check.webpackConfig, config.configPath)
} else {
check.webpackConfig = await getConfig(
config,
check,
check.webpackOutput
)
if (check.modifyWebpackConfig) {
check.webpackConfig = check.modifyWebpackConfig(check.webpackConfig)
}
}
},
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) {
if (check.gzip === true) {
check.size -= WEBPACK_EMPTY_PROJECT_IMPORT_GZIP
} else if (check.brotli === false) {
check.size -= WEBPACK_EMPTY_PROJECT_IMPORT
} else {
check.size -= WEBPACK_EMPTY_PROJECT_IMPORT_BROTLI
}
} else if (check.gzip === true) {
check.size -= WEBPACK_EMPTY_PROJECT_GZIP
} else if (check.brotli === false) {
check.size -= WEBPACK_EMPTY_PROJECT
} else {
check.size -= WEBPACK_EMPTY_PROJECT_BROTLI
}
}
},
wait40: 'Adding to empty webpack project'
}
]