Skip to content

Commit

Permalink
fix #5308
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 2, 2022
1 parent fb02207 commit 41c0ebe
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 61 deletions.
27 changes: 5 additions & 22 deletions packages/kit/src/vite/build/build_server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import { mkdirp, posixify } from '../../utils/filesystem.js';
import { merge_vite_configs, resolve_entry } from '../utils.js';
import { get_vite_config, merge_vite_configs, resolve_entry } from '../utils.js';
import { load_template } from '../../core/config/index.js';
import { get_runtime_path } from '../../core/utils.js';
import { create_build, find_deps, get_default_config, remove_svelte_kit } from './utils.js';
Expand Down Expand Up @@ -104,7 +104,6 @@ export class Server {
`;

/**
* @param {import('vite').UserConfig} vite_config
* @param {{
* cwd: string;
* config: import('types').ValidatedConfig
Expand All @@ -115,7 +114,7 @@ export class Server {
* }} options
* @param {{ vite_manifest: import('vite').Manifest, assets: import('rollup').OutputAsset[] }} client
*/
export async function build_server(vite_config, options, client) {
export async function build_server(options, client) {
const { cwd, config, manifest_data, build_dir, output_dir, service_worker_entry_file } = options;

let hooks_file = resolve_entry(config.kit.files.hooks);
Expand Down Expand Up @@ -175,27 +174,11 @@ export async function build_server(vite_config, options, client) {
})
);

const default_config = {
build: {
target: 'node14.8'
},
ssr: {
// when developing against the Kit src code, we want to ensure that
// our dependencies are bundled so that apps don't need to install
// them as peerDependencies
noExternal: process.env.BUNDLED
? []
: Object.keys(
JSON.parse(fs.readFileSync(new URL('../../../package.json', import.meta.url), 'utf-8'))
.devDependencies
)
}
};
const vite_config = await get_vite_config();

const merged_config = merge_vite_configs(
default_config,
vite_config,
get_default_config({ config, input, ssr: true, outDir: `${output_dir}/server` })
get_default_config({ config, input, ssr: true, outDir: `${output_dir}/server` }),
vite_config
);

remove_svelte_kit(merged_config);
Expand Down
7 changes: 4 additions & 3 deletions packages/kit/src/vite/build/build_service_worker.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import fs from 'fs';
import * as vite from 'vite';
import { s } from '../../utils/misc.js';
import { merge_vite_configs } from '../utils.js';
import { get_vite_config, merge_vite_configs } from '../utils.js';
import { normalize_path } from '../../utils/url.js';
import { assets_base, remove_svelte_kit } from './utils.js';

/**
* @param {import('vite').UserConfig} vite_config
* @param {{
* config: import('types').ValidatedConfig;
* manifest_data: import('types').ManifestData;
Expand All @@ -17,7 +16,6 @@ import { assets_base, remove_svelte_kit } from './utils.js';
* @param {import('vite').Manifest} client_manifest
*/
export async function build_service_worker(
vite_config,
{ config, manifest_data, output_dir, service_worker_entry_file },
prerendered,
client_manifest
Expand Down Expand Up @@ -67,6 +65,7 @@ export async function build_service_worker(
.trim()
);

const vite_config = await get_vite_config();
const merged_config = merge_vite_configs(vite_config, {
base: assets_base(config.kit),
build: {
Expand All @@ -83,6 +82,8 @@ export async function build_service_worker(
outDir: `${output_dir}/client`,
emptyOutDir: false
},
// @ts-expect-error
configFile: false,
resolve: {
alias: {
'$service-worker': service_worker,
Expand Down
16 changes: 15 additions & 1 deletion packages/kit/src/vite/build/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import * as vite from 'vite';
import { get_aliases } from '../utils.js';

Expand Down Expand Up @@ -77,13 +78,26 @@ export const get_default_config = function ({ config, input, ssr, outDir }) {
},
preserveEntrySignatures: 'strict'
},
ssr
ssr,
target: ssr ? 'node14.8' : undefined
},
// 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.kit)
},
// @ts-expect-error
ssr: {
// when developing against the Kit src code, we want to ensure that
// our dependencies are bundled so that apps don't need to install
// them as peerDependencies
noExternal: process.env.BUNDLED
? []
: Object.keys(
JSON.parse(fs.readFileSync(new URL('../../../package.json', import.meta.url), 'utf-8'))
.devDependencies
)
}
};
};
Expand Down
77 changes: 42 additions & 35 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ function kit() {
*/
let paths;

function create_client_config() {
/** @type {Record<string, string>} */
const input = {
start: `${get_runtime_path(svelte_config.kit)}/client/start.js`
};

// This step is optional — Vite/Rollup will create the necessary chunks
// for everything regardless — but it means that entry chunks reflect
// their location in the source code, which is helpful for debugging
manifest_data.components.forEach((file) => {
const resolved = path.resolve(cwd, file);
const relative = path.relative(svelte_config.kit.files.routes, resolved);

const name = relative.startsWith('..')
? path.basename(file)
: posixify(path.join('pages', relative));
input[name] = resolved;
});

return get_default_config({
config: svelte_config,
input,
ssr: false,
outDir: `${paths.client_out_dir}/immutable`
});
}

return {
name: 'vite-plugin-svelte-kit',

Expand All @@ -104,33 +131,11 @@ function kit() {

manifest_data = sync.all(svelte_config).manifest_data;

/** @type {Record<string, string>} */
const input = {
start: `${get_runtime_path(svelte_config.kit)}/client/start.js`
};

// This step is optional — Vite/Rollup will create the necessary chunks
// for everything regardless — but it means that entry chunks reflect
// their location in the source code, which is helpful for debugging
manifest_data.components.forEach((file) => {
const resolved = path.resolve(cwd, file);
const relative = path.relative(svelte_config.kit.files.routes, resolved);

const name = relative.startsWith('..')
? path.basename(file)
: posixify(path.join('pages', relative));
input[name] = resolved;
});

const result = get_default_config({
config: svelte_config,
input,
ssr: false,
outDir: `${paths.client_out_dir}/immutable`
});

warn_overridden_config(config, result);
return result;
const new_config = create_client_config();

warn_overridden_config(config, new_config);

return new_config;
}

// dev and preview config can be shared
Expand Down Expand Up @@ -242,7 +247,7 @@ function kit() {

log.info('Building server');

const server = await build_server(vite_config, options, client);
const server = await build_server(options, client);

process.env.SVELTEKIT_SERVER_BUILD_COMPLETED = 'true';

Expand All @@ -255,12 +260,14 @@ function kit() {
server
};

const manifest = `export const manifest = ${generate_manifest({
build_data,
relative_path: '.',
routes: manifest_data.routes
})};\n`;
fs.writeFileSync(`${paths.output_dir}/server/manifest.js`, manifest);
fs.writeFileSync(
`${paths.output_dir}/server/manifest.js`,
`export const manifest = ${generate_manifest({
build_data,
relative_path: '.',
routes: manifest_data.routes
})};\n`
);

const static_files = manifest_data.assets.map((asset) => posixify(asset.file));

Expand Down Expand Up @@ -295,7 +302,7 @@ function kit() {

log.info('Building service worker');

await build_service_worker(vite_config, options, prerendered, client.vite_manifest);
await build_service_worker(options, prerendered, client.vite_manifest);
}

console.log(
Expand Down
14 changes: 14 additions & 0 deletions packages/kit/src/vite/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import fs from 'fs';
import path from 'path';
import { pathToFileURL } from 'url';
import { get_runtime_path } from '../core/utils.js';

/**
* @return {Promise<import('vite').UserConfig>}
*/
export async function get_vite_config() {
for (const file of ['vite.config.js', 'vite.config.mjs', 'vite.config.cjs']) {
if (fs.existsSync(file)) {
const config = await import(pathToFileURL(file).toString());
return config.default || config;
}
}
throw new Error('Could not find vite.config.js');
}

/**
* @param {...import('vite').UserConfig} configs
* @returns {import('vite').UserConfig}
Expand Down

0 comments on commit 41c0ebe

Please sign in to comment.