Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bundle vite config file with esbuild instead of rollup #2517

Merged
merged 4 commits into from
Mar 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs'
import path from 'path'
import { Plugin } from './plugin'
import Rollup from 'rollup'
import { BuildOptions, resolveBuildOptions } from './build'
import { ServerOptions } from './server'
import { CSSOptions } from './plugins/css'
Expand All @@ -14,7 +13,7 @@ import {
} from './utils'
import { resolvePlugins } from './plugins'
import chalk from 'chalk'
import { ESBuildOptions, esbuildPlugin } from './plugins/esbuild'
import { ESBuildOptions } from './plugins/esbuild'
import dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'
import { Alias, AliasOptions } from 'types/alias'
Expand All @@ -35,6 +34,7 @@ import {
PluginContainer
} from './server/pluginContainer'
import aliasPlugin from '@rollup/plugin-alias'
import { build } from 'esbuild'

const debug = createDebugger('vite:config')

Expand Down Expand Up @@ -725,43 +725,49 @@ async function bundleConfigFile(
fileName: string,
mjs = false
): Promise<string> {
const rollup = require('rollup') as typeof Rollup
// node-resolve must be imported since it's bundled
const bundle = await rollup.rollup({
external: (id: string) =>
(id[0] !== '.' && !path.isAbsolute(id)) ||
id.slice(-5, id.length) === '.json',
input: fileName,
treeshake: false,
const result = await build({
entryPoints: [fileName],
outfile: 'out.js',
write: false,
platform: 'node',
bundle: true,
format: mjs ? 'esm' : 'cjs',
plugins: [
// use esbuild + node-resolve to support .ts files
esbuildPlugin({ target: 'esnext' }),
resolvePlugin({
root: path.dirname(fileName),
isBuild: true,
asSrc: false,
isProduction: false
}),
{
name: 'externalize-deps',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
const id = args.path
if (
(id[0] !== '.' && !path.isAbsolute(id)) ||
id.slice(-5, id.length) === '.json'
) {
return {
external: true
}
}
})
}
},
{
name: 'replace-import-meta',
transform(code, id) {
return code.replace(
/\bimport\.meta\.url\b/g,
JSON.stringify(`file://${id}`)
)
setup(build) {
build.onLoad({ filter: /\.[jt]s$/ }, async (args) => {
const contents = await fs.promises.readFile(args.path, 'utf8')
return {
loader: args.path.endsWith('.ts') ? 'ts' : 'js',
contents: contents.replace(
/\bimport\.meta\.url\b/g,
JSON.stringify(`file://${args.path}`)
)
}
})
}
}
]
})

const {
output: [{ code }]
} = await bundle.generate({
exports: mjs ? 'auto' : 'named',
format: mjs ? 'es' : 'cjs'
})

return code
const { text } = result.outputFiles[0]
return text
}

interface NodeModuleWithCompile extends NodeModule {
Expand Down