Skip to content

Commit

Permalink
feat: make esbuild optional peer dep
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Dec 12, 2024
1 parent 4402cc7 commit c87cd8a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
},
"//": "READ CONTRIBUTING.md to understand what to put under deps vs. devDeps!",
"dependencies": {
"esbuild": "^0.24.0",
"lightningcss": "^1.28.1",
"postcss": "^8.4.49",
"rolldown": "0.15.0-snapshot-3cea4f5-20241211003613",
Expand Down Expand Up @@ -119,6 +118,7 @@
"dotenv": "^16.4.5",
"dotenv-expand": "^12.0.1",
"es-module-lexer": "^1.5.4",
"esbuild": "^0.24.0",
"escape-html": "^1.0.3",
"estree-walker": "^3.0.3",
"etag": "^1.8.1",
Expand Down Expand Up @@ -155,6 +155,7 @@
},
"peerDependencies": {
"@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0",
"esbuild": "^0.24.0",
"jiti": ">=1.21.0",
"less": "*",
"lightningcss": "^1.21.0",
Expand All @@ -170,6 +171,9 @@
"@types/node": {
"optional": true
},
"esbuild": {
"optional": true
},
"jiti": {
"optional": true
},
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
TransformOptions,
TransformResult,
} from 'esbuild'
import { transform } from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import type { InternalModuleFormat, SourceMap } from 'rolldown'
import type { TSConfckParseResult } from 'tsconfck'
Expand Down Expand Up @@ -75,6 +74,12 @@ type TSConfigJSON = {
}
type TSCompilerOptions = NonNullable<TSConfigJSON['compilerOptions']>

let esbuild: Promise<typeof import('esbuild')> | undefined
const importEsbuild = () => {
esbuild ||= import('esbuild')
return esbuild
}

export async function transformWithEsbuild(
code: string,
filename: string,
Expand Down Expand Up @@ -197,6 +202,7 @@ export async function transformWithEsbuild(
delete resolvedOptions.jsxInject

try {
const { transform } = await importEsbuild()
const result = await transform(code, resolvedOptions)
let map: SourceMap
if (inMap && resolvedOptions.sourcemap) {
Expand Down
1 change: 0 additions & 1 deletion packages/vite/src/node/publicUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export {
DEFAULT_SERVER_CONDITIONS as defaultServerConditions,
DEFAULT_SERVER_MAIN_FIELDS as defaultServerMainFields,
} from './constants'
export { version as esbuildVersion } from 'esbuild'
export {
splitVendorChunkPlugin,
splitVendorChunk,
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c87cd8a

Please sign in to comment.