@@ -6,6 +6,7 @@ import colors from 'picocolors'
6
6
import type { Alias , AliasOptions } from 'dep-types/alias'
7
7
import type { RollupOptions } from 'rollup'
8
8
import picomatch from 'picomatch'
9
+ import { ESModulesEvaluator } from 'vite/module-runner'
9
10
import type { AnymatchFn } from '../types/anymatch'
10
11
import { withTrailingSlash } from '../shared/utils'
11
12
import {
@@ -37,7 +38,6 @@ import { resolveBuildEnvironmentOptions, resolveBuilderOptions } from './build'
37
38
import type { ResolvedServerOptions , ServerOptions } from './server'
38
39
import { resolveServerOptions } from './server'
39
40
import { DevEnvironment } from './server/environment'
40
- import type { RunnableDevEnvironment } from './server/environments/runnableEnvironment'
41
41
import { createRunnableDevEnvironment } from './server/environments/runnableEnvironment'
42
42
import type { WebSocketServer } from './server/ws'
43
43
import type { PreviewOptions , ResolvedPreviewOptions } from './preview'
@@ -1529,87 +1529,100 @@ export async function loadConfigFromFile(
1529
1529
return null
1530
1530
}
1531
1531
1532
- let environment : RunnableDevEnvironment | undefined
1533
-
1534
1532
try {
1535
- environment = createRunnableDevEnvironment (
1536
- 'config' ,
1537
- await resolveConfig (
1538
- {
1539
- configFile : false ,
1540
- environments : {
1541
- config : {
1542
- consumer : 'server' ,
1543
- dev : {
1544
- moduleRunnerTransform : true ,
1545
- } ,
1546
- resolve : {
1547
- external : true ,
1548
- } ,
1533
+ const { userConfig, dependencies } = await importConfig ( resolvedPath )
1534
+ debug ?.( `config file loaded in ${ getTime ( ) } ` )
1535
+
1536
+ const config = await ( typeof userConfig === 'function'
1537
+ ? userConfig ( configEnv )
1538
+ : userConfig )
1539
+ if ( ! isObject ( config ) ) {
1540
+ throw new Error ( `config must export or return an object.` )
1541
+ }
1542
+ return {
1543
+ path : normalizePath ( resolvedPath ) ,
1544
+ config,
1545
+ dependencies,
1546
+ }
1547
+ } catch ( e ) {
1548
+ createLogger ( logLevel , { customLogger } ) . error (
1549
+ colors . red ( `failed to load config from ${ resolvedPath } ` ) ,
1550
+ {
1551
+ error : e ,
1552
+ } ,
1553
+ )
1554
+ throw e
1555
+ }
1556
+ }
1557
+
1558
+ async function importConfig ( resolvedPath : string ) {
1559
+ const environment = createRunnableDevEnvironment (
1560
+ 'config' ,
1561
+ // TODO: provide a dummy config?
1562
+ await resolveConfig (
1563
+ {
1564
+ configFile : false ,
1565
+ environments : {
1566
+ config : {
1567
+ consumer : 'server' ,
1568
+ dev : {
1569
+ moduleRunnerTransform : true ,
1570
+ } ,
1571
+ resolve : {
1572
+ external : true ,
1549
1573
} ,
1550
1574
} ,
1551
1575
} ,
1552
- 'serve' ,
1553
- ) ,
1554
- {
1555
- // options: {
1556
- // consumer: 'server',
1557
- // dev: {
1558
- // moduleRunnerTransform: true,
1559
- // } ,
1560
- // TODO for some reason this doesn't work, only setting it the config works
1561
- // resolve: {
1562
- // external: true,
1563
- // } ,
1564
- // },
1565
- runnerOptions : {
1566
- hmr : {
1567
- logger : false ,
1568
- } ,
1576
+ } ,
1577
+ 'serve' ,
1578
+ ) ,
1579
+ {
1580
+ // options: {
1581
+ // consumer: 'server',
1582
+ // dev: {
1583
+ // moduleRunnerTransform: true ,
1584
+ // },
1585
+ // TODO for some reason this doesn't work, only setting it the config works
1586
+ // resolve: {
1587
+ // external: true ,
1588
+ // },
1589
+ // },
1590
+ runnerOptions : {
1591
+ hmr : {
1592
+ logger : false ,
1569
1593
} ,
1570
- hot : false ,
1594
+ evaluator : new ESModulesEvaluator ( {
1595
+ cjsGlobals : true ,
1596
+ } ) ,
1571
1597
} ,
1572
- )
1573
- await environment . init ( )
1598
+ hot : false ,
1599
+ } ,
1600
+ )
1601
+ await environment . init ( )
1602
+ try {
1574
1603
const { default : userConfig } = ( await environment . runner . import (
1575
1604
resolvedPath ,
1576
1605
) ) as {
1577
1606
default : UserConfigExport
1578
1607
}
1579
- debug ?.( `config file loaded in ${ getTime ( ) } ` )
1580
-
1581
- const config = await ( typeof userConfig === 'function'
1582
- ? userConfig ( configEnv )
1583
- : userConfig )
1584
- if ( ! isObject ( config ) ) {
1585
- throw new Error ( `config must export or return an object.` )
1586
- }
1587
1608
const modules = [
1588
1609
...environment . runner . evaluatedModules . fileToModulesMap . entries ( ) ,
1589
1610
]
1590
- await environment . runner . close ( )
1591
1611
const dependencies = modules
1592
1612
. filter ( ( [ file , modules ] ) => {
1593
- const isExternal = [ ...modules ] . some (
1613
+ const isExternal = [ ...modules ] . every (
1594
1614
( m ) => ! m . meta || 'externalize' in m . meta ,
1595
1615
)
1596
1616
return ! isExternal && file !== resolvedPath
1597
1617
} )
1598
1618
. map ( ( [ file ] ) => file )
1599
1619
return {
1600
- path : normalizePath ( resolvedPath ) ,
1601
- config,
1620
+ userConfig,
1602
1621
dependencies,
1603
1622
}
1604
- } catch ( e ) {
1605
- await environment ?. runner . close ( )
1606
- createLogger ( logLevel , { customLogger } ) . error (
1607
- colors . red ( `failed to load config from ${ resolvedPath } ` ) ,
1608
- {
1609
- error : e ,
1610
- } ,
1611
- )
1612
- throw e
1623
+ } catch ( err ) {
1624
+ await environment . close ( )
1625
+ throw err
1613
1626
}
1614
1627
}
1615
1628
0 commit comments