Skip to content

Commit

Permalink
[chore] shared Vite build config
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed May 29, 2022
1 parent 712b4d8 commit 0b12c61
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 93 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-numbers-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[chore] shared Vite build config
60 changes: 10 additions & 50 deletions packages/kit/src/core/build/build_client.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
import fs from 'fs';
import path from 'path';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { deep_merge } from '../../utils/object.js';
import { print_config_conflicts } from '../config/index.js';
import { get_aliases } from '../utils.js';
import { create_build, find_deps } from './utils.js';
import { create_build, find_deps, get_default_config } from './utils.js';
import { posixify } from '../../utils/filesystem.js';

/**
* @param {{
* cwd: string;
* assets_base: string;
* config: import('types').ValidatedConfig
* manifest_data: import('types').ManifestData
* config: import('types').ValidatedConfig;
* manifest_data: import('types').ManifestData;
* output_dir: string;
* client_entry_file: string;
* service_worker_entry_file: string | null;
* service_worker_register: boolean;
* }} options
*/
export async function build_client({
cwd,
assets_base,
config,
manifest_data,
output_dir,
client_entry_file
}) {
export async function build_client(options) {
const { cwd, config, manifest_data, output_dir, client_entry_file } = options;

process.env.VITE_SVELTEKIT_APP_VERSION = config.kit.version.name;
process.env.VITE_SVELTEKIT_APP_VERSION_FILE = `${config.kit.appDir}/version.json`;
process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = `${config.kit.version.pollInterval}`;
Expand All @@ -52,43 +45,10 @@ export async function build_client({
});

/** @type {[any, string[]]} */
const [merged_config, conflicts] = deep_merge(await config.kit.vite(), {
configFile: false,
root: cwd,
base: assets_base,
build: {
cssCodeSplit: true,
manifest: true,
outDir: `${client_out_dir}/immutable`,
polyfillDynamicImport: false,
rollupOptions: {
input,
output: {
entryFileNames: '[name]-[hash].js',
chunkFileNames: 'chunks/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash][extname]'
},
preserveEntrySignatures: 'strict'
}
},
resolve: {
alias: get_aliases(config)
},
plugins: [
svelte({
...config,
emitCss: true,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
},
configFile: false
})
],
// prevent Vite copying the contents of `config.kit.files.assets`,
// if it happens to be 'public' instead of 'static'
publicDir: false
});
const [merged_config, conflicts] = deep_merge(
await config.kit.vite(),
get_default_config({ ...options, client_out_dir, input, ssr: false })
);

print_config_conflicts(conflicts, 'kit.vite.', 'build_client');

Expand Down
53 changes: 10 additions & 43 deletions packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import fs from 'fs';
import path from 'path';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { mkdirp, posixify } from '../../utils/filesystem.js';
import { deep_merge } from '../../utils/object.js';
import { load_template, print_config_conflicts } from '../config/index.js';
import { get_aliases, get_runtime_path, resolve_entry } from '../utils.js';
import { create_build, find_deps } from './utils.js';
import { get_runtime_path, resolve_entry } from '../utils.js';
import { create_build, find_deps, get_default_config } from './utils.js';
import { s } from '../../utils/misc.js';

/**
Expand Down Expand Up @@ -117,19 +116,17 @@ export class Server {
* }} options
* @param {{ vite_manifest: import('vite').Manifest, assets: import('rollup').OutputAsset[] }} client
*/
export async function build_server(
{
export async function build_server(options, client) {
const {
cwd,
assets_base,
config,
manifest_data,
build_dir,
output_dir,
service_worker_entry_file,
service_worker_register
},
client
) {
} = options;

let hooks_file = resolve_entry(config.kit.files.hooks);
if (!hooks_file || !fs.existsSync(hooks_file)) {
hooks_file = path.join(config.kit.outDir, 'build/hooks.js');
Expand Down Expand Up @@ -211,40 +208,10 @@ export async function build_server(
const [modified_vite_config] = deep_merge(default_config, vite_config);

/** @type {[any, string[]]} */
const [merged_config, conflicts] = deep_merge(modified_vite_config, {
configFile: false,
root: cwd,
base: assets_base,
build: {
ssr: true,
outDir: `${output_dir}/server`,
manifest: true,
polyfillDynamicImport: false,
rollupOptions: {
input,
output: {
format: 'esm',
entryFileNames: '[name].js',
chunkFileNames: 'chunks/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash][extname]'
},
preserveEntrySignatures: 'strict'
}
},
plugins: [
svelte({
...config,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
},
configFile: false
})
],
resolve: {
alias: get_aliases(config)
}
});
const [merged_config, conflicts] = deep_merge(
modified_vite_config,
get_default_config({ ...options, input, ssr: true })
);

print_config_conflicts(conflicts, 'kit.vite.', 'build_server');

Expand Down
62 changes: 62 additions & 0 deletions packages/kit/src/core/build/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { svelte } from '@sveltejs/vite-plugin-svelte';
import * as vite from 'vite';
import { get_aliases } from '../utils.js';

const cwd = process.cwd();

/**
* @typedef {import('rollup').RollupOutput} RollupOutput
Expand Down Expand Up @@ -41,3 +45,61 @@ export function find_deps(file, manifest, js, css) {
chunk.imports.forEach((file) => find_deps(file, manifest, js, css));
}
}

/**
* @param {{
* assets_base: string;
* client_out_dir?: string;
* config: import('types').ValidatedConfig;
* input: Record<string, string>;
* output_dir: string;
* ssr: boolean;
* }} options
* @return {import('vite').UserConfig}
*/
export const get_default_config = function ({
assets_base,
client_out_dir,
config,
input,
output_dir,
ssr
}) {
return {
base: assets_base,
build: {
cssCodeSplit: true,
manifest: true,
outDir: ssr ? `${output_dir}/server` : `${client_out_dir}/immutable`,
polyfillDynamicImport: false,
rollupOptions: {
input,
output: {
format: 'esm',
entryFileNames: ssr ? '[name].js' : '[name]-[hash].js',
chunkFileNames: 'chunks/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash][extname]'
},
preserveEntrySignatures: 'strict'
},
ssr
},
plugins: [
svelte({
...config,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
},
configFile: false
})
],
// prevent Vite copying the contents of `config.kit.files.assets`,
// if it happens to be 'public' instead of 'static'
publicDir: false,
resolve: {
alias: get_aliases(config)
},
root: cwd
};
};

0 comments on commit 0b12c61

Please sign in to comment.