Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Vite 4 #7543

Merged
merged 23 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
reinstate base path handling
  • Loading branch information
Rich-Harris committed Dec 9, 2022
commit b922ff372bb269b810b73ba4956fa3525d24bf6f
5 changes: 5 additions & 0 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SVELTE_KIT_ASSETS } from '../../../constants.js';
import * as sync from '../../../core/sync/sync.js';
import { get_mime_lookup, runtime_base, runtime_prefix } from '../../../core/utils.js';
import { compact } from '../../../utils/array.js';
import { not_found } from '../utils.js';

const cwd = process.cwd();

Expand Down Expand Up @@ -318,6 +319,10 @@ export async function dev(vite, vite_config, svelte_config) {
return;
}

if (!decoded.startsWith(svelte_config.kit.paths.base)) {
return not_found(req, res, svelte_config.kit.paths.base);
}

if (decoded === svelte_config.kit.paths.base + '/service-worker.js') {
const resolved = resolve_entry(svelte_config.kit.files.serviceWorker);

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function kit() {
/** @type {import('vite').UserConfig} */
const result = {
appType: 'custom',
base: svelte_config.kit.paths.base,
base: './',
build: {
rollupOptions: {
// Vite dependency crawler needs an explicit JS entry point
Expand Down
7 changes: 2 additions & 5 deletions packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getRequest, setResponse } from '../../../exports/node/index.js';
import { installPolyfills } from '../../../exports/node/polyfills.js';
import { SVELTE_KIT_ASSETS } from '../../../constants.js';
import { loadEnv, normalizePath } from 'vite';
import { not_found } from '../utils.js';

/** @typedef {import('http').IncomingMessage} Req */
/** @typedef {import('http').ServerResponse} Res */
Expand Down Expand Up @@ -73,11 +74,7 @@ export async function preview(vite, vite_config, svelte_config) {
next();
} else {
res.statusCode = 404;
res.end(
`The server is configured with a public base URL of ${base} - did you mean to visit <a href="${
base + pathname
}">${base + pathname}</a> instead?`
);
not_found(req, res, base);
}
});

Expand Down
29 changes: 29 additions & 0 deletions packages/kit/src/exports/vite/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import { loadConfigFromFile, loadEnv, mergeConfig } from 'vite';
import { runtime_directory } from '../../core/utils.js';
import { posixify } from '../../utils/filesystem.js';
import { negotiate } from '../../utils/http.js';

/**
* @param {import('vite').ResolvedConfig} config
Expand Down Expand Up @@ -149,3 +150,31 @@ export function get_env(env_config, mode) {
private: Object.fromEntries(entries.filter(([k]) => !k.startsWith(env_config.publicPrefix)))
};
}

/**
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
* @param {string} base
*/
export function not_found(req, res, base) {
const type = negotiate(req.headers.accept ?? '*', ['text/plain', 'text/html']);

// special case — handle `/` request automatically
if (req.url === '/' && type === 'text/html') {
res.statusCode = 307;
res.setHeader('location', base);
res.end();
return;
}

res.statusCode = 404;

const prefixed = base + req.url;

if (type === 'text/html') {
res.setHeader('Content-Type', 'text/html');
res.end(`Not found (did you mean <a href="${prefixed}">${prefixed}</a>?)`);
} else {
res.end(`Not found (did you mean ${prefixed}?)`);
}
}