Skip to content

Commit ac3669e

Browse files
author
Rich Harris
authored
remove vite.config.js files (#569)
* retire vite.config.js in favour of config.kit.vite - fixes #509 * changeset * message for people with a vite.config.js * add configFile: false * add docs for config.kit.vite * fix ssr config * update user-facing config type
1 parent 472d1d7 commit ac3669e

File tree

25 files changed

+147
-148
lines changed

25 files changed

+147
-148
lines changed

.changeset/swift-pumpkins-pump.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'create-svelte': patch
3+
'@sveltejs/kit': patch
4+
---
5+
6+
Move Vite config into svelte.config.cjs

documentation/docs/13-configuration.md

+19-14
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ module.exports = {
3737
force: false,
3838
pages: ['*']
3939
},
40-
target: null
40+
target: null,
41+
vite: () => ({})
4142
},
4243

4344
// options passed to svelte.preprocess (https://svelte.dev/docs#svelte_preprocess)
@@ -61,12 +62,12 @@ The directory relative to `paths.assets` where the built JS and CSS (and importe
6162

6263
An object containing zero or more of the following `string` values:
6364

64-
* `assets` — a place to put static files that should have stable URLs and undergo no processing, such as `favicon.ico` or `manifest.json`
65-
* `lib` — your app's internal library, accessible throughout the codebase as `$lib`
66-
* `routes` — the files that define the structure of your app (see [Routing](#routing))
67-
* `serviceWorker` — the location of your service worker's entry point (see [Service workers](#service-workers))
68-
* `setup` — the location of your setup file (see [Setup](#setup))
69-
* `template` — the location of the template for HTML responses
65+
- `assets` — a place to put static files that should have stable URLs and undergo no processing, such as `favicon.ico` or `manifest.json`
66+
- `lib` — your app's internal library, accessible throughout the codebase as `$lib`
67+
- `routes` — the files that define the structure of your app (see [Routing](#routing))
68+
- `serviceWorker` — the location of your service worker's entry point (see [Service workers](#service-workers))
69+
- `setup` — the location of your setup file (see [Setup](#setup))
70+
- `template` — the location of the template for HTML responses
7071

7172
#### host
7273

@@ -91,18 +92,22 @@ module.exports = {
9192

9293
An object containing zero or more of the following `string` values:
9394

94-
* `assets` — an absolute path, or a path relative to `base`, where your app's files are served from. This is useful if your files are served from a storage bucket of some kind
95-
* `base` — a root-relative (i.e. starts with `/`) path that specifies where your app is served from. This allows the app to live on a non-root path
95+
- `assets` — an absolute path, or a path relative to `base`, where your app's files are served from. This is useful if your files are served from a storage bucket of some kind
96+
- `base` — a root-relative (i.e. starts with `/`) path that specifies where your app is served from. This allows the app to live on a non-root path
9697

9798
#### prerender
9899

99100
See [Prerendering](#prerendering). An object containing zero or more of the following:
100101

101-
* `crawl` — determines whether SvelteKit should find pages to prerender by following links from the seed page(s)
102-
* `enabled` — set to `false` to disable prerendering altogether
103-
* `force` — if `true`, a page that fails to render will _not_ cause the entire build to fail
104-
* `pages` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]` )
102+
- `crawl` — determines whether SvelteKit should find pages to prerender by following links from the seed page(s)
103+
- `enabled` — set to `false` to disable prerendering altogether
104+
- `force` — if `true`, a page that fails to render will _not_ cause the entire build to fail
105+
- `pages` — an array of pages to prerender, or start crawling from (if `crawl: true`). The `*` string includes all non-dynamic routes (i.e. pages with no `[parameters]` )
105106

106107
#### target
107108

108-
Specifies an element to mount the app to. It must be a DOM selector that identifies an element that exists in your template file. If unspecified, the app will be mounted to `document.body`.
109+
Specifies an element to mount the app to. It must be a DOM selector that identifies an element that exists in your template file. If unspecified, the app will be mounted to `document.body`.
110+
111+
#### vite
112+
113+
A [Vite config object](https://vitejs.dev/config), or a function that returns one. Not all configuration options can be set, since SvelteKit depends on certain values being configured internally.
-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module.exports = {
22
kit: {
33
adapter: '@sveltejs/adapter-netlify',
4-
5-
// hydrate the <div id="svelte"> element in src/app.html
64
target: '#svelte'
75
}
86
};

examples/hn.svelte.dev/vite.config.js

-13
This file was deleted.

examples/realworld.svelte.dev/svelte.config.cjs

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ module.exports = {
66
adapter: '@sveltejs/adapter-node',
77

88
// hydrate the <div id="svelte"> element in src/app.html
9-
target: '#svelte'
9+
target: '#svelte',
10+
11+
vite: {
12+
ssr: {
13+
noExternal: ['node-fetch']
14+
}
15+
}
1016
}
1117
};

examples/realworld.svelte.dev/vite.config.js

-13
This file was deleted.

examples/sandbox/svelte.config.cjs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
const pkg = require('./package.json');
2+
13
module.exports = {
24
kit: {
3-
// By default, `npm run build` will create a standard Node app.
4-
// You can create optimized builds for different platforms by
5-
// specifying a different adapter
65
adapter: '@sveltejs/adapter-node',
7-
8-
// hydrate the <div id="svelte"> element in src/app.html
9-
target: '#svelte'
6+
target: '#svelte',
7+
vite: {
8+
build: {
9+
minify: false
10+
},
11+
ssr: {
12+
noExternal: Object.keys(pkg.dependencies || {})
13+
}
14+
}
1015
}
1116
};

examples/sandbox/vite.config.js

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
module.exports = {
22
kit: {
3-
// By default, `npm run build` will create a standard Node app.
4-
// You can create optimized builds for different platforms by
5-
// specifying a different adapter
63
adapter: '@sveltejs/adapter-node',
7-
8-
// hydrate the <div id="svelte"> element in src/app.html
94
target: '#svelte'
105
}
116
};

examples/svelte-kit-demo/vite.config.js

-13
This file was deleted.

packages/create-svelte/template/svelte.config.cjs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const pkg = require('./package.json');
2+
13
/** @type {import('@sveltejs/kit').Config} */
24
module.exports = {
35
kit: {
@@ -7,6 +9,12 @@ module.exports = {
79
adapter: '@sveltejs/adapter-node',
810

911
// hydrate the <div id="svelte"> element in src/app.html
10-
target: '#svelte'
12+
target: '#svelte',
13+
14+
vite: {
15+
ssr: {
16+
noExternal: Object.keys(pkg.dependencies || {})
17+
}
18+
}
1119
}
1220
};

packages/create-svelte/template/vite.config.js

-13
This file was deleted.

packages/kit/src/cli.js

+6-20
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,13 @@ async function get_config() {
88
if (existsSync('snowpack.config.js') || existsSync('snowpack.config.cjs')) {
99
// prettier-ignore
1010
console.error(colors.bold().red(
11-
'SvelteKit now uses https://vitejs.dev. Please replace snowpack.config.js with vite.config.js:'
11+
'SvelteKit now uses https://vitejs.dev. Please remove snowpack.config.js and put Vite config in svelte.config.cjs: https://kit.svelte.dev/docs#configuration-vite'
1212
));
13-
13+
} else if (existsSync('vite.config.js')) {
1414
// prettier-ignore
15-
console.error(`
16-
// Consult https://vitejs.dev/config/ to learn about these options
17-
import { join } from 'path';
18-
import { readFileSync } from 'fs';
19-
import { cwd } from 'process';
20-
21-
const pkg = JSON.parse(readFileSync(join(cwd(), 'package.json')));
22-
23-
/** @type {import('vite').UserConfig} */
24-
export default {
25-
ssr: {
26-
noExternal: Object.keys(pkg.dependencies || {})
27-
}
28-
};
29-
30-
`.replace(/^\t{3}/gm, '').replace(/\t/gm, ' ').trim());
15+
console.error(colors.bold().red(
16+
'Please remove vite.config.js and put Vite config in svelte.config.cjs: https://kit.svelte.dev/docs#configuration-vite'
17+
));
3118
}
3219

3320
try {
@@ -38,8 +25,7 @@ async function get_config() {
3825
if (error.code === 'MODULE_NOT_FOUND') {
3926
if (existsSync('svelte.config.js')) {
4027
// TODO this is temporary, for the benefit of early adopters
41-
message =
42-
'You must rename svelte.config.js to svelte.config.cjs, and snowpack.config.js to snowpack.config.cjs';
28+
message = 'You must rename svelte.config.js to svelte.config.cjs';
4329
} else {
4430
message = 'Missing svelte.config.cjs';
4531
}

packages/kit/src/core/build/index.js

+36-4
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,16 @@ async function build_client({
109109
});
110110
});
111111

112-
// client build
112+
/** @type {any} */
113+
const user_config = config.kit.vite();
114+
113115
await vite.build({
116+
...user_config,
117+
configFile: false,
114118
root: cwd,
115119
base,
116120
build: {
121+
...user_config.build,
117122
cssCodeSplit: true,
118123
manifest: true,
119124
lib: {
@@ -126,6 +131,7 @@ async function build_client({
126131
},
127132
outDir: client_out_dir,
128133
rollupOptions: {
134+
...(user_config.build && user_config.build.rollupOptions),
129135
input,
130136
output: {
131137
entryFileNames: '[name]-[hash].js',
@@ -136,12 +142,15 @@ async function build_client({
136142
}
137143
},
138144
resolve: {
145+
...user_config.resolve,
139146
alias: {
147+
...(user_config.resolve && user_config.resolve.alias),
140148
$app: path.resolve(`${build_dir}/runtime/app`),
141149
$lib: config.kit.files.lib
142150
}
143151
},
144152
plugins: [
153+
...(user_config.plugins || []),
145154
svelte({
146155
extensions: config.extensions
147156
})
@@ -370,26 +379,35 @@ async function build_server(
370379
.trim()
371380
);
372381

382+
/** @type {any} */
383+
const user_config = config.kit.vite();
384+
373385
await vite.build({
386+
...user_config,
387+
configFile: false,
374388
root: cwd,
375389
base,
376390
build: {
391+
target: 'es2018',
392+
...user_config.build,
377393
ssr: true,
378394
lib: {
379395
entry: app_file,
380396
name: 'app',
381397
formats: ['es']
382398
},
383-
outDir: `${output_dir}/server`,
384-
target: 'es2018'
399+
outDir: `${output_dir}/server`
385400
},
386401
resolve: {
402+
...user_config.resolve,
387403
alias: {
404+
...(user_config.resolve && user_config.resolve.alias),
388405
$app: path.resolve(`${build_dir}/runtime/app`),
389406
$lib: config.kit.files.lib
390407
}
391408
},
392409
plugins: [
410+
...(user_config.plugins || []),
393411
svelte({
394412
extensions: config.extensions
395413
})
@@ -399,7 +417,12 @@ async function build_server(
399417
// so we need to ignore the fact that it's missing
400418
// @ts-ignore
401419
ssr: {
402-
noExternal: ['svelte', '@sveltejs/kit']
420+
...user_config.ssr,
421+
noExternal: [
422+
'svelte',
423+
'@sveltejs/kit',
424+
...((user_config.ssr && user_config.ssr.noExternal) || [])
425+
]
403426
},
404427
optimizeDeps: {
405428
entries: []
@@ -457,16 +480,23 @@ async function build_service_worker(
457480
.trim()
458481
);
459482

483+
/** @type {any} */
484+
const user_config = config.kit.vite();
485+
460486
await vite.build({
487+
...user_config,
488+
configFile: false,
461489
root: cwd,
462490
base,
463491
build: {
492+
...user_config.build,
464493
lib: {
465494
entry: service_worker_entry_file,
466495
name: 'app',
467496
formats: ['es']
468497
},
469498
rollupOptions: {
499+
...(user_config.build && user_config.build.rollupOptions),
470500
output: {
471501
entryFileNames: 'service-worker.js'
472502
}
@@ -475,7 +505,9 @@ async function build_service_worker(
475505
emptyOutDir: false
476506
},
477507
resolve: {
508+
...user_config.resolve,
478509
alias: {
510+
...(user_config.resolve && user_config.resolve.alias),
479511
'$service-worker': path.resolve(`${build_dir}/runtime/service-worker`)
480512
}
481513
},

0 commit comments

Comments
 (0)