Skip to content

Commit d3142cf

Browse files
authored
refactor: upgrade to esbuild 0.9.x (#2506)
1 parent b18af15 commit d3142cf

File tree

7 files changed

+20
-56
lines changed

7 files changed

+20
-56
lines changed

packages/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"//": "READ .github/contributing.md to understand what to put under deps vs. devDeps!",
4646
"dependencies": {
47-
"esbuild": "^0.8.52",
47+
"esbuild": "^0.9.2",
4848
"postcss": "^8.2.1",
4949
"resolve": "^1.19.0",
5050
"rollup": "^2.38.5"

packages/vite/src/node/config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from './utils'
1515
import { resolvePlugins } from './plugins'
1616
import chalk from 'chalk'
17-
import { ESBuildOptions, esbuildPlugin, stopService } from './plugins/esbuild'
17+
import { ESBuildOptions, esbuildPlugin } from './plugins/esbuild'
1818
import dotenv from 'dotenv'
1919
import dotenvExpand from 'dotenv-expand'
2020
import { Alias, AliasOptions } from 'types/alias'
@@ -718,8 +718,6 @@ export async function loadConfigFromFile(
718718
chalk.red(`failed to load config from ${resolvedPath}`)
719719
)
720720
throw e
721-
} finally {
722-
await stopService()
723721
}
724722
}
725723

