11import { fork } from 'node:child_process' ;
22import fs from 'node:fs' ;
33import path from 'node:path' ;
4- import colors from 'kleur' ;
4+ import { fileURLToPath } from 'node:url' ;
5+
56import { svelte } from '@sveltejs/vite-plugin-svelte' ;
7+ import colors from 'kleur' ;
68import * as vite from 'vite' ;
9+
710import { mkdirp , posixify , resolve_entry , rimraf } from '../../utils/filesystem.js' ;
11+ import { SVELTE_KIT_ASSETS } from '../../constants.js' ;
12+ import { create_static_module , create_dynamic_module } from '../../core/env.js' ;
813import * as sync from '../../core/sync/sync.js' ;
14+ import { create_assets } from '../../core/sync/create_manifest_data/index.js' ;
15+ import { runtime_base , runtime_directory , runtime_prefix , logger } from '../../core/utils.js' ;
16+ import { load_config } from '../../core/config/index.js' ;
17+ import { generate_manifest } from '../../core/generate_manifest/index.js' ;
918import { build_server } from './build/build_server.js' ;
1019import { build_service_worker } from './build/build_service_worker.js' ;
11- import { load_config } from '../../core/config/index .js' ;
20+ import { find_deps , get_build_setup_config , get_build_compile_config } from './build/utils .js' ;
1221import { dev } from './dev/index.js' ;
13- import { generate_manifest } from '../../core/generate_manifest/index.js' ;
14- import { runtime_directory , logger } from '../../core/utils.js' ;
15- import { find_deps , get_default_build_config } from './build/utils.js' ;
22+ import { is_illegal , module_guard , normalize_id } from './graph_analysis/index.js' ;
1623import { preview } from './preview/index.js' ;
1724import { get_config_aliases , get_app_aliases , get_env } from './utils.js' ;
18- import { fileURLToPath } from 'node:url' ;
19- import { create_static_module , create_dynamic_module } from '../../core/env.js' ;
20- import { is_illegal , module_guard , normalize_id } from './graph_analysis/index.js' ;
21- import { create_assets } from '../../core/sync/create_manifest_data/index.js' ;
2225
2326export { vitePreprocess } from '@sveltejs/vite-plugin-svelte' ;
2427
@@ -168,7 +171,7 @@ function kit({ svelte_config }) {
168171 }
169172 } ) ;
170173
171- return get_default_build_config ( {
174+ return get_build_compile_config ( {
172175 config : svelte_config ,
173176 input,
174177 ssr : false ,
@@ -200,8 +203,8 @@ function kit({ svelte_config }) {
200203 check_vite_version ( ) ;
201204
202205 /** @type {import('vite').Plugin } */
203- const plugin_build = {
204- name : 'vite-plugin-sveltekit-build ' ,
206+ const plugin_setup = {
207+ name : 'vite-plugin-sveltekit-setup ' ,
205208
206209 /**
207210 * Build the SvelteKit-provided Vite config to be merged with the user's vite.config.js file.
@@ -226,7 +229,7 @@ function kit({ svelte_config }) {
226229 if ( is_build ) {
227230 manifest_data = ( await sync . all ( svelte_config , config_env . mode ) ) . manifest_data ;
228231
229- const new_config = vite_client_build_config ( ) ;
232+ const new_config = get_build_setup_config ( { config : svelte_config , ssr : false } ) ;
230233
231234 const warning = warn_overridden_config ( config , new_config ) ;
232235 if ( warning ) console . error ( warning + '\n' ) ;
@@ -252,20 +255,10 @@ function kit({ svelte_config }) {
252255 // dev and preview config can be shared
253256 /** @type {import('vite').UserConfig } */
254257 const result = {
255- appType : 'custom' ,
256- base : svelte_config . kit . paths . base ,
257- build : {
258- rollupOptions : {
259- // Vite dependency crawler needs an explicit JS entry point
260- // eventhough server otherwise works without it
261- input : `${ runtime_directory } /client/start.js`
262- }
263- } ,
264258 define : {
265259 __SVELTEKIT_APP_VERSION_POLL_INTERVAL__ : '0' ,
266260 __SVELTEKIT_EMBEDDED__ : svelte_config . kit . embedded ? 'true' : 'false'
267261 } ,
268- publicDir : svelte_config . kit . files . assets ,
269262 resolve : {
270263 alias : [ ...get_app_aliases ( svelte_config . kit ) , ...get_config_aliases ( svelte_config . kit ) ]
271264 } ,
@@ -554,12 +547,68 @@ function kit({ svelte_config }) {
554547 fs . unlinkSync ( `${ paths . output_dir } /client/${ vite_config . build . manifest } ` ) ;
555548 fs . unlinkSync ( `${ paths . output_dir } /server/${ vite_config . build . manifest } ` ) ;
556549 }
550+ } ,
551+
552+ /**
553+ * @see https://vitejs.dev/guide/api-plugin.html#configureserver
554+ */
555+ async configureServer ( vite ) {
556+ // set `import { version } from '$app/environment'`
557+ ( await vite . ssrLoadModule ( `${ runtime_prefix } /env.js` ) ) . set_version (
558+ svelte_config . kit . version . name
559+ ) ;
560+
561+ // set `import { base, assets } from '$app/paths'`
562+ const { base, assets } = svelte_config . kit . paths ;
563+
564+ ( await vite . ssrLoadModule ( `${ runtime_base } /paths.js` ) ) . set_paths ( {
565+ base,
566+ assets : assets ? SVELTE_KIT_ASSETS : base
567+ } ) ;
557568 }
558569 } ;
559570
560571 /** @type {import('vite').Plugin } */
561- const plugin_middleware = {
562- name : 'vite-plugin-sveltekit-middleware' ,
572+ const plugin_compile = {
573+ name : 'vite-plugin-sveltekit-compile' ,
574+
575+ /**
576+ * Build the SvelteKit-provided Vite config to be merged with the user's vite.config.js file.
577+ * @see https://vitejs.dev/guide/api-plugin.html#config
578+ */
579+ async config ( config , config_env ) {
580+ // The config is created in build_server for SSR mode and passed inline
581+ if ( config . build ?. ssr ) return ;
582+
583+ if ( config_env . command === 'build' ) {
584+ const new_config = vite_client_build_config ( ) ;
585+
586+ const warning = warn_overridden_config ( config , new_config ) ;
587+ if ( warning ) console . error ( warning + '\n' ) ;
588+
589+ return new_config ;
590+ }
591+
592+ // dev and preview config can be shared
593+ /** @type {import('vite').UserConfig } */
594+ const result = {
595+ appType : 'custom' ,
596+ base : svelte_config . kit . paths . base ,
597+ build : {
598+ rollupOptions : {
599+ // Vite dependency crawler needs an explicit JS entry point
600+ // eventhough server otherwise works without it
601+ input : `${ runtime_directory } /client/start.js`
602+ }
603+ } ,
604+ publicDir : svelte_config . kit . files . assets
605+ } ;
606+
607+ const warning = warn_overridden_config ( config , result ) ;
608+ if ( warning ) console . error ( warning ) ;
609+
610+ return result ;
611+ } ,
563612
564613 /**
565614 * Adds the SvelteKit middleware to do SSR in dev mode.
@@ -578,7 +627,7 @@ function kit({ svelte_config }) {
578627 }
579628 } ;
580629
581- return [ plugin_build , plugin_middleware ] ;
630+ return [ plugin_setup , plugin_compile ] ;
582631}
583632
584633function check_vite_version ( ) {
@@ -634,8 +683,12 @@ function warn_overridden_config(config, resolved_config) {
634683 * @param {string[] } out used locally to compute the return value
635684 */
636685function find_overridden_config ( config , resolved_config , enforced_config , path , out ) {
686+ if ( config == null || resolved_config == null ) {
687+ return out ;
688+ }
689+
637690 for ( const key in enforced_config ) {
638- if ( typeof config === 'object' && config !== null && key in config ) {
691+ if ( typeof config === 'object' && key in config && key in resolved_config ) {
639692 const enforced = enforced_config [ key ] ;
640693
641694 if ( enforced === true ) {
@@ -647,7 +700,6 @@ function find_overridden_config(config, resolved_config, enforced_config, path,
647700 }
648701 }
649702 }
650-
651703 return out ;
652704}
653705
0 commit comments