-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
get-config.js
215 lines (197 loc) · 6.08 KB
/
get-config.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import bytes from 'bytes-iec'
import { globby } from 'globby'
import { lilconfig } from 'lilconfig'
import { createRequire } from 'node:module'
import { dirname, isAbsolute, join, relative } from 'node:path'
import { fileURLToPath } from 'node:url'
import { SizeLimitError } from './size-limit-error.js'
const require = createRequire(import.meta.url)
let OPTIONS = {
brotli: 'file',
compareWith: 'webpack',
config: ['webpack', 'esbuild'],
disableModuleConcatenation: 'webpack',
entry: 'webpack',
gzip: 'file',
hidePassed: false,
highlightLess: false,
ignore: ['webpack', 'esbuild'],
import: ['webpack', 'esbuild'],
limit: true,
modifyEsbuildConfig: 'esbuild',
modifyWebpackConfig: 'webpack',
module: true,
name: true,
path: true,
running: 'time',
uiReports: 'webpack',
webpack: 'webpack'
}
function isStrings(value) {
if (!Array.isArray(value)) return false
return value.every(i => typeof i === 'string')
}
function isStringsOrUndefined(value) {
let type = typeof value
return type === 'undefined' || type === 'string' || isStrings(value)
}
function checkChecks(plugins, checks) {
if (!Array.isArray(checks)) {
throw new SizeLimitError('noArrayConfig')
}
if (checks.length === 0) {
throw new SizeLimitError('emptyConfig')
}
for (let check of checks) {
if (typeof check !== 'object') {
throw new SizeLimitError('noObjectCheck')
}
if (!isStringsOrUndefined(check.path)) {
throw new SizeLimitError('pathNotString')
}
if (!isStringsOrUndefined(check.entry)) {
throw new SizeLimitError('entryNotString')
}
for (let opt in check) {
let available = OPTIONS[opt]
if (typeof available === 'string') {
if (!plugins.has(available)) {
throw new SizeLimitError('pluginlessConfig', opt, available)
}
} else if (Array.isArray(available)) {
if (available.every(i => !plugins.has(i))) {
throw new SizeLimitError('multiPluginlessConfig', opt, ...available)
}
} else if (available !== true) {
throw new SizeLimitError('unknownOption', opt)
}
}
}
}
function toAbsolute(file, cwd) {
return isAbsolute(file) ? file : join(cwd, file)
}
function toName(files, cwd) {
return files.map(i => (i.startsWith(cwd) ? relative(cwd, i) : i)).join(', ')
}
const dynamicImport = async filePath => (await import(filePath)).default
const tsLoader = async filePath => {
let jiti = (await import('jiti')).default(fileURLToPath(import.meta.url), {
interopDefault: true
})
return jiti(filePath)
}
export default async function getConfig(plugins, process, args, pkg) {
let config = {
cwd: process.cwd()
}
if (args.why) {
config.project = pkg.packageJson.name
config.why = args.why
}
if (args.compareWith) {
config.compareWith = toAbsolute(args.compareWith, process.cwd())
}
if (args.saveBundle) {
config.saveBundle = toAbsolute(args.saveBundle, process.cwd())
}
if (args.cleanDir) {
config.cleanDir = args.cleanDir
}
if (args.hidePassed) {
config.hidePassed = args.hidePassed
}
if (args.highlightLess) {
config.highlightLess = args.highlightLess
}
if (args.files.length > 0) {
config.checks = [{ files: args.files }]
} else {
let explorer = lilconfig('size-limit', {
loaders: {
'.cjs': dynamicImport,
'.cts': tsLoader,
'.js': dynamicImport,
'.mjs': dynamicImport,
'.mts': tsLoader,
'.ts': tsLoader
},
searchPlaces: [
'package.json',
'.size-limit.json',
'.size-limit',
'.size-limit.js',
'.size-limit.mjs',
'.size-limit.cjs',
'.size-limit.ts',
'.size-limit.mts',
'.size-limit.cts'
]
})
let result = await explorer.search(process.cwd())
if (result === null) throw new SizeLimitError('noConfig')
checkChecks(plugins, result.config)
config.configPath = relative(process.cwd(), result.filepath)
config.cwd = dirname(result.filepath)
config.checks = await Promise.all(
result.config.map(async check => {
let processed = { ...check }
if (check.path) {
processed.files = await globby(check.path, { cwd: config.cwd })
} else if (!check.entry) {
if (pkg.packageJson.main) {
processed.files = [
require.resolve(join(dirname(pkg.path), pkg.packageJson.main))
]
} else {
processed.files = [join(dirname(pkg.path), 'index.js')]
}
}
return processed
})
)
}
let peer = Object.keys(pkg.packageJson.peerDependencies || {})
for (let check of config.checks) {
if (peer.length > 0) check.ignore = peer.concat(check.ignore || [])
if (check.entry && !Array.isArray(check.entry)) check.entry = [check.entry]
if (!check.name) check.name = toName(check.entry || check.files, config.cwd)
if (args.limit) check.limit = args.limit
if (check.limit) {
if (/ ?ms/i.test(check.limit)) {
check.timeLimit = parseFloat(check.limit) / 1000
} else if (/ ?s/i.test(check.limit)) {
check.timeLimit = parseFloat(check.limit)
} else {
check.sizeLimit = bytes.parse(check.limit)
}
if (check.timeLimit && !plugins.has('time')) {
throw new SizeLimitError('timeWithoutPlugin')
}
}
if (config.highlightLess) check.highlightLess = true
if (/\sB$|\dB$/.test(check.limit)) check.highlightLess = true
if (check.files) {
check.files = check.files.map(i => toAbsolute(i, config.cwd))
}
if (check.config) check.config = toAbsolute(check.config, config.cwd)
if (typeof check.import === 'string') {
check.import = {
[check.files[0]]: check.import
}
}
if (check.import) {
let imports = {}
for (let i in check.import) {
if (peer.includes(i)) {
check.ignore = check.ignore.filter(j => j !== i)
imports[require.resolve(i, config.cwd)] = check.import[i]
} else {
imports[toAbsolute(i, config.cwd)] = check.import[i]
}
}
check.import = imports
}
}
return config
}