packages/vite/src/node/optimizer/esbuildDepPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import { Loader, Plugin, ResolveKind } from 'esbuild'
2+
import { Loader, Plugin, ImportKind } from 'esbuild'
33
import { KNOWN_ASSET_TYPES } from '../constants'
44
import { ResolvedConfig } from '..'
55
import {
@@ -47,7 +47,7 @@ export function esbuildDepPlugin(
4747
const resolve = (
4848
id: string,
4949
importer: string,
50-
kind: ResolveKind,
50+
kind: ImportKind,
5151
resolveDir?: string
5252
): Promise<string | undefined> => {
5353
let _importer

packages/vite/src/node/optimizer/index.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import chalk from 'chalk'
44
import { createHash } from 'crypto'
5+
import { build } from 'esbuild'
56
import { ResolvedConfig } from '../config'
67
import {
78
createDebugger,
@@ -14,7 +15,6 @@ import {
1415
import { esbuildDepPlugin } from './esbuildDepPlugin'
1516
import { ImportSpecifier, init, parse } from 'es-module-lexer'
1617
import { scanImports } from './scan'
17-
import { ensureService, stopService } from '../plugins/esbuild'
1818

1919
const debug = createDebugger('vite:deps')
2020

@@ -191,8 +191,6 @@ export async function optimizeDeps(
191191
logger.info(chalk.greenBright(`Optimizing dependencies:\n ${depsString}`))
192192
}
193193

194-
const esbuildMetaPath = path.join(cacheDir, '_esbuild.json')
195-
196194
// esbuild generates nested directory output with lowest common ancestor base
197195
// this is unpredictable and makes it difficult to analyze entry / output
198196
// mapping. So what we do here is:
@@ -227,8 +225,8 @@ export async function optimizeDeps(
227225
}
228226

229227
const start = Date.now()
230-
const esbuildService = await ensureService()
231-
await esbuildService.build({
228+
229+
const result = await build({
232230
entryPoints: Object.keys(flatIdDeps),
233231
bundle: true,
234232
keepNames: true,
@@ -239,12 +237,12 @@ export async function optimizeDeps(
239237
sourcemap: true,
240238
outdir: cacheDir,
241239
treeShaking: 'ignore-annotations',
242-
metafile: esbuildMetaPath,
240+
metafile: true,
243241
define,
244242
plugins: [esbuildDepPlugin(flatIdDeps, flatIdToExports, config)]
245243
})
246244

247-
const meta = JSON.parse(fs.readFileSync(esbuildMetaPath, 'utf-8'))
245+
const meta = result.metafile!
248246

249247
for (const id in deps) {
250248
const entry = deps[id]
@@ -256,9 +254,6 @@ export async function optimizeDeps(
256254
}
257255

258256
writeFile(dataPath, JSON.stringify(data, null, 2))
259-
if (asCommand) {
260-
await stopService()
261-
}
262257

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

packages/vite/src/node/optimizer/scan.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import glob from 'fast-glob'
44
import { ResolvedConfig } from '..'
5-
import { Loader, Plugin } from 'esbuild'
5+
import { Loader, Plugin, build, transform } from 'esbuild'
66
import {
77
KNOWN_ASSET_TYPES,
88
JS_TYPES_RE,
@@ -25,7 +25,6 @@ import {
2525
import { init, parse } from 'es-module-lexer'
2626
import MagicString from 'magic-string'
2727
import { transformImportGlob } from '../importGlob'
28-
import { ensureService } from '../plugins/esbuild'
2928

3029
const debug = createDebugger('vite:deps')
3130

@@ -82,10 +81,9 @@ export async function scanImports(
8281
const container = await createPluginContainer(config)
8382
const plugin = esbuildScanPlugin(config, container, deps, missing, entries)
8483

85-
const esbuildService = await ensureService()
8684
await Promise.all(
8785
entries.map((entry) =>
88-
esbuildService.build({
86+
build({
8987
entryPoints: [entry],
9088
bundle: true,
9189
format: 'esm',
@@ -362,7 +360,7 @@ async function transformGlob(
362360
) {
363361
// transform the content first since es-module-lexer can't handle non-js
364362
if (loader !== 'js') {
365-
source = (await (await ensureService()).transform(source, { loader })).code
363+
source = (await transform(source, { loader })).code
366364
}
367365

368366
await init

packages/vite/src/node/plugins/esbuild.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path'
22
import chalk from 'chalk'
33
import { Plugin } from '../plugin'
44
import {
5-
Service,
5+
transform,
66
Message,
77
Loader,
88
TransformOptions,
@@ -16,30 +16,12 @@ import { createFilter } from '@rollup/pluginutils'
1616

1717
const debug = createDebugger('vite:esbuild')
1818

19-
// lazy start the service
20-
let _servicePromise: Promise<Service> | undefined
21-
2219
export interface ESBuildOptions extends TransformOptions {
2320
include?: string | RegExp | string[] | RegExp[]
2421
exclude?: string | RegExp | string[] | RegExp[]
2522
jsxInject?: string
2623
}
2724

28-
export async function ensureService() {
29-
if (!_servicePromise) {
30-
_servicePromise = require('esbuild').startService()
31-
}
32-
return _servicePromise!
33-
}
34-
35-
export async function stopService() {
36-
if (_servicePromise) {
37-
const service = await _servicePromise
38-
service.stop()
39-
_servicePromise = undefined
40-
}
41-
}
42-
4325
export type ESBuildTransformResult = Omit<TransformResult, 'map'> & {
4426
map: SourceMap
4527
}
@@ -50,18 +32,17 @@ export async function transformWithEsbuild(
5032
options?: TransformOptions,
5133
inMap?: object
5234
): Promise<ESBuildTransformResult> {
53-
const service = await ensureService()
5435
// if the id ends with a valid ext, use it (e.g. vue blocks)
5536
// otherwise, cleanup the query before checking the ext
5637
const ext = path.extname(
5738
/\.\w+$/.test(filename) ? filename : cleanUrl(filename)
5839
)
59-
40+
6041
let loader = ext.slice(1)
6142
if (loader === 'cjs' || loader === 'mjs') {
6243
loader = 'js'
6344
}
64-
45+
6546
const resolvedOptions = {
6647
loader: loader as Loader,
6748
sourcemap: true,
@@ -75,7 +56,7 @@ export async function transformWithEsbuild(
7556
delete resolvedOptions.jsxInject
7657

7758
try {
78-
const result = await service.transform(code, resolvedOptions)
59+
const result = await transform(code, resolvedOptions)
7960
if (inMap) {
8061
const nextMap = JSON.parse(result.map)
8162
// merge-source-map will overwrite original sources if newMap also has
@@ -129,10 +110,6 @@ export function esbuildPlugin(options: ESBuildOptions = {}): Plugin {
129110
map: result.map
130111
}
131112
}
132-
},
133-
134-
async closeBundle() {
135-
await stopService()
136113
}
137114
}
138115
}
@@ -155,10 +132,6 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
155132
target: target || undefined,
156133
minify
157134
})
158-
},
159-
160-
async closeBundle() {
161-
await stopService()
162135
}
163136
}
164137
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,10 +2812,10 @@ es-to-primitive@^1.2.1:
28122812
is-date-object "^1.0.1"
28132813
is-symbol "^1.0.2"
28142814

2815-
esbuild@^0.8.52:
2816-
version "0.8.52"
2817-
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.8.52.tgz#6dabf11c517af449a96d66da20dfc204ee7b5294"
2818-
integrity sha512-b5KzFweLLXoXQwdC/e2+Z80c8uo2M5MgP7yQEEebkFw6In4T9CvYcNoM2ElvJt8ByO04zAZUV0fZkXmXoi2s9A==
2815+
esbuild@^0.9.2:
2816+
version "0.9.2"
2817+
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.9.2.tgz#7e9fde247c913ed8ee059e2648b0c53f7d00abe5"
2818+
integrity sha512-xE3oOILjnmN8PSjkG3lT9NBbd1DbxNqolJ5qNyrLhDWsFef3yTp/KTQz1C/x7BYFKbtrr9foYtKA6KA1zuNAUQ==
28192819

28202820
escalade@^3.1.1:
28212821
version "3.1.1"

0 commit comments

Comments
 (0)