diff --git a/.changeset/few-cobras-switch.md b/.changeset/few-cobras-switch.md new file mode 100644 index 000000000000..c619a06410cf --- /dev/null +++ b/.changeset/few-cobras-switch.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Allow symlinked directories in the routes folder diff --git a/packages/kit/src/core/sync/create_manifest_data/index.js b/packages/kit/src/core/sync/create_manifest_data/index.js index bd4f3c296a98..76d555072a67 100644 --- a/packages/kit/src/core/sync/create_manifest_data/index.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.js @@ -426,8 +426,8 @@ function count_occurrences(needle, haystack) { * @param {string[]} [files] */ function list_files(dir, path = '', files = []) { - fs.readdirSync(dir, { withFileTypes: true }) - .sort(({ name: a }, { name: b }) => { + fs.readdirSync(dir) + .sort((a, b) => { // sort each directory in (__layout, __error, everything else) order // so that we can trace layouts/errors immediately @@ -444,10 +444,12 @@ function list_files(dir, path = '', files = []) { return a < b ? -1 : 1; }) .forEach((file) => { - const joined = path ? `${path}/${file.name}` : file.name; + const full = `${dir}/${file}`; + const stats = fs.statSync(full); + const joined = path ? `${path}/${file}` : file; - if (file.isDirectory()) { - list_files(`${dir}/${file.name}`, joined, files); + if (stats.isDirectory()) { + list_files(full, joined, files); } else { files.push(joined); } diff --git a/packages/kit/src/core/sync/create_manifest_data/index.spec.js b/packages/kit/src/core/sync/create_manifest_data/index.spec.js index ac197bee9e3a..d7b06bf71fe9 100644 --- a/packages/kit/src/core/sync/create_manifest_data/index.spec.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.spec.js @@ -1,3 +1,4 @@ +import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { test } from 'uvu'; @@ -95,6 +96,43 @@ test('creates routes', () => { ]); }); +const symlink_survived_git = fs + .statSync(path.join(cwd, 'samples/symlinks/routes/foo')) + .isSymbolicLink(); + +const test_symlinks = symlink_survived_git ? test : test.skip; + +test_symlinks('creates symlinked routes', () => { + const { components, routes } = create('samples/symlinks/routes'); + + const index = 'samples/symlinks/routes/index.svelte'; + const symlinked_index = 'samples/symlinks/routes/foo/index.svelte'; + + assert.equal(components, [default_layout, default_error, symlinked_index, index]); + + assert.equal(routes, [ + { + type: 'page', + id: '', + pattern: /^\/$/, + path: '/', + shadow: null, + a: [default_layout, index], + b: [default_error] + }, + + { + type: 'page', + id: 'foo', + pattern: /^\/foo\/?$/, + path: '/foo', + shadow: null, + a: [default_layout, symlinked_index], + b: [default_error] + } + ]); +}); + test('creates routes with layout', () => { const { components, routes } = create('samples/basic-layout'); diff --git a/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/bar/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/bar/index.svelte new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/foo b/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/foo new file mode 120000 index 000000000000..79264abecd10 --- /dev/null +++ b/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/foo @@ -0,0 +1 @@ +../bar \ No newline at end of file diff --git a/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/symlinks/routes/index.svelte new file mode 100644 index 000000000000..e69de29bb2d1