forked from ai/size-limit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (139 loc) · 4.54 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
145
146
147
148
149
150
151
152
import { nanoid } from 'nanoid/non-secure'
import { readdir, readFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join, parse, resolve } from 'node:path'
import { rm, SizeLimitError } from 'size-limit'
import { convertConfig } from './convert-config.js'
import { getConfig } from './get-config.js'
import { runEsbuild } from './run-esbuild.js'
const ESBUILD_EMPTY_PROJECT = 12
const ESBUILD_EMPTY_PROJECT_GZIP = 32
const ESBUILD_EMPTY_PROJECT_BROTLI = 16
const ESBUILD_EMPTY_PROJECT_IMPORT = 34
const ESBUILD_EMPTY_PROJECT_IMPORT_GZIP = 46
const ESBUILD_EMPTY_PROJECT_IMPORT_BROTLI = 30
const ESBUILD_EMPTY_PROJECT_IMPORT_IGNORE = 331
const ESBUILD_EMPTY_PROJECT_IMPORT_IGNORE_GZIP = 182
const ESBUILD_EMPTY_PROJECT_IMPORT_IGNORE_BROTLI = 182
function getFiles(buildResult, check) {
let entries = {}
let outputs = buildResult.metafile.outputs
for (let key in outputs) {
let outputEntryName = parse(key).base
outputs[outputEntryName] = outputs[key]
outputs[outputEntryName].path = resolve(key)
delete outputs[key]
}
if (check.entry) {
for (let entry of check.entry) {
let matches = Object.keys(outputs).filter(key => parse(key).name === entry)
if(matches.length === 0) {
throw new SizeLimitError('unknownEntry', entry)
}
for(let match of matches) {
entries[match] = outputs[match]
}
}
} else {
entries = outputs
}
return Object.values(entries).map(({ path }) => path)
}
async function isDirNotEmpty(dir) {
try {
let files = await readdir(dir)
return !!files.length
} catch (e) {
if (e.code === 'ENOENT') return false
throw e
}
}
export default [
{
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 finally(config, check) {
if (check.esbuildOutfile && !config.saveBundle) {
await rm(check.esbuildOutfile)
}
},
name: '@size-limit/esbuild',
async step20(config, check) {
if (check.esbuild === false) return
check.esbuildOutfile = config.saveBundle
if (!check.esbuildOutfile) {
check.esbuildOutfile = join(tmpdir(), `size-limit-${nanoid()}`)
}
if (check.config) {
check.esbuildConfig = (await import(check.config)).default
convertConfig(check.esbuildConfig, config.configPath)
} else {
check.esbuildConfig = await getConfig(
config,
check,
check.esbuildOutfile
)
if (check.modifyEsbuildConfig) {
check.esbuildConfig = check.modifyEsbuildConfig(check.esbuildConfig)
}
}
},
async step40(config, check) {
if (check.esbuildConfig && check.esbuild !== false) {
let result = await runEsbuild(check)
check.esbuildMetafile = result.metafile
check.bundles = getFiles(result, check)
}
},
async step61(config, check) {
if (check.bundles) {
if (typeof check.size === 'undefined') {
throw new SizeLimitError('missedPlugin', 'file')
}
let hasRequirePolyfill = false
if (check.ignore) {
for (let bundle of check.bundles) {
let js = await readFile(bundle)
if (js.toString().includes('Dynamic require of ')) {
hasRequirePolyfill = true
}
}
}
if (hasRequirePolyfill && check.import) {
if (check.gzip === true) {
check.size -= ESBUILD_EMPTY_PROJECT_IMPORT_IGNORE_GZIP
} else if (check.brotli === false) {
check.size -= ESBUILD_EMPTY_PROJECT_IMPORT_IGNORE
} else {
check.size -= ESBUILD_EMPTY_PROJECT_IMPORT_IGNORE_BROTLI
}
} else if (check.import) {
if (check.gzip === true) {
check.size -= ESBUILD_EMPTY_PROJECT_IMPORT_GZIP
} else if (check.brotli === false) {
check.size -= ESBUILD_EMPTY_PROJECT_IMPORT
} else {
check.size -= ESBUILD_EMPTY_PROJECT_IMPORT_BROTLI
}
} else if (check.gzip === true) {
check.size -= ESBUILD_EMPTY_PROJECT_GZIP
} else if (check.brotli === false) {
check.size -= ESBUILD_EMPTY_PROJECT
} else {
check.size -= ESBUILD_EMPTY_PROJECT_BROTLI
}
}
},
wait40: 'Adding to empty esbuild project'
}
]