-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
config.ts
1097 lines (1012 loc) · 31.4 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from 'node:fs'
import path from 'node:path'
import { parse as parseUrl, pathToFileURL } from 'node:url'
import { performance } from 'node:perf_hooks'
import { createRequire } from 'node:module'
import colors from 'picocolors'
import type { Alias, AliasOptions } from 'types/alias'
import aliasPlugin from '@rollup/plugin-alias'
import { build } from 'esbuild'
import type { Plugin as ESBuildPlugin } from 'esbuild'
import type { RollupOptions } from 'rollup'
import type { Plugin } from './plugin'
import type {
BuildAdvancedBaseConfig,
BuildOptions,
ResolvedBuildAdvancedBaseConfig,
ResolvedBuildOptions
} from './build'
import { resolveBuildAdvancedBaseConfig, resolveBuildOptions } from './build'
import type { ResolvedServerOptions, ServerOptions } from './server'
import { resolveServerOptions } from './server'
import type { PreviewOptions, ResolvedPreviewOptions } from './preview'
import { resolvePreviewOptions } from './preview'
import type { CSSOptions } from './plugins/css'
import {
asyncFlatten,
createDebugger,
createFilter,
dynamicImport,
isExternalUrl,
isObject,
isTS,
lookupFile,
mergeAlias,
mergeConfig,
normalizeAlias,
normalizePath
} from './utils'
import { resolvePlugins } from './plugins'
import type { ESBuildOptions } from './plugins/esbuild'
import {
CLIENT_ENTRY,
DEFAULT_ASSETS_RE,
DEFAULT_CONFIG_FILES,
ENV_ENTRY
} from './constants'
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
import { resolvePlugin } from './plugins/resolve'
import type { LogLevel, Logger } from './logger'
import { createLogger } from './logger'
import type { DepOptimizationOptions } from './optimizer'
import type { JsonOptions } from './plugins/json'
import type { PluginContainer } from './server/pluginContainer'
import { createPluginContainer } from './server/pluginContainer'
import type { PackageCache } from './packages'
import { loadEnv, resolveEnvPrefix } from './env'
import type { ResolvedSSROptions, SSROptions } from './ssr'
import { resolveSSROptions } from './ssr'
const debug = createDebugger('vite:config')
export type { BuildAdvancedBaseOptions, BuildAdvancedBaseConfig } from './build'
// NOTE: every export in this file is re-exported from ./index.ts so it will
// be part of the public API.
export interface ConfigEnv {
command: 'build' | 'serve'
mode: string
}
/**
* spa: include SPA fallback middleware and configure sirv with `single: true` in preview
*
* mpa: only include non-SPA HTML middlewares
*
* custom: don't include HTML middlewares
*/
export type AppType = 'spa' | 'mpa' | 'custom'
export type UserConfigFn = (env: ConfigEnv) => UserConfig | Promise<UserConfig>
export type UserConfigExport = UserConfig | Promise<UserConfig> | UserConfigFn
/**
* Type helper to make it easier to use vite.config.ts
* accepts a direct {@link UserConfig} object, or a function that returns it.
* The function receives a {@link ConfigEnv} object that exposes two properties:
* `command` (either `'build'` or `'serve'`), and `mode`.
*/
export function defineConfig(config: UserConfigExport): UserConfigExport {
return config
}
export type PluginOption =
| Plugin
| false
| null
| undefined
| PluginOption[]
| Promise<Plugin | false | null | undefined | PluginOption[]>
export interface UserConfig {
/**
* Project root directory. Can be an absolute path, or a path relative from
* the location of the config file itself.
* @default process.cwd()
*/
root?: string
/**
* Base public path when served in development or production.
* @default '/'
*/
base?: string
/**
* Directory to serve as plain static assets. Files in this directory are
* served and copied to build dist dir as-is without transform. The value
* can be either an absolute file system path or a path relative to <root>.
*
* Set to `false` or an empty string to disable copied static assets to build dist dir.
* @default 'public'
*/
publicDir?: string | false
/**
* Directory to save cache files. Files in this directory are pre-bundled
* deps or some other cache files that generated by vite, which can improve
* the performance. You can use `--force` flag or manually delete the directory
* to regenerate the cache files. The value can be either an absolute file
* system path or a path relative to <root>.
* Default to `.vite` when no `package.json` is detected.
* @default 'node_modules/.vite'
*/
cacheDir?: string
/**
* Explicitly set a mode to run in. This will override the default mode for
* each command, and can be overridden by the command line --mode option.
*/
mode?: string
/**
* Define global variable replacements.
* Entries will be defined on `window` during dev and replaced during build.
*/
define?: Record<string, any>
/**
* Array of vite plugins to use.
*/
plugins?: PluginOption[]
/**
* Configure resolver
*/
resolve?: ResolveOptions & { alias?: AliasOptions }
/**
* CSS related options (preprocessors and CSS modules)
*/
css?: CSSOptions
/**
* JSON loading options
*/
json?: JsonOptions
/**
* Transform options to pass to esbuild.
* Or set to `false` to disable esbuild.
*/
esbuild?: ESBuildOptions | false
/**
* Specify additional picomatch patterns to be treated as static assets.
*/
assetsInclude?: string | RegExp | (string | RegExp)[]
/**
* Server specific options, e.g. host, port, https...
*/
server?: ServerOptions
/**
* Build specific options
*/
build?: BuildOptions
/**
* Preview specific options, e.g. host, port, https...
*/
preview?: PreviewOptions
/**
* Dep optimization options
*/
optimizeDeps?: DepOptimizationOptions
/**
* SSR specific options
*/
ssr?: SSROptions
/**
* Experimental features
*
* Features under this field could change in the future and might NOT follow semver.
* Please be careful and always pin Vite's version when using them.
* @experimental
*/
experimental?: ExperimentalOptions
/**
* Legacy options
*
* Features under this field only follow semver for patches, they could be removed in a
* future minor version. Please always pin Vite's version to a minor when using them.
*/
legacy?: LegacyOptions
/**
* Log level.
* Default: 'info'
*/
logLevel?: LogLevel
/**
* Custom logger.
*/
customLogger?: Logger
/**
* Default: true
*/
clearScreen?: boolean
/**
* Environment files directory. Can be an absolute path, or a path relative from
* the location of the config file itself.
* @default root
*/
envDir?: string
/**
* Env variables starts with `envPrefix` will be exposed to your client source code via import.meta.env.
* @default 'VITE_'
*/
envPrefix?: string | string[]
/**
* Worker bundle options
*/
worker?: {
/**
* Output format for worker bundle
* @default 'iife'
*/
format?: 'es' | 'iife'
/**
* Vite plugins that apply to worker bundle
*/
plugins?: PluginOption[]
/**
* Rollup options to build worker bundle
*/
rollupOptions?: Omit<
RollupOptions,
'plugins' | 'input' | 'onwarn' | 'preserveEntrySignatures'
>
}
/**
* Whether your application is a Single Page Application (SPA),
* a Multi-Page Application (MPA), or Custom Application (SSR
* and frameworks with custom HTML handling)
* @default 'spa'
*/
appType?: AppType
}
export interface ExperimentalOptions {
/**
* Append fake `&lang.(ext)` when queries are specified, to preseve the file extension for following plugins to process.
*
* @experimental
* @default false
*/
importGlobRestoreExtension?: boolean
/**
* Build advanced base options. Allow finegrain contol over assets and public files base
*
* @experimental
*/
buildAdvancedBaseOptions?: BuildAdvancedBaseConfig
/**
* Enables support of HMR partial accept via `import.meta.hot.acceptExports`.
*
* @experimental
* @default false
*/
hmrPartialAccept?: boolean
}
export type ResolvedExperimentalOptions = Required<ExperimentalOptions> & {
buildAdvancedBaseOptions: ResolvedBuildAdvancedBaseConfig
}
export interface LegacyOptions {
/**
* Revert vite dev to the v2.9 strategy. Enable esbuild based deps scanner.
*
* @experimental
* @deprecated
* @default false
*/
devDepsScanner?: boolean
/**
* Revert vite build to the v2.9 strategy. Disable esbuild deps optimization and adds `@rollup/plugin-commonjs`
*
* @experimental
* @deprecated
* @default false
*/
buildRollupPluginCommonjs?: boolean
/**
* Revert vite build --ssr to the v2.9 strategy. Use CJS SSR build and v2.9 externalization heuristics
*
* @experimental
* @deprecated
* @default false
*/
buildSsrCjsExternalHeuristics?: boolean
}
export interface ResolveWorkerOptions {
format: 'es' | 'iife'
plugins: Plugin[]
rollupOptions: RollupOptions
}
export interface InlineConfig extends UserConfig {
configFile?: string | false
envFile?: false
}
export type ResolvedConfig = Readonly<
Omit<UserConfig, 'plugins' | 'assetsInclude' | 'optimizeDeps' | 'worker'> & {
configFile: string | undefined
configFileDependencies: string[]
inlineConfig: InlineConfig
root: string
base: string
publicDir: string
cacheDir: string
command: 'build' | 'serve'
mode: string
isWorker: boolean
// in nested worker bundle to find the main config
/** @internal */
mainConfig: ResolvedConfig | null
isProduction: boolean
env: Record<string, any>
resolve: ResolveOptions & {
alias: Alias[]
}
plugins: readonly Plugin[]
server: ResolvedServerOptions
build: ResolvedBuildOptions
preview: ResolvedPreviewOptions
ssr: ResolvedSSROptions | undefined
assetsInclude: (file: string) => boolean
logger: Logger
createResolver: (options?: Partial<InternalResolveOptions>) => ResolveFn
optimizeDeps: DepOptimizationOptions
/** @internal */
packageCache: PackageCache
worker: ResolveWorkerOptions
appType: AppType
experimental: ResolvedExperimentalOptions
}
>
export type ResolveFn = (
id: string,
importer?: string,
aliasOnly?: boolean,
ssr?: boolean
) => Promise<string | undefined>
export async function resolveConfig(
inlineConfig: InlineConfig,
command: 'build' | 'serve',
defaultMode = 'development'
): Promise<ResolvedConfig> {
let config = inlineConfig
let configFileDependencies: string[] = []
let mode = inlineConfig.mode || defaultMode
// some dependencies e.g. @vue/compiler-* relies on NODE_ENV for getting
// production-specific behavior, so set it here even though we haven't
// resolve the final mode yet
if (mode === 'production') {
process.env.NODE_ENV = 'production'
}
const configEnv = {
mode,
command
}
let { configFile } = config
if (configFile !== false) {
const loadResult = await loadConfigFromFile(
configEnv,
configFile,
config.root,
config.logLevel
)
if (loadResult) {
config = mergeConfig(loadResult.config, config)
configFile = loadResult.path
configFileDependencies = loadResult.dependencies
}
}
// Define logger
const logger = createLogger(config.logLevel, {
allowClearScreen: config.clearScreen,
customLogger: config.customLogger
})
// user config may provide an alternative mode. But --mode has a higher priority
mode = inlineConfig.mode || config.mode || mode
configEnv.mode = mode
// resolve plugins
const rawUserPlugins = (
(await asyncFlatten(config.plugins || [])) as Plugin[]
).filter((p) => {
if (!p) {
return false
} else if (!p.apply) {
return true
} else if (typeof p.apply === 'function') {
return p.apply({ ...config, mode }, configEnv)
} else {
return p.apply === command
}
})
const [prePlugins, normalPlugins, postPlugins] =
sortUserPlugins(rawUserPlugins)
// resolve worker
const resolvedWorkerOptions: ResolveWorkerOptions = {
format: config.worker?.format || 'iife',
plugins: [],
rollupOptions: config.worker?.rollupOptions || {}
}
// run config hooks
const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins]
for (const p of userPlugins) {
if (p.config) {
const res = await p.config(config, configEnv)
if (res) {
config = mergeConfig(config, res)
}
}
}
// resolve root
const resolvedRoot = normalizePath(
config.root ? path.resolve(config.root) : process.cwd()
)
const clientAlias = [
{ find: /^[\/]?@vite\/env/, replacement: () => ENV_ENTRY },
{ find: /^[\/]?@vite\/client/, replacement: () => CLIENT_ENTRY }
]
// resolve alias with internal client alias
const resolvedAlias = normalizeAlias(
mergeAlias(
// @ts-ignore because @rollup/plugin-alias' type doesn't allow function
// replacement, but its implementation does work with function values.
clientAlias,
config.resolve?.alias || []
)
)
const resolveOptions: ResolvedConfig['resolve'] = {
...config.resolve,
alias: resolvedAlias
}
// load .env files
const envDir = config.envDir
? normalizePath(path.resolve(resolvedRoot, config.envDir))
: resolvedRoot
const userEnv =
inlineConfig.envFile !== false &&
loadEnv(mode, envDir, resolveEnvPrefix(config))
// Note it is possible for user to have a custom mode, e.g. `staging` where
// production-like behavior is expected. This is indicated by NODE_ENV=production
// loaded from `.staging.env` and set by us as VITE_USER_NODE_ENV
const isProduction =
(process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) ===
'production'
if (isProduction) {
// in case default mode was not production and is overwritten
process.env.NODE_ENV = 'production'
}
// resolve public base url
const isBuild = command === 'build'
const relativeBaseShortcut = config.base === '' || config.base === './'
// During dev, we ignore relative base and fallback to '/'
// For the SSR build, relative base isn't possible by means
// of import.meta.url. The user will be able to work out a setup
// using experimental.buildAdvancedBaseOptions
const base =
relativeBaseShortcut && (!isBuild || config.build?.ssr)
? '/'
: config.base ?? '/'
let resolvedBase = relativeBaseShortcut
? base
: resolveBaseUrl(base, isBuild, logger, 'base')
if (
config.experimental?.buildAdvancedBaseOptions?.relative &&
config.base === undefined
) {
resolvedBase = './'
}
const resolvedBuildAdvancedBaseOptions = resolveBuildAdvancedBaseConfig(
config.experimental?.buildAdvancedBaseOptions,
resolvedBase,
isBuild,
logger
)
const resolvedBuildOptions = resolveBuildOptions(
config.build,
isBuild,
logger
)
// resolve cache directory
const pkgPath = lookupFile(resolvedRoot, [`package.json`], { pathOnly: true })
const cacheDir = config.cacheDir
? path.resolve(resolvedRoot, config.cacheDir)
: pkgPath
? path.join(path.dirname(pkgPath), `node_modules/.vite`)
: path.join(resolvedRoot, `.vite`)
const assetsFilter = config.assetsInclude
? createFilter(config.assetsInclude)
: () => false
// create an internal resolver to be used in special scenarios, e.g.
// optimizer & handling css @imports
const createResolver: ResolvedConfig['createResolver'] = (options) => {
let aliasContainer: PluginContainer | undefined
let resolverContainer: PluginContainer | undefined
return async (id, importer, aliasOnly, ssr) => {
let container: PluginContainer
if (aliasOnly) {
container =
aliasContainer ||
(aliasContainer = await createPluginContainer({
...resolved,
plugins: [aliasPlugin({ entries: resolved.resolve.alias })]
}))
} else {
container =
resolverContainer ||
(resolverContainer = await createPluginContainer({
...resolved,
plugins: [
aliasPlugin({ entries: resolved.resolve.alias }),
resolvePlugin({
...resolved.resolve,
root: resolvedRoot,
isProduction,
isBuild: command === 'build',
ssrConfig: resolved.ssr,
asSrc: true,
preferRelative: false,
tryIndex: true,
...options
})
]
}))
}
return (await container.resolveId(id, importer, { ssr }))?.id
}
}
const { publicDir } = config
const resolvedPublicDir =
publicDir !== false && publicDir !== ''
? path.resolve(
resolvedRoot,
typeof publicDir === 'string' ? publicDir : 'public'
)
: ''
const server = resolveServerOptions(resolvedRoot, config.server, logger)
let ssr = resolveSSROptions(config.ssr)
if (config.legacy?.buildSsrCjsExternalHeuristics) {
if (ssr) ssr.format = 'cjs'
else ssr = { target: 'node', format: 'cjs' }
}
const middlewareMode = config?.server?.middlewareMode
config = mergeConfig(config, externalConfigCompat(config, configEnv))
const optimizeDeps = config.optimizeDeps || {}
const BASE_URL = resolvedBase
const resolved: ResolvedConfig = {
...config,
configFile: configFile ? normalizePath(configFile) : undefined,
configFileDependencies: configFileDependencies.map((name) =>
normalizePath(path.resolve(name))
),
inlineConfig,
root: resolvedRoot,
base: resolvedBase,
resolve: resolveOptions,
publicDir: resolvedPublicDir,
cacheDir,
command,
mode,
ssr,
isWorker: false,
mainConfig: null,
isProduction,
plugins: userPlugins,
server,
build: resolvedBuildOptions,
preview: resolvePreviewOptions(config.preview, server),
env: {
...userEnv,
BASE_URL,
MODE: mode,
DEV: !isProduction,
PROD: isProduction
},
assetsInclude(file: string) {
return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file)
},
logger,
packageCache: new Map(),
createResolver,
optimizeDeps: {
...optimizeDeps,
esbuildOptions: {
preserveSymlinks: config.resolve?.preserveSymlinks,
...optimizeDeps.esbuildOptions
}
},
worker: resolvedWorkerOptions,
appType: config.appType ?? middlewareMode === 'ssr' ? 'custom' : 'spa',
experimental: {
importGlobRestoreExtension: false,
hmrPartialAccept: false,
...config.experimental,
buildAdvancedBaseOptions: resolvedBuildAdvancedBaseOptions
}
}
if (middlewareMode === 'ssr') {
logger.warn(
colors.yellow(
`server.middlewareMode 'ssr' is now deprecated, use server.middlewareMode true and appType 'custom'`
)
)
}
if (middlewareMode === 'html') {
logger.warn(
colors.yellow(
`server.middlewareMode 'html' is now deprecated, use server.middlewareMode true`
)
)
}
if (resolved.legacy?.buildRollupPluginCommonjs) {
const optimizerDisabled = resolved.optimizeDeps.disabled
if (!optimizerDisabled) {
resolved.optimizeDeps.disabled = 'build'
} else if (optimizerDisabled === 'dev') {
resolved.optimizeDeps.disabled = true // Also disabled during build
}
}
// Some plugins that aren't intended to work in the bundling of workers (doing post-processing at build time for example).
// And Plugins may also have cached that could be corrupted by being used in these extra rollup calls.
// So we need to separate the worker plugin from the plugin that vite needs to run.
const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] =
sortUserPlugins(config.worker?.plugins as Plugin[])
const workerResolved: ResolvedConfig = {
...resolved,
isWorker: true,
mainConfig: resolved
}
resolved.worker.plugins = await resolvePlugins(
workerResolved,
workerPrePlugins,
workerNormalPlugins,
workerPostPlugins
)
// call configResolved worker plugins hooks
await Promise.all(
resolved.worker.plugins.map((p) => p.configResolved?.(workerResolved))
)
;(resolved.plugins as Plugin[]) = await resolvePlugins(
resolved,
prePlugins,
normalPlugins,
postPlugins
)
// call configResolved hooks
await Promise.all(userPlugins.map((p) => p.configResolved?.(resolved)))
if (process.env.DEBUG) {
debug(`using resolved config: %O`, {
...resolved,
plugins: resolved.plugins.map((p) => p.name)
})
}
if (config.build?.terserOptions && config.build.minify !== 'terser') {
logger.warn(
colors.yellow(
`build.terserOptions is specified but build.minify is not set to use Terser. ` +
`Note Vite now defaults to use esbuild for minification. If you still ` +
`prefer Terser, set build.minify to "terser".`
)
)
}
// Check if all assetFileNames have the same reference.
// If not, display a warn for user.
const outputOption = config.build?.rollupOptions?.output ?? []
// Use isArray to narrow its type to array
if (Array.isArray(outputOption)) {
const assetFileNamesList = outputOption.map(
(output) => output.assetFileNames
)
if (assetFileNamesList.length > 1) {
const firstAssetFileNames = assetFileNamesList[0]
const hasDifferentReference = assetFileNamesList.some(
(assetFileNames) => assetFileNames !== firstAssetFileNames
)
if (hasDifferentReference) {
resolved.logger.warn(
colors.yellow(`
assetFileNames isn't equal for every build.rollupOptions.output. A single pattern across all outputs is supported by Vite.
`)
)
}
}
}
return resolved
}
/**
* Resolve base url. Note that some users use Vite to build for non-web targets like
* electron or expects to deploy
*/
export function resolveBaseUrl(
base: UserConfig['base'] = '/',
isBuild: boolean,
logger: Logger,
optionName: string
): string {
if (base.startsWith('.')) {
logger.warn(
colors.yellow(
colors.bold(
`(!) invalid "${optionName}" option: ${base}. The value can only be an absolute ` +
`URL, ./, or an empty string.`
)
)
)
base = '/'
}
// external URL
if (isExternalUrl(base)) {
if (!isBuild) {
// get base from full url during dev
const parsed = parseUrl(base)
base = parsed.pathname || '/'
}
} else {
// ensure leading slash
if (!base.startsWith('/')) {
logger.warn(
colors.yellow(
colors.bold(`(!) "${optionName}" option should start with a slash.`)
)
)
base = '/' + base
}
}
// ensure ending slash
if (!base.endsWith('/')) {
logger.warn(
colors.yellow(
colors.bold(`(!) "${optionName}" option should end with a slash.`)
)
)
base += '/'
}
return base
}
export function sortUserPlugins(
plugins: (Plugin | Plugin[])[] | undefined
): [Plugin[], Plugin[], Plugin[]] {
const prePlugins: Plugin[] = []
const postPlugins: Plugin[] = []
const normalPlugins: Plugin[] = []
if (plugins) {
plugins.flat().forEach((p) => {
if (p.enforce === 'pre') prePlugins.push(p)
else if (p.enforce === 'post') postPlugins.push(p)
else normalPlugins.push(p)
})
}
return [prePlugins, normalPlugins, postPlugins]
}
export async function loadConfigFromFile(
configEnv: ConfigEnv,
configFile?: string,
configRoot: string = process.cwd(),
logLevel?: LogLevel
): Promise<{
path: string
config: UserConfig
dependencies: string[]
} | null> {
const start = performance.now()
const getTime = () => `${(performance.now() - start).toFixed(2)}ms`
let resolvedPath: string | undefined
let dependencies: string[] = []
if (configFile) {
// explicit config path is always resolved from cwd
resolvedPath = path.resolve(configFile)
} else {
// implicit config file loaded from inline root (if present)
// otherwise from cwd
for (const filename of DEFAULT_CONFIG_FILES) {
const filePath = path.resolve(configRoot, filename)
if (!fs.existsSync(filePath)) continue
resolvedPath = filePath
break
}
}
if (!resolvedPath) {
debug('no config file found.')
return null
}
let isESM = false
if (/\.m[jt]s$/.test(resolvedPath)) {
isESM = true
} else if (/\.c[jt]s$/.test(resolvedPath)) {
isESM = false
} else {
// check package.json for type: "module" and set `isESM` to true
try {
const pkg = lookupFile(configRoot, ['package.json'])
isESM = !!pkg && JSON.parse(pkg).type === 'module'
} catch (e) {}
}
try {
let userConfig: UserConfigExport | undefined
if (isESM) {
const fileUrl = pathToFileURL(resolvedPath)
const bundled = await bundleConfigFile(resolvedPath, true)
dependencies = bundled.dependencies
if (isTS(resolvedPath)) {
// before we can register loaders without requiring users to run node
// with --experimental-loader themselves, we have to do a hack here:
// bundle the config file w/ ts transforms first, write it to disk,
// load it with native Node ESM, then delete the file.
fs.writeFileSync(resolvedPath + '.mjs', bundled.code)
userConfig = (await dynamicImport(`${fileUrl}.mjs?t=${Date.now()}`))
.default
fs.unlinkSync(resolvedPath + '.mjs')
debug(`TS + native esm config loaded in ${getTime()}`, fileUrl)
} else {
// using Function to avoid this from being compiled away by TS/Rollup
// append a query so that we force reload fresh config in case of
// server restart
userConfig = (await dynamicImport(`${fileUrl}?t=${Date.now()}`)).default
debug(`native esm config loaded in ${getTime()}`, fileUrl)
}
}
if (!userConfig) {
// Bundle config file and transpile it to cjs using esbuild.
const bundled = await bundleConfigFile(resolvedPath)
dependencies = bundled.dependencies
userConfig = await loadConfigFromBundledFile(resolvedPath, bundled.code)
debug(`bundled config file loaded in ${getTime()}`)
}
const config = await (typeof userConfig === 'function'
? userConfig(configEnv)
: userConfig)
if (!isObject(config)) {
throw new Error(`config must export or return an object.`)
}
return {
path: normalizePath(resolvedPath),
config,
dependencies
}
} catch (e) {
createLogger(logLevel).error(
colors.red(`failed to load config from ${resolvedPath}`),
{ error: e }
)
throw e
}
}
async function bundleConfigFile(
fileName: string,
isESM = false
): Promise<{ code: string; dependencies: string[] }> {
const importMetaUrlVarName = '__vite_injected_original_import_meta_url'
const result = await build({
absWorkingDir: process.cwd(),
entryPoints: [fileName],
outfile: 'out.js',
write: false,
platform: 'node',
bundle: true,
format: isESM ? 'esm' : 'cjs',
sourcemap: 'inline',
metafile: true,
define: {
'import.meta.url': importMetaUrlVarName
},
plugins: [
{
name: 'externalize-deps',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
const id = args.path
if (id[0] !== '.' && !path.isAbsolute(id)) {
return {
external: true
}
}
})
}
},
{
name: 'inject-file-scope-variables',
setup(build) {
build.onLoad({ filter: /\.[cm]?[jt]s$/ }, async (args) => {
const contents = await fs.promises.readFile(args.path, 'utf8')
const injectValues =
`const __dirname = ${JSON.stringify(path.dirname(args.path))};` +
`const __filename = ${JSON.stringify(args.path)};` +
`const ${importMetaUrlVarName} = ${JSON.stringify(
pathToFileURL(args.path).href
)};`
return {
loader: isTS(args.path) ? 'ts' : 'js',
contents: injectValues + contents
}
})
}
}
]
})
const { text } = result.outputFiles[0]
return {
code: text,
dependencies: result.metafile ? Object.keys(result.metafile.inputs) : []
}
}
interface NodeModuleWithCompile extends NodeModule {
_compile(code: string, filename: string): any
}
const _require = createRequire(import.meta.url)
async function loadConfigFromBundledFile(
fileName: string,
bundledCode: string
): Promise<UserConfig> {
const realFileName = fs.realpathSync(fileName)
const defaultLoader = _require.extensions['.js']
_require.extensions['.js'] = (module: NodeModule, filename: string) => {
if (filename === realFileName) {
;(module as NodeModuleWithCompile)._compile(bundledCode, filename)
} else {
defaultLoader(module, filename)
}