-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
index.ts
306 lines (277 loc) · 8.34 KB
/
index.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
import { createHash } from 'crypto'
import { ResolvedConfig } from '../config'
import {
createDebugger,
emptyDir,
lookupFile,
normalizePath,
writeFile
} from '../utils'
import { build } from 'esbuild'
import { esbuildDepPlugin } from './esbuildDepPlugin'
import { init, parse } from 'es-module-lexer'
import { scanImports } from './scan'
const debug = createDebugger('vite:deps')
export interface DepOptimizationOptions {
/**
* By default, Vite will crawl your index.html to detect dependencies that
* need to be pre-bundled. If build.rollupOptions.input is specified, Vite
* will crawl those entry points instead.
*
* If neither of these fit your needs, you can specify custom entries using
* this option - the value should be a fast-glob pattern or array of patterns
* (https://github.com/mrmlnc/fast-glob#basic-syntax) that are relative from
* vite project root. This will overwrite default entries inference.
*/
entries?: string | string[]
/**
* Force optimize listed dependencies (must be resolvalble import paths,
* cannot be globs).
*/
include?: string[]
/**
* Do not optimize these dependencies (must be resolvable import paths,
* cannot be globs).
*/
exclude?: string[]
}
export interface DepOptimizationMetadata {
/**
* The main hash is determined by user config and dependency lockfiles.
* This is checked on server startup to avoid unncessary re-bundles.
*/
hash: string
/**
* The browser hash is determined by the main hash plus additional dependencies
* discovered at runtime. This is used to invalidate browser requests to
* optimized deps.
*/
browserHash: string
optimized: Record<
string,
{
file: string
src: string
needsInterop: boolean
}
>
}
export async function optimizeDeps(
config: ResolvedConfig,
force = config.server.force,
asCommand = false,
newDeps?: Record<string, string> // missing imports encountered after server has started
): Promise<DepOptimizationMetadata | null> {
config = {
...config,
command: 'build'
}
const { root, logger, optimizeCacheDir: cacheDir } = config
const log = asCommand ? logger.info : debug
if (!cacheDir) {
log(`No package.json. Skipping.`)
return null
}
const dataPath = path.join(cacheDir, 'metadata.json')
const mainHash = getDepHash(root, config)
const data: DepOptimizationMetadata = {
hash: mainHash,
browserHash: mainHash,
optimized: {}
}
if (!force) {
let prevData
try {
prevData = JSON.parse(fs.readFileSync(dataPath, 'utf-8'))
} catch (e) {}
// hash is consistent, no need to re-bundle
if (prevData && prevData.hash === data.hash) {
log('Hash is consistent. Skipping. Use --force to override.')
return prevData
}
}
if (fs.existsSync(cacheDir)) {
emptyDir(cacheDir)
} else {
fs.mkdirSync(cacheDir, { recursive: true })
}
let deps: Record<string, string>, missing: Record<string, string>
if (!newDeps) {
;({ deps, missing } = await scanImports(config))
} else {
deps = newDeps
missing = {}
}
// update browser hash
data.browserHash = createHash('sha256')
.update(data.hash + JSON.stringify(deps))
.digest('hex')
.substr(0, 8)
const missingIds = Object.keys(missing)
if (missingIds.length) {
throw new Error(
`The following dependencies are imported but could not be resolved:\n\n ${missingIds
.map(
(id) =>
`${chalk.cyan(id)} ${chalk.white.dim(
`(imported by ${missing[id]})`
)}`
)
.join(`\n `)}\n\nAre they installed?`
)
}
const qualifiedIds = Object.keys(deps)
const include = config.optimizeDeps?.include
if (include) {
const resolve = config.createResolver({ asSrc: false })
for (const id of include) {
if (!deps[id]) {
const entry = await resolve(id)
if (entry) {
deps[id] = entry
} else {
throw new Error(
`Failed to resolve force included dependency: ${chalk.cyan(id)}`
)
}
}
}
}
if (!qualifiedIds.length) {
writeFile(dataPath, JSON.stringify(data, null, 2))
log(`No dependencies to bundle. Skipping.\n\n\n`)
return data
}
const depsString = qualifiedIds.map((id) => chalk.yellow(id)).join(`, `)
if (!asCommand) {
if (!newDeps) {
// This is auto run on server start - let the user know that we are
// pre-optimizing deps
logger.info(
chalk.greenBright(`Pre-bundling dependencies:\n${depsString}`)
)
logger.info(
`(this will be run only when your dependencies or config have changed)`
)
}
} else {
logger.info(chalk.greenBright(`Optimizing dependencies:\n${depsString}`))
}
const esbuildMetaPath = path.join(cacheDir, 'esbuild.json')
await build({
entryPoints: Object.values(deps).map((p) => path.resolve(p)),
bundle: true,
format: 'esm',
external: config.optimizeDeps?.exclude,
logLevel: 'error',
splitting: true,
sourcemap: true,
outdir: cacheDir,
treeShaking: 'ignore-annotations',
metafile: esbuildMetaPath,
define: {
'process.env.NODE_ENV': '"development"'
},
plugins: [esbuildDepPlugin(deps, config)]
})
const meta = JSON.parse(fs.readFileSync(esbuildMetaPath, 'utf-8'))
await init
const entryToIdMap: Record<string, string> = {}
for (const id in deps) {
entryToIdMap[deps[id].toLowerCase()] = id
}
const normalizedInputs = Object.keys(meta.inputs).map((input) =>
normalizePath(path.resolve(input))
)
for (const output in meta.outputs) {
if (/\.vite[\/\\]chunk\.\w+\.js$/.test(output) || output.endsWith('.map')) {
continue
}
const { exports } = meta.outputs[output]
const relativeOutput = normalizePath(
path.relative(cacheDir, path.resolve(output))
)
for (const input of normalizedInputs) {
const entry = normalizePath(path.resolve(input))
if (!entry.replace(/\.mjs$/, '.js').endsWith(relativeOutput)) {
continue
}
const id = entryToIdMap[entry.toLowerCase()]
if (!id) {
continue
}
data.optimized[id] = {
file: normalizePath(path.resolve(output)),
src: entry,
needsInterop: needsInterop(id, entry, exports)
}
break
}
}
writeFile(dataPath, JSON.stringify(data, null, 2))
return data
}
// https://github.com/vitejs/vite/issues/1724#issuecomment-767619642
// a list of modules that pretends to be ESM but still uses `require`.
// this causes esbuild to wrap them as CJS even when its entry appears to be ESM.
const KNOWN_INTEROP_IDS = new Set(['moment'])
function needsInterop(
id: string,
entry: string,
generatedExports: string[]
): boolean {
if (KNOWN_INTEROP_IDS.has(id)) {
return true
}
const [imports, exports] = parse(fs.readFileSync(entry, 'utf-8'))
// entry has no ESM syntax - likely CJS or UMD
if (!exports.length && !imports.length) {
return true
}
// if a peer dep used require() on a ESM dep, esbuild turns the
// ESM dep's entry chunk into a single default export... detect
// such cases by checking exports mismatch, and force interop.
if (
isSingleDefaultExport(generatedExports) &&
!isSingleDefaultExport(exports)
) {
return true
}
return false
}
function isSingleDefaultExport(exports: string[]) {
return exports.length === 1 && exports[0] === 'default'
}
const lockfileFormats = ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']
let cachedHash: string | undefined
function getDepHash(root: string, config: ResolvedConfig): string {
if (cachedHash) {
return cachedHash
}
let content = lookupFile(root, lockfileFormats) || ''
// also take config into account
// only a subset of config options that can affect dep optimization
content += JSON.stringify(
{
mode: config.mode,
root: config.root,
alias: config.alias,
dedupe: config.dedupe,
assetsInclude: config.assetsInclude,
optimizeDeps: {
include: config.optimizeDeps?.include,
exclude: config.optimizeDeps?.exclude
}
},
(_, value) => {
if (typeof value === 'function' || value instanceof RegExp) {
return value.toString()
}
return value
}
)
return createHash('sha256').update(content).digest('hex').substr(0, 8)
}