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

Notes to self #851

Closed
wants to merge 5 commits into from
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
17 changes: 12 additions & 5 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ async function build_client({
manifest.components.forEach((file) => {
const resolved = path.resolve(cwd, file);
const relative = path.relative(config.kit.files.routes, resolved);
input[path.join('pages', relative)] = resolved;

if (relative[0] === '.') {
// default layout/error component
input[path.basename(file)] = resolved;
} else {
input[path.join('pages', relative)] = resolved;
}
});

/** @type {any} */
Expand Down Expand Up @@ -297,19 +303,19 @@ async function build_server(

const manifest = {
assets: ${s(manifest.assets)},
layout: ${stringify_component(manifest.layout)},
error: ${stringify_component(manifest.error)},
routes: [
${manifest.routes
.map((route) => {
if (route.type === 'page') {
const params = get_params(route.params);
const parts = route.parts.map(id => `{ id: ${s(id)}, load: components[${component_indexes.get(id)}] }`);
const good = route.good.map(id => `{ id: ${s(id)}, load: components[${component_indexes.get(id)}] }`);
const bad = route.bad.map(id => `{ id: ${s(id)}, load: components[${component_indexes.get(id)}] }`);

const js_deps = new Set(common_js_deps);
const css_deps = new Set(common_css_deps);

for (const file of route.parts) {
for (const file of [...route.good, ...route.bad]) {
js_deps_by_file.get(file).forEach(asset => {
js_deps.add(asset);
});
Expand All @@ -323,7 +329,8 @@ async function build_server(
type: 'page',
pattern: ${route.pattern},
params: ${params},
parts: [${parts.join(', ')}],
good: [${good.join(', ')}],
bad: [${bad.join(', ')}],
css: [${Array.from(css_deps).map(s).join(', ')}],
js: [${Array.from(js_deps).map(s).join(', ')}]
}`;
Expand Down
28 changes: 10 additions & 18 deletions packages/kit/src/core/create_app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,27 @@ function generate_client_manifest(manifest_data, base) {

const tuple = [
route.pattern,
`[${route.parts.map((part) => `components[${component_indexes[part]}]`).join(', ')}]`,
`[${route.good.map((part) => `${component_indexes[part]}`).join(', ')}]`,
`[${route.bad.map((part) => `${component_indexes[part]}`).join(', ')}]`,
params
]
.filter(Boolean)
.join(', ');

return `// ${route.parts[route.parts.length - 1]}\n\t[${tuple}]`;
return `// ${route.good[route.good.length - 1]}\n\t\t[${tuple}]`;
} else {
return `// ${route.file}\n\t[${route.pattern}]`;
return `// ${route.file}\n\t\t[${route.pattern}]`;
}
})
.join(',\n\n\t')}
.join(',\n\n\t\t')}
]`.replace(/^\t/gm, '');

return trim(`
import * as layout from ${s(get_path(manifest_data.layout))};

const components = ${components};

const d = decodeURIComponent;
const empty = () => ({});

export const routes = ${routes};
export const components = ${components};

export { layout };
export const routes = ${routes};
`);
}

Expand All @@ -116,11 +112,9 @@ function generate_client_manifest(manifest_data, base) {
* @param {string} base
*/
function generate_app(manifest_data, base) {
// TODO remove default layout altogether

const max_depth = Math.max(
...manifest_data.routes.map((route) =>
route.type === 'page' ? route.parts.filter(Boolean).length : 0
route.type === 'page' ? route.good.filter(Boolean).length : 0
)
);

Expand Down Expand Up @@ -164,8 +158,6 @@ function generate_app(manifest_data, base) {
export let components;
${levels.map((l) => `export let props_${l} = null;`).join('\n\t\t\t')}

const Layout = components[0];

setContext('__svelte__', stores);

$: stores.page.set(page);
Expand All @@ -188,13 +180,13 @@ function generate_app(manifest_data, base) {
});
</script>

<Layout {...(props_0 || {})}>
<svelte:component this={components[0]} {...(props_0 || {})}>
{#if error}
<ErrorComponent {status} {error}/>
{:else}
${pyramid.replace(/\n/g, '\n\t\t\t\t')}
{/if}
</Layout>
</svelte:component>

{#if mounted}
<div id="svelte-announcer" aria-live="assertive" aria-atomic="true">
Expand Down
49 changes: 35 additions & 14 deletions packages/kit/src/core/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ export default function create_manifest_data({ config, output, cwd = process.cwd
* @param {string} dir
* @param {Part[][]} parent_segments
* @param {string[]} parent_params
* @param {string[]} stack
* @param {string[]} layout_stack
* @param {string[]} error_stack
*/
function walk(dir, parent_segments, parent_params, stack) {
function walk(dir, parent_segments, parent_params, layout_stack, error_stack) {
/** @type {Item[]} */
const items = fs
.readdirSync(dir)
Expand Down Expand Up @@ -138,31 +139,49 @@ 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 component = find_layout('$layout', item.file);
const layout_component = find_layout('$layout', item.file);
const error_component = find_layout('$error', item.file);

if (component) components.push(component);
if (layout_component) components.push(layout_component);
if (error_component) components.push(error_component);

walk(
path.join(dir, item.basename),
segments,
params,
component ? stack.concat(component) : stack
layout_stack.concat(layout_component),
error_stack.concat(error_component)
);
} else if (item.is_page) {
components.push(item.file);

const parts =
item.is_index && stack[stack.length - 1] === null
? stack.slice(0, -1).concat(item.file)
: stack.concat(item.file);
const good = [...layout_stack, item.file];
const bad = [...error_stack];

const pattern = get_pattern(segments, true);
let i = bad.length;
let trim = true;

while (i--) {
if (bad[i]) {
trim = false;
} else {
if (!good[i]) {
good.splice(i, 1);
bad.splice(i, 1);
}

if (trim) {
bad.length = i;
}
}
}

routes.push({
type: 'page',
pattern,
pattern: get_pattern(segments, true),
params,
parts
good,
bad
});
} else {
const pattern = get_pattern(segments, !item.route_suffix);
Expand All @@ -182,13 +201,15 @@ export default function create_manifest_data({ config, output, cwd = process.cwd
const layout = find_layout('$layout', base) || default_layout;
const error = find_layout('$error', base) || default_error;

walk(config.kit.files.routes, [], [], []);
components.push(layout);
components.push(error);

walk(config.kit.files.routes, [], [], [layout], [error]);

const assets_dir = config.kit.files.assets;

return {
assets: fs.existsSync(assets_dir) ? list_files(assets_dir, '') : [],
layout,
error,
components,
routes
Expand Down
Loading