-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathdepOptimizer.ts
309 lines (282 loc) · 9.44 KB
/
depOptimizer.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
307
308
309
import fs from 'fs-extra'
import path from 'path'
import { createHash } from 'crypto'
import { ResolvedConfig } from './config'
import type Rollup from 'rollup'
import { createResolver, supportedExts, resolveNodeModule } from './resolver'
import { createBaseRollupPlugins, onRollupWarning } from './build'
import { lookupFile } from './utils'
import { init, parse } from 'es-module-lexer'
import chalk from 'chalk'
import { Ora } from 'ora'
import { createBuildCssPlugin } from './build/buildPluginCss'
const KNOWN_IGNORE_LIST = new Set([
'tailwindcss',
'@tailwindcss/ui',
'@pika/react',
'@pika/react-dom'
])
export interface DepOptimizationOptions {
/**
* Only optimize explicitly listed dependencies.
*/
include?: string[]
/**
* Do not optimize these dependencies.
*/
exclude?: string[]
/**
* Explicitly allow these CommonJS deps to be bundled.
*/
commonJSWhitelist?: string[]
/**
* Automatically run `vite optimize` on server start?
* @default true
*/
auto?: boolean
}
export const OPTIMIZE_CACHE_DIR = `node_modules/.vite_opt_cache`
export async function optimizeDeps(
config: ResolvedConfig & { force?: boolean },
asCommand = false
) {
const debug = require('debug')('vite:optimize')
const log = asCommand ? console.log : debug
const root = config.root || process.cwd()
// warn presence of web_modules
if (fs.existsSync(path.join(root, 'web_modules'))) {
console.warn(
chalk.yellow(
`[vite] vite 0.15 has built-in dependency pre-bundling and resolving ` +
`from web_modules is no longer supported.`
)
)
}
const pkgPath = lookupFile(root, [`package.json`], true /* pathOnly */)
if (!pkgPath) {
log(`package.json not found. Skipping.`)
return
}
const cacheDir = resolveOptimizedCacheDir(root, pkgPath)!
const hashPath = path.join(cacheDir, 'hash')
const depHash = getDepHash(root, config.__path)
if (!config.force) {
let prevhash
try {
prevhash = await fs.readFile(hashPath, 'utf-8')
} catch (e) {}
// hash is consistent, no need to re-bundle
if (prevhash === depHash) {
log('Hash is consistent. Skipping. Use --force to override.')
return
}
}
await fs.remove(cacheDir)
await fs.ensureDir(cacheDir)
const deps = Object.keys(require(pkgPath).dependencies || {})
if (!deps.length) {
await fs.writeFile(hashPath, depHash)
log(`No dependencies listed in package.json. Skipping.`)
return
}
const resolver = createResolver(root, config.resolvers, config.alias)
const { include, exclude, commonJSWhitelist } = config.optimizeDeps || {}
// Determine deps to optimize. The goal is to only pre-bundle deps that falls
// under one of the following categories:
// 1. Has imports to relative files (e.g. lodash-es, lit-html)
// 2. Has imports to bare modules that are not in the project's own deps
// (i.e. esm that imports its own dependencies, e.g. styled-components)
await init
const cjsDeps: string[] = []
const qualifiedDeps = deps.filter((id) => {
if (include && !include.includes(id)) {
debug(`skipping ${id} (not included)`)
return false
}
if (exclude && exclude.includes(id)) {
debug(`skipping ${id} (excluded)`)
return false
}
if (commonJSWhitelist && commonJSWhitelist.includes(id)) {
debug(`optimizing ${id} (commonJSWhitelist)`)
return true
}
if (KNOWN_IGNORE_LIST.has(id)) {
debug(`skipping ${id} (internal excluded)`)
return false
}
const pkgInfo = resolveNodeModule(root, id)
if (!pkgInfo) {
debug(`skipping ${id} (cannot resolve entry)`)
return false
}
const { entryFilePath, pkg } = pkgInfo
if (!supportedExts.includes(path.extname(entryFilePath))) {
debug(`skipping ${id} (entry is not js)`)
return false
}
const content = fs.readFileSync(entryFilePath, 'utf-8')
const [imports, exports] = parse(content)
if (!exports.length && !/export\s+\*\s+from/.test(content)) {
if (!pkg.module) {
cjsDeps.push(id)
}
debug(`skipping ${id} (no exports, likely commonjs)`)
return false
}
for (const { s, e } of imports) {
let i = content.slice(s, e).trim()
i = resolver.alias(i) || i
if (i.startsWith('.')) {
debug(`optimizing ${id} (contains relative imports)`)
return true
}
if (!deps.includes(i)) {
debug(`optimizing ${id} (imports sub dependencies)`)
return true
}
}
debug(`skipping ${id} (single esm file, doesn't need optimization)`)
})
if (!qualifiedDeps.length) {
if (!cjsDeps.length) {
await fs.writeFile(hashPath, depHash)
log(`No listed dependency requires optimization. Skipping.`)
} else {
console.error(
chalk.yellow(
`[vite] The following dependencies seem to be CommonJS modules that\n` +
`do not provide ESM-friendly file formats:\n\n ` +
cjsDeps.map((dep) => chalk.magenta(dep)).join(`\n `) +
`\n` +
`\n- If you are not using them in browser code, you can move them\n` +
`to devDependencies or exclude them from this check by adding\n` +
`them to ${chalk.cyan(
`optimizeDeps.exclude`
)} in vue.config.js.\n` +
`\n- If you do intend to use them in the browser, you can try adding\n` +
`them to ${chalk.cyan(
`optimizeDeps.commonJSWhitelist`
)} in vue.config.js but they\n` +
`may fail to bundle or work properly. Consider choosing more modern\n` +
`alternatives that provide ES module build formts.`
)
)
}
return
}
if (!asCommand) {
// This is auto run on server start - let the user know that we are
// pre-optimizing deps
console.log(chalk.greenBright(`[vite] Optimizable dependencies detected.`))
}
let spinner: Ora | undefined
const msg = asCommand
? `Pre-bundling dependencies to speed up dev server page load...`
: `Pre-bundling them to speed up dev server page load...\n` +
`(this will be run only when your dependencies have changed)`
if (process.env.DEBUG || process.env.NODE_ENV === 'test') {
console.log(msg)
} else {
spinner = require('ora')(msg + '\n').start()
}
try {
// Non qualified deps are marked as externals, since they will be preserved
// and resolved from their original node_modules locations.
const preservedDeps = deps
.filter((id) => !qualifiedDeps.includes(id))
// make sure aliased deps are external
// https://github.com/vitejs/vite-plugin-react/issues/4
.map((id) => resolver.alias(id) || id)
const input = qualifiedDeps.reduce((entries, name) => {
entries[name] = name
return entries
}, {} as Record<string, string>)
const rollup = require('rollup') as typeof Rollup
const bundle = await rollup.rollup({
input,
external: preservedDeps,
treeshake: { moduleSideEffects: 'no-external' },
onwarn: onRollupWarning,
...config.rollupInputOptions,
plugins: [
...(await createBaseRollupPlugins(root, resolver, config)),
createBuildCssPlugin(root, '/', 'assets')
]
})
const { output } = await bundle.generate({
...config.rollupOutputOptions,
format: 'es',
exports: 'named',
entryFileNames: '[name]',
chunkFileNames: 'common/[name]-[hash].js'
})
spinner && spinner.stop()
const optimized = []
for (const chunk of output) {
if (chunk.type === 'chunk') {
const fileName = chunk.fileName
const filePath = path.join(cacheDir, fileName)
await fs.ensureDir(path.dirname(filePath))
await fs.writeFile(filePath, chunk.code)
if (!fileName.startsWith('common/')) {
optimized.push(fileName.replace(/\.js$/, ''))
}
}
}
console.log(
`Optimized modules:\n${optimized
.map((id) => chalk.yellowBright(id))
.join(`, `)}`
)
await fs.writeFile(hashPath, depHash)
} catch (e) {
spinner && spinner.stop()
if (asCommand) {
throw e
} else {
console.error(chalk.red(`[vite] Dep optimization failed with error:`))
console.error(e)
console.log()
console.log(
chalk.yellow(
`Tip: You can configure what deps to include/exclude for optimization\n` +
`using the \`optimizeDeps\` option in the Vite config file.`
)
)
}
}
}
const lockfileFormats = ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']
let cachedHash: string | undefined
export function getDepHash(
root: string,
configPath: string | undefined
): string {
if (cachedHash) {
return cachedHash
}
let content = lookupFile(root, lockfileFormats) || ''
const pkg = JSON.parse(lookupFile(root, [`package.json`]) || '{}')
content += JSON.stringify(pkg.dependencies)
// also take config into account
if (configPath) {
content += fs.readFileSync(configPath, 'utf-8')
}
return createHash('sha1').update(content).digest('base64')
}
const cacheDirCache = new Map<string, string | null>()
export function resolveOptimizedCacheDir(
root: string,
pkgPath?: string
): string | null {
const cached = cacheDirCache.get(root)
if (cached !== undefined) return cached
pkgPath = pkgPath || lookupFile(root, [`package.json`], true /* pathOnly */)
if (!pkgPath) {
return null
}
const cacheDir = path.join(path.dirname(pkgPath), OPTIMIZE_CACHE_DIR)
cacheDirCache.set(root, cacheDir)
return cacheDir
}