Skip to content

Commit

Permalink
perf: use esbuild service mode during pre-bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 29, 2021
1 parent 81eb7a0 commit b24b07c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
writeFile,
flattenId
} from '../utils'
import { build } from 'esbuild'
import { esbuildDepPlugin } from './esbuildDepPlugin'
import { init, parse } from 'es-module-lexer'
import { scanImports } from './scan'
import { ensureService, stopService } from '../plugins/esbuild'

const debug = createDebugger('vite:deps')

Expand Down Expand Up @@ -191,7 +191,9 @@ export async function optimizeDeps(
flatIdDeps[flattenId(id)] = deps[id]
}

await build({
const start = Date.now()
const esbuildService = await ensureService()
await esbuildService.build({
entryPoints: Object.keys(flatIdDeps),
bundle: true,
format: 'esm',
Expand Down Expand Up @@ -221,6 +223,11 @@ export async function optimizeDeps(
}

writeFile(dataPath, JSON.stringify(data, null, 2))
if (asCommand) {
await stopService()
}

debug(`deps bundled in ${Date.now() - start}ms`)
return data
}

Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'
import glob from 'fast-glob'
import { ResolvedConfig } from '..'
import { build, Loader, Plugin } from 'esbuild'
import { Loader, Plugin } from 'esbuild'
import { knownAssetTypes } from '../constants'
import {
createDebugger,
Expand All @@ -22,6 +22,7 @@ import { init, parse } from 'es-module-lexer'
import MagicString from 'magic-string'
import { transformImportGlob } from '../importGlob'
import { isCSSRequest } from '../plugins/css'
import { ensureService } from '../plugins/esbuild'

const debug = createDebugger('vite:deps')

Expand Down Expand Up @@ -72,9 +73,10 @@ export async function scanImports(
const missing: Record<string, string> = {}
const plugin = esbuildScanPlugin(config, deps, missing, entries)

const esbuildService = await ensureService()
await Promise.all(
entries.map((entry) =>
build({
esbuildService.build({
entryPoints: [entry],
bundle: true,
format: 'esm',
Expand Down

0 comments on commit b24b07c

Please sign in to comment.