Skip to content

Commit

Permalink
retire vite.config.js in favour of config.kit.vite - fixes #509
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Mar 18, 2021
1 parent 495e61f commit 7968431
Show file tree
Hide file tree
Showing 21 changed files with 104 additions and 111 deletions.
2 changes: 0 additions & 2 deletions examples/hn.svelte.dev/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module.exports = {
kit: {
adapter: '@sveltejs/adapter-netlify',

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
}
};
13 changes: 0 additions & 13 deletions examples/hn.svelte.dev/vite.config.js

This file was deleted.

8 changes: 7 additions & 1 deletion examples/realworld.svelte.dev/svelte.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ module.exports = {
adapter: '@sveltejs/adapter-node',

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
target: '#svelte',

vite: {
ssr: {
noExternal: ['node-fetch']
}
}
}
};
13 changes: 0 additions & 13 deletions examples/realworld.svelte.dev/vite.config.js

This file was deleted.

17 changes: 11 additions & 6 deletions examples/sandbox/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const pkg = require('./package.json');

module.exports = {
kit: {
// By default, `npm run build` will create a standard Node app.
// You can create optimized builds for different platforms by
// specifying a different adapter
adapter: '@sveltejs/adapter-node',

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
target: '#svelte',
vite: {
build: {
minify: false
},
ssr: {
noExternal: Object.keys(pkg.dependencies || {})
}
}
}
};
16 changes: 0 additions & 16 deletions examples/sandbox/vite.config.js

This file was deleted.

5 changes: 0 additions & 5 deletions examples/svelte-kit-demo/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
module.exports = {
kit: {
// By default, `npm run build` will create a standard Node app.
// You can create optimized builds for different platforms by
// specifying a different adapter
adapter: '@sveltejs/adapter-node',

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
}
};
13 changes: 0 additions & 13 deletions examples/svelte-kit-demo/vite.config.js

This file was deleted.

10 changes: 9 additions & 1 deletion packages/create-svelte/template/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const pkg = require('./package.json');

/** @type {import('@sveltejs/kit').Config} */
module.exports = {
kit: {
Expand All @@ -7,6 +9,12 @@ module.exports = {
adapter: '@sveltejs/adapter-node',

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
target: '#svelte',

vite: {
ssr: {
noExternal: Object.keys(pkg.dependencies || {})
}
}
}
};
13 changes: 0 additions & 13 deletions packages/create-svelte/template/vite.config.js

This file was deleted.

26 changes: 25 additions & 1 deletion packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,15 @@ async function build_client({
});
});

// client build
/** @type {any} */
const user_config = config.kit.vite();

await vite.build({
...user_config,
root: cwd,
base,
build: {
...user_config.build,
cssCodeSplit: true,
manifest: true,
lib: {
Expand All @@ -126,6 +130,7 @@ async function build_client({
},
outDir: client_out_dir,
rollupOptions: {
...(user_config.build && user_config.build.rollupOptions),
input,
output: {
entryFileNames: '[name]-[hash].js',
Expand All @@ -136,12 +141,15 @@ async function build_client({
}
},
resolve: {
...user_config.resolve,
alias: {
...(user_config.resolve && user_config.resolve.alias),
$app: path.resolve(`${build_dir}/runtime/app`),
$lib: config.kit.files.lib
}
},
plugins: [
...(user_config.plugins || []),
svelte({
extensions: config.extensions
})
Expand Down Expand Up @@ -370,10 +378,15 @@ async function build_server(
.trim()
);

/** @type {any} */
const user_config = config.kit.vite();

await vite.build({
...user_config,
root: cwd,
base,
build: {
...user_config.build,
ssr: true,
lib: {
entry: app_file,
Expand All @@ -384,12 +397,15 @@ async function build_server(
target: 'es2018'
},
resolve: {
...user_config.resolve,
alias: {
...(user_config.resolve && user_config.resolve.alias),
$app: path.resolve(`${build_dir}/runtime/app`),
$lib: config.kit.files.lib
}
},
plugins: [
...(user_config.plugins || []),
svelte({
extensions: config.extensions
})
Expand Down Expand Up @@ -457,16 +473,22 @@ async function build_service_worker(
.trim()
);

/** @type {any} */
const user_config = config.kit.vite();

await vite.build({
...user_config,
root: cwd,
base,
build: {
...user_config.build,
lib: {
entry: service_worker_entry_file,
name: 'app',
formats: ['es']
},
rollupOptions: {
...(user_config.build && user_config.build.rollupOptions),
output: {
entryFileNames: 'service-worker.js'
}
Expand All @@ -475,7 +497,9 @@ async function build_service_worker(
emptyOutDir: false
},
resolve: {
...user_config.resolve,
alias: {
...(user_config.resolve && user_config.resolve.alias),
'$service-worker': path.resolve(`${build_dir}/runtime/service-worker`)
}
},
Expand Down
9 changes: 9 additions & 0 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,33 @@ class Watcher extends EventEmitter {
}

async init_server() {
/** @type {any} */
const user_config = (this.config.kit.vite && this.config.kit.vite()) || {};

/**
* @type {vite.ViteDevServer}
*/
this.viteDevServer = await vite.createServer({
...user_config,
configFile: false,
root: this.cwd,
resolve: {
...user_config.resolve,
alias: {
...(user_config.resolve && user_config.resolve.alias),
$app: path.resolve(`${this.dir}/runtime/app`),
$lib: this.config.kit.files.lib
}
},
plugins: [
...(user_config.plugins || []),
svelte({
extensions: this.config.extensions
})
],
publicDir: this.config.kit.files.assets,
server: {
...user_config.server,
middlewareMode: true
}
});
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/src/core/load_config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { validate_config } from './index.js';
test('fills in defaults', () => {
const validated = validate_config({});

delete validated.kit.vite;

assert.equal(validated, {
compilerOptions: null,
extensions: ['.svelte'],
Expand Down Expand Up @@ -77,6 +79,10 @@ test('fills in partial blanks', () => {
}
});

assert.equal(validated.kit.vite(), {});

delete validated.kit.vite;

assert.equal(validated, {
compilerOptions: null,
extensions: ['.svelte'],
Expand Down
21 changes: 20 additions & 1 deletion packages/kit/src/core/load_config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,26 @@ const options = {
}
},

target: expect_string(null)
target: expect_string(null),

vite: {
type: 'leaf',
default: () => ({}),
validate: (option, keypath) => {
if (typeof option === 'object') {
const config = option;
option = () => config;
}

if (typeof option !== 'function') {
throw new Error(
`${keypath} must be a Vite config object (https://vitejs.dev/config) or a function that returns one`
);
}

return option;
}
}
}
},

Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/load_config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ suite('load default config', async () => {

const config = await load_config({ cwd });

delete config.kit.vite; // can't test equality of a function

assert.equal(config, {
compilerOptions: null,
extensions: ['.svelte'],
Expand Down
10 changes: 7 additions & 3 deletions packages/kit/test/apps/basics/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module.exports = {
kit: {
// TODO adapterless builds
adapter: '@sveltejs/adapter-node',

hostHeader: 'x-forwarded-host'
hostHeader: 'x-forwarded-host',
vite: {
build: {
minify: false
},
clearScreen: false
}
}
};
6 changes: 0 additions & 6 deletions packages/kit/test/apps/basics/vite.config.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/kit/test/apps/custom-extension/vite.config.js

This file was deleted.

Loading

0 comments on commit 7968431

Please sign in to comment.