From 6d4482fb064d421faedc468dfc649563339d87d6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 16 Apr 2021 20:19:02 -0400 Subject: [PATCH] layout resets - closes #626 --- .changeset/modern-donkeys-invite.md | 5 +++ packages/kit/src/core/build/index.js | 3 -- .../src/core/create_manifest_data/index.js | 10 ++++- .../core/create_manifest_data/index.spec.js | 38 +++++++++++++++++++ .../samples/layout-reset/foo/$layout.svelte | 0 .../layout-reset/foo/bar/$error.svelte | 0 .../layout-reset/foo/bar/$layout.reset.svelte | 0 .../samples/layout-reset/foo/bar/index.svelte | 0 .../samples/layout-reset/foo/index.svelte | 0 .../test/samples/layout-reset/index.svelte | 0 10 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 .changeset/modern-donkeys-invite.md create mode 100644 packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/$layout.svelte create mode 100644 packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/$error.svelte create mode 100644 packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/$layout.reset.svelte create mode 100644 packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/index.svelte create mode 100644 packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/index.svelte create mode 100644 packages/kit/src/core/create_manifest_data/test/samples/layout-reset/index.svelte diff --git a/.changeset/modern-donkeys-invite.md b/.changeset/modern-donkeys-invite.md new file mode 100644 index 000000000000..d8472c8fffe9 --- /dev/null +++ b/.changeset/modern-donkeys-invite.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Implement layout resets diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 3b2a6da5630b..ceb5431e33f9 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -340,9 +340,6 @@ async function build_server( const metadata_lookup = ${s(metadata_lookup)}; async function load_component(file) { - if (!module_lookup[file]) { - console.log({ file }); - } return { module: await module_lookup[file](), ...metadata_lookup[file] diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index 89d431039ec3..9c0b7189fb70 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -140,9 +140,15 @@ export default function create_manifest_data({ config, output, cwd = process.cwd params.push(...item.parts.filter((p) => p.dynamic).map((p) => p.content)); if (item.is_dir) { + const layout_reset = find_layout('$layout.reset', item.file); const layout = find_layout('$layout', item.file); const error = find_layout('$error', item.file); + if (layout_reset && layout) { + throw new Error(`Cannot have $layout next to $layout.reset: ${layout_reset}`); + } + + if (layout_reset) components.push(layout_reset); if (layout) components.push(layout); if (error) components.push(error); @@ -150,8 +156,8 @@ export default function create_manifest_data({ config, output, cwd = process.cwd path.join(dir, item.basename), segments, params, - layout_stack.concat(layout), - error_stack.concat(error) + layout_reset ? [layout_reset] : layout_stack.concat(layout), + layout_reset ? [error] : error_stack.concat(error) ); } else if (item.is_page) { components.push(item.file); diff --git a/packages/kit/src/core/create_manifest_data/index.spec.js b/packages/kit/src/core/create_manifest_data/index.spec.js index 70f2e9e3fd5f..0309ed10856d 100644 --- a/packages/kit/src/core/create_manifest_data/index.spec.js +++ b/packages/kit/src/core/create_manifest_data/index.spec.js @@ -357,4 +357,42 @@ test('includes nested error components', () => { ]); }); +test('resets layout', () => { + const { routes } = create('samples/layout-reset'); + + assert.equal(routes, [ + { + type: 'page', + pattern: /^\/$/, + params: [], + path: '/', + a: [layout, 'samples/layout-reset/index.svelte'], + b: [error] + }, + { + type: 'page', + pattern: /^\/foo\/?$/, + params: [], + path: '/foo', + a: [ + layout, + 'samples/layout-reset/foo/$layout.svelte', + 'samples/layout-reset/foo/index.svelte' + ], + b: [error] + }, + { + type: 'page', + pattern: /^\/foo\/bar\/?$/, + params: [], + path: '/foo/bar', + a: [ + 'samples/layout-reset/foo/bar/$layout.reset.svelte', + 'samples/layout-reset/foo/bar/index.svelte' + ], + b: ['samples/layout-reset/foo/bar/$error.svelte'] + } + ]); +}); + test.run(); diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/$layout.svelte b/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/$layout.svelte new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/$error.svelte b/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/$error.svelte new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/$layout.reset.svelte b/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/$layout.reset.svelte new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/index.svelte b/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/index.svelte new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/index.svelte b/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/index.svelte new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/index.svelte b/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/index.svelte new file mode 100644 index 000000000000..e69de29bb2d1