1
1
import fs from 'fs'
2
2
import path from 'path'
3
3
import { Plugin } from './plugin'
4
- import Rollup from 'rollup'
5
4
import { BuildOptions , resolveBuildOptions } from './build'
6
5
import { ServerOptions } from './server'
7
6
import { CSSOptions } from './plugins/css'
@@ -14,7 +13,7 @@ import {
14
13
} from './utils'
15
14
import { resolvePlugins } from './plugins'
16
15
import chalk from 'chalk'
17
- import { ESBuildOptions , esbuildPlugin } from './plugins/esbuild'
16
+ import { ESBuildOptions } from './plugins/esbuild'
18
17
import dotenv from 'dotenv'
19
18
import dotenvExpand from 'dotenv-expand'
20
19
import { Alias , AliasOptions } from 'types/alias'
@@ -35,6 +34,7 @@ import {
35
34
PluginContainer
36
35
} from './server/pluginContainer'
37
36
import aliasPlugin from '@rollup/plugin-alias'
37
+ import { build } from 'esbuild'
38
38
39
39
const debug = createDebugger ( 'vite:config' )
40
40
@@ -726,43 +726,49 @@ async function bundleConfigFile(
726
726
fileName : string ,
727
727
mjs = false
728
728
) : 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' ,
737
736
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
+ } ,
746
753
{
747
754
name : 'replace-import-meta' ,
748
- transform ( code , id ) {
749
- return code . replace (
750
- / \b i m p o r t \. m e t a \. u r l \b / g,
751
- JSON . stringify ( `file://${ id } ` )
752
- )
755
+ setup ( build ) {
756
+ build . onLoad ( { filter : / \. [ j t ] 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
+ / \b i m p o r t \. m e t a \. u r l \b / g,
762
+ JSON . stringify ( `file://${ args . path } ` )
763
+ )
764
+ }
765
+ } )
753
766
}
754
767
}
755
768
]
756
769
} )
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
766
772
}
767
773
768
774
interface NodeModuleWithCompile extends NodeModule {
0 commit comments