Skip to content

Commit e034ee2

Browse files
authored
feat: bundle vite config file with esbuild instead of rollup (#2517)
1 parent c6f0b7c commit e034ee2

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

packages/vite/src/node/config.ts

+38-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'fs'
22
import path from 'path'
33
import { Plugin } from './plugin'
4-
import Rollup from 'rollup'
54
import { BuildOptions, resolveBuildOptions } from './build'
65
import { ServerOptions } from './server'
76
import { CSSOptions } from './plugins/css'
@@ -14,7 +13,7 @@ import {
1413
} from './utils'
1514
import { resolvePlugins } from './plugins'
1615
import chalk from 'chalk'
17-
import { ESBuildOptions, esbuildPlugin } from './plugins/esbuild'
16+
import { ESBuildOptions } from './plugins/esbuild'
1817
import dotenv from 'dotenv'
1918
import dotenvExpand from 'dotenv-expand'
2019
import { Alias, AliasOptions } from 'types/alias'
@@ -35,6 +34,7 @@ import {
3534
PluginContainer
3635
} from './server/pluginContainer'
3736
import aliasPlugin from '@rollup/plugin-alias'
37+
import { build } from 'esbuild'
3838

3939
const debug = createDebugger('vite:config')
4040

@@ -726,43 +726,49 @@ async function bundleConfigFile(
726726
fileName: string,
727727
mjs = false
728728
): Promise<string> {
729-
const rollup = require('rollup') as typeof Rollup
730-
// node-resolve must be imported since it's bundled
731-
const bundle = await rollup.rollup({
732-
external: (id: string) =>
733-
(id[0] !== '.' && !path.isAbsolute(id)) ||
734-
id.slice(-5, id.length) === '.json',
735-
input: fileName,
736-
treeshake: false,
729+
const result = await build({
730+
entryPoints: [fileName],
731+
outfile: 'out.js',
732+
write: false,
733+
platform: 'node',
734+
bundle: true,
735+
format: mjs ? 'esm' : 'cjs',
737736
plugins: [
738-
// use esbuild + node-resolve to support .ts files
739-
esbuildPlugin({ target: 'esnext' }),
740-
resolvePlugin({
741-
root: path.dirname(fileName),
742-
isBuild: true,
743-
asSrc: false,
744-
isProduction: false
745-
}),
737+
{
738+
name: 'externalize-deps',
739+
setup(build) {
740+
build.onResolve({ filter: /.*/ }, (args) => {
741+
const id = args.path
742+
if (
743+
(id[0] !== '.' && !path.isAbsolute(id)) ||
744+
id.slice(-5, id.length) === '.json'
745+
) {
746+
return {
747+
external: true
748+
}
749+
}
750+
})
751+
}
752+
},
746753
{
747754
name: 'replace-import-meta',
748-
transform(code, id) {
749-
return code.replace(
750-
/\bimport\.meta\.url\b/g,
751-
JSON.stringify(`file://${id}`)
752-
)
755+
setup(build) {
756+
build.onLoad({ filter: /\.[jt]s$/ }, async (args) => {
757+
const contents = await fs.promises.readFile(args.path, 'utf8')
758+
return {
759+
loader: args.path.endsWith('.ts') ? 'ts' : 'js',
760+
contents: contents.replace(
761+
/\bimport\.meta\.url\b/g,
762+
JSON.stringify(`file://${args.path}`)
763+
)
764+
}
765+
})
753766
}
754767
}
755768
]
756769
})
757-
758-
const {
759-
output: [{ code }]
760-
} = await bundle.generate({
761-
exports: mjs ? 'auto' : 'named',
762-
format: mjs ? 'es' : 'cjs'
763-
})
764-
765-
return code
770+
const { text } = result.outputFiles[0]
771+
return text
766772
}
767773

768774
interface NodeModuleWithCompile extends NodeModule {

0 commit comments

Comments
 (0)