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

feat: add warning for possible route filename typos #10558

Merged
merged 7 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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/brave-paws-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: add warning for mistyped route filenames
27 changes: 25 additions & 2 deletions packages/kit/src/core/sync/create_manifest_data/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import colors from 'kleur';
import mime from 'mime';
import { list_files, runtime_directory } from '../../utils.js';
import { posixify } from '../../../utils/filesystem.js';
Expand Down Expand Up @@ -201,8 +202,30 @@ function create_routes_and_nodes(cwd, config, fallback) {
// process files first
for (const file of files) {
if (file.is_dir) continue;
if (!file.name.startsWith('+')) continue;
if (!valid_extensions.find((ext) => file.name.endsWith(ext))) continue;

const ext = valid_extensions.find((ext) => file.name.endsWith(ext));
if (!ext) continue;

if (!file.name.startsWith('+')) {
const name = file.name.slice(0, -ext.length);
const typo =
/^(?:(page(?:@(.*))?)|(layout(?:@(.*))?)|(error))$/.test(name) ||
/^(?:(server)|(page(?:(@[a-zA-Z0-9_-]*))?(\.server)?)|(layout(?:(@[a-zA-Z0-9_-]*))?(\.server)?))$/.test(
name
);
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved
if (typo) {
console.log(
colors
.bold()
.yellow(
`Missing route file prefix. Did you mean +${file.name}?` +
` at ${path.join(dir, file.name)}`
)
);
}

continue;
}

if (file.name.endsWith('.d.ts')) {
let name = file.name.slice(0, -5);
Expand Down
14 changes: 14 additions & 0 deletions packages/kit/src/core/sync/write_client_manifest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'node:path';
import { relative_path, resolve_entry } from '../../utils/filesystem.js';
import { s } from '../../utils/misc.js';
import { dedent, write_if_changed } from './utils.js';
import colors from 'kleur';

/**
* Writes the client manifest to disk. The manifest is used to power the router. It contains the
Expand Down Expand Up @@ -108,6 +110,18 @@ export function write_client_manifest(kit, manifest_data, output, metadata) {

const hooks_file = resolve_entry(kit.files.hooks.client);

const typo = resolve_entry('src/+hooks.client');
if (typo) {
console.log(
colors
.bold()
.yellow(
`Unexpected + prefix. Did you mean ${typo.split('/').at(-1)?.slice(1)}?` +
` at ${path.resolve(typo)}`
)
);
}

write_if_changed(
`${output}/app.js`,
dedent`
Expand Down
19 changes: 15 additions & 4 deletions packages/kit/src/core/sync/write_server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'node:fs';
import path from 'node:path';
import { hash } from '../../runtime/hash.js';
import { posixify, resolve_entry } from '../../utils/filesystem.js';
import { s } from '../../utils/misc.js';
import { load_error_page, load_template } from '../config/index.js';
import { runtime_directory } from '../utils.js';
import { write_if_changed } from './utils.js';
import colors from 'kleur';

/**
* @param {{
Expand Down Expand Up @@ -76,8 +76,19 @@ export { set_assets, set_building, set_private_env, set_public_env };
* @param {string} output
*/
export function write_server(config, output) {
// TODO the casting shouldn't be necessary — investigate
const hooks_file = /** @type {string} */ (resolve_entry(config.kit.files.hooks.server));
const hooks_file = resolve_entry(config.kit.files.hooks.server);

const typo = resolve_entry('src/+hooks.server');
if (typo) {
console.log(
colors
.bold()
.yellow(
`Unexpected + prefix. Did you mean ${typo.split('/').at(-1)?.slice(1)}?` +
` at ${path.resolve(typo)}`
)
);
}

/** @param {string} file */
function relative(file) {
Expand All @@ -88,7 +99,7 @@ export function write_server(config, output) {
`${output}/server/internal.js`,
server_template({
config,
hooks: fs.existsSync(hooks_file) ? relative(hooks_file) : null,
hooks: hooks_file ? relative(hooks_file) : null,
has_service_worker:
config.kit.serviceWorker.register && !!resolve_entry(config.kit.files.serviceWorker),
runtime_directory: relative(runtime_directory),
Expand Down
Loading