Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/large-seahorses-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: don't try adapting if build failed
154 changes: 89 additions & 65 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ function kit() {
*/
let paths;

/** @type {Error | undefined} */
let build_error;

/** @type {Error | undefined} */
let write_bundle_error;
Comment on lines +112 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @type {Error | undefined} */
let build_error;
/** @type {Error | undefined} */
let write_bundle_error;
let errored = false;


function vite_client_config() {
/** @type {Record<string, string>} */
const input = {
Expand Down Expand Up @@ -247,6 +253,9 @@ function kit() {
* Clears the output directories.
*/
buildStart() {
// Reset errors in watch mode
build_error = write_bundle_error = undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
build_error = write_bundle_error = undefined;
errored = false;


if (is_build) {
rimraf(paths.build_dir);
mkdirp(paths.build_dir);
Expand All @@ -256,6 +265,10 @@ function kit() {
}
},

buildEnd(err) {
build_error = err;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
build_error = err;
errored = !!err;

},

/**
* Vite builds a single bundle. We need three bundles: client, server, and service worker.
* The user's package.json scripts will invoke the Vite CLI to execute the client build. We
Expand All @@ -266,80 +279,86 @@ function kit() {
verbose: vite_config.logLevel === 'info'
});

fs.writeFileSync(
`${paths.client_out_dir}/version.json`,
JSON.stringify({ version: process.env.VITE_SVELTEKIT_APP_VERSION })
);
try {
fs.writeFileSync(
`${paths.client_out_dir}/version.json`,
JSON.stringify({ version: process.env.VITE_SVELTEKIT_APP_VERSION })
);

const { assets, chunks } = collect_output(bundle);
log.info(`Client build completed. Wrote ${chunks.length} chunks and ${assets.length} assets`);

log.info('Building server');
const options = {
cwd,
config: svelte_config,
vite_config_env,
build_dir: paths.build_dir, // TODO just pass `paths`
manifest_data,
output_dir: paths.output_dir,
service_worker_entry_file: resolve_entry(svelte_config.kit.files.serviceWorker)
};
const client = client_build_info(assets, chunks);
const server = await build_server(options, client);

/** @type {import('types').BuildData} */
build_data = {
app_dir: svelte_config.kit.appDir,
manifest_data,
service_worker: options.service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
client,
server
};
const { assets, chunks } = collect_output(bundle);
log.info(
`Client build completed. Wrote ${chunks.length} chunks and ${assets.length} assets`
);

fs.writeFileSync(
`${paths.output_dir}/server/manifest.js`,
`export const manifest = ${generate_manifest({
build_data,
relative_path: '.',
routes: manifest_data.routes
})};\n`
);
log.info('Building server');
const options = {
cwd,
config: svelte_config,
vite_config_env,
build_dir: paths.build_dir, // TODO just pass `paths`
manifest_data,
output_dir: paths.output_dir,
service_worker_entry_file: resolve_entry(svelte_config.kit.files.serviceWorker)
};
const client = client_build_info(assets, chunks);
const server = await build_server(options, client);

/** @type {import('types').BuildData} */
build_data = {
app_dir: svelte_config.kit.appDir,
manifest_data,
service_worker: options.service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
client,
server
};

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

process.env.SVELTEKIT_SERVER_BUILD_COMPLETED = 'true';
log.info('Prerendering');
process.env.SVELTEKIT_SERVER_BUILD_COMPLETED = 'true';
log.info('Prerendering');

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

const files = new Set([
...static_files,
...chunks.map((chunk) => `${svelte_config.kit.appDir}/${chunk.fileName}`),
...assets.map((chunk) => `${svelte_config.kit.appDir}/${chunk.fileName}`)
]);
const files = new Set([
...static_files,
...chunks.map((chunk) => `${svelte_config.kit.appDir}/${chunk.fileName}`),
...assets.map((chunk) => `${svelte_config.kit.appDir}/${chunk.fileName}`)
]);

// TODO is this right?
static_files.forEach((file) => {
if (file.endsWith('/index.html')) {
files.add(file.slice(0, -11));
}
});
// TODO is this right?
static_files.forEach((file) => {
if (file.endsWith('/index.html')) {
files.add(file.slice(0, -11));
}
});

prerendered = await prerender({
config: svelte_config.kit,
entries: manifest_data.routes
.map((route) => (route.type === 'page' ? route.path : ''))
.filter(Boolean),
files,
log
});

if (options.service_worker_entry_file) {
if (svelte_config.kit.paths.assets) {
throw new Error('Cannot use service worker alongside config.kit.paths.assets');
}

prerendered = await prerender({
config: svelte_config.kit,
entries: manifest_data.routes
.map((route) => (route.type === 'page' ? route.path : ''))
.filter(Boolean),
files,
log
});
log.info('Building service worker');

if (options.service_worker_entry_file) {
if (svelte_config.kit.paths.assets) {
throw new Error('Cannot use service worker alongside config.kit.paths.assets');
await build_service_worker(options, prerendered, client.vite_manifest);
}

log.info('Building service worker');

await build_service_worker(options, prerendered, client.vite_manifest);
} catch (error) {
throw (write_bundle_error = /** @type {Error} */ (error));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw (write_bundle_error = /** @type {Error} */ (error));
errored = true;
throw (error);

}

console.log(
Expand All @@ -354,6 +373,11 @@ function kit() {
if (!is_build) {
return; // vite calls closeBundle when dev-server restarts, ignore that
}

if (build_error || write_bundle_error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (build_error || write_bundle_error) {
if (errored) {

return; // Do not try adapting if build failed
}

if (svelte_config.kit.adapter) {
const { adapt } = await import('../core/adapt/index.js');
await adapt(svelte_config, build_data, prerendered, { log });
Expand Down