From 255e0d2a0bc3b1f8faaa408a7b54eddd84252b81 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 3 Mar 2022 15:27:19 -0500 Subject: [PATCH] add `svelte-kit sync` (#4182) * move copy_assets, generate_tsconfig, create_app etc into new sync module * move some code around * add sync CLI command * tidy up * add prepare script to templates * add sync CLI command * Update packages/kit/src/core/sync/copy_assets.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- .changeset/five-poems-pretend.md | 6 + .../templates/default/package.template.json | 3 +- .../templates/skeleton/package.template.json | 3 +- packages/kit/src/cli.js | 13 + packages/kit/src/core/build/build_client.js | 11 +- packages/kit/src/core/build/index.js | 10 +- packages/kit/src/core/create_app/index.js | 263 ------------------ packages/kit/src/core/dev/index.js | 8 +- packages/kit/src/core/dev/plugin.js | 7 +- packages/kit/src/core/sync/copy_assets.js | 25 ++ .../{ => sync}/create_manifest_data/index.js | 4 +- .../create_manifest_data/index.spec.js | 2 +- .../test/samples/basic-layout/__layout.svelte | 0 .../samples/basic-layout/foo/__layout.svelte | 0 .../samples/basic-layout/foo/index.svelte | 0 .../test/samples/basic-layout/index.svelte | 0 .../test/samples/basic/about.svelte | 0 .../test/samples/basic/blog/[slug].json.ts | 0 .../test/samples/basic/blog/[slug].svelte | 0 .../test/samples/basic/blog/_default.svelte | 0 .../test/samples/basic/blog/index.json.js | 0 .../test/samples/basic/blog/index.svelte | 0 .../test/samples/basic/index.svelte | 0 .../test/samples/custom-extension/about.jazz | 0 .../custom-extension/blog/[slug].beebop | 0 .../custom-extension/blog/[slug].json.js | 0 .../custom-extension/blog/_default.svelte | 0 .../custom-extension/blog/index.json.js | 0 .../custom-extension/blog/index.svelte | 0 .../test/samples/custom-extension/index.funk | 0 .../test/samples/encoding/#.svelte | 0 .../samples/hidden-by-excludes-config/.a.js | 0 .../.well-known/dnt-policy.txt.js | 0 .../hidden-by-excludes-config/__error.svelte | 0 .../hidden-by-excludes-config/__layout.svelte | 0 .../samples/hidden-by-excludes-config/_a.js | 0 .../samples/hidden-by-excludes-config/a.js | 0 .../samples/hidden-by-excludes-config/a.md | 0 .../hidden-by-excludes-config/a.spec.js | 0 .../hidden-by-excludes-config/subdir/.a.js | 0 .../subdir/.well-known/dnt-policy.txt.js | 0 .../subdir/__error.svelte | 0 .../subdir/__layout.svelte | 0 .../hidden-by-excludes-config/subdir/_a.js | 0 .../hidden-by-excludes-config/subdir/a.js | 0 .../hidden-by-excludes-config/subdir/a.md | 0 .../subdir/a.spec.js | 0 .../samples/hidden-dot/.unknown/foo.txt.js | 0 .../hidden-dot/.well-known/dnt-policy.txt.js | 0 .../test/samples/hidden-underscore/_foo.js | 0 .../samples/hidden-underscore/a/_b/c/d.js | 0 .../test/samples/hidden-underscore/e/f/g/h.js | 0 .../test/samples/hidden-underscore/i/_j.js | 0 .../samples/hidden-underscore/index.svelte | 0 .../test/samples/illegal-dunder/__foo.svelte | 0 .../test/samples/invalid-params/[foo][bar].js | 0 .../invalid-qualifier/[foo([a-z]([0-9]))].js | 0 .../samples/layout-reset/foo/__layout.svelte | 0 .../layout-reset/foo/bar/__error.svelte | 0 .../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 .../test/samples/lockfiles/foo.js | 0 .../test/samples/lockfiles/foo.js_tmp | 0 .../samples/multiple-slugs/[file].[ext].js | 0 .../samples/nested-errors/foo/__layout.svelte | 0 .../nested-errors/foo/bar/__error.svelte | 0 .../nested-errors/foo/bar/baz/__error.svelte | 0 .../nested-errors/foo/bar/baz/__layout.svelte | 0 .../nested-errors/foo/bar/baz/index.svelte | 0 .../rest-prefix-suffix/[...rest].json.js | 0 .../prefix-[...rest].svelte | 0 .../test/samples/rest/a/[...rest].js | 0 .../test/samples/rest/a/[...rest].svelte | 0 .../test/samples/rest/b/[...rest].svelte | 0 .../test/samples/rest/b/[...rest].ts | 0 .../sorting/[...anotherrest]/index.svelte | 0 .../test/samples/sorting/[...rest]/abc.svelte | 0 .../deep/[...deep_rest]/index.svelte | 0 .../[...rest]/deep/[...deep_rest]/xyz.svelte | 0 .../sorting/[...rest]/deep/index.svelte | 0 .../samples/sorting/[...rest]/index.svelte | 0 .../test/samples/sorting/[endpoint].js | 0 .../test/samples/sorting/[wildcard].svelte | 0 .../test/samples/sorting/_layout.svelte | 0 .../test/samples/sorting/about.svelte | 0 .../test/samples/sorting/index.svelte | 0 .../test/samples/sorting/post/[id].svelte | 0 .../test/samples/sorting/post/_default.svelte | 0 .../test/samples/sorting/post/bar.svelte | 0 .../test/samples/sorting/post/f[xx].svelte | 0 .../test/samples/sorting/post/f[yy].svelte | 0 .../test/samples/sorting/post/f[zz].ts | 0 .../test/samples/sorting/post/foo.svelte | 0 .../test/samples/sorting/post/index.svelte | 0 .../test/static/bar/baz.txt | 0 .../create_manifest_data/test/static/foo.txt | 0 packages/kit/src/core/sync/sync.js | 33 +++ packages/kit/src/core/sync/utils.js | 25 ++ packages/kit/src/core/sync/write_manifest.js | 74 +++++ packages/kit/src/core/sync/write_root.js | 87 ++++++ .../{tsconfig.js => sync/write_tsconfig.js} | 9 +- packages/kit/src/core/sync/write_types.js | 72 +++++ packages/kit/src/core/utils.js | 25 +- 105 files changed, 356 insertions(+), 324 deletions(-) create mode 100644 .changeset/five-poems-pretend.md delete mode 100644 packages/kit/src/core/create_app/index.js create mode 100644 packages/kit/src/core/sync/copy_assets.js rename packages/kit/src/core/{ => sync}/create_manifest_data/index.js (99%) rename packages/kit/src/core/{ => sync}/create_manifest_data/index.spec.js (99%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic-layout/__layout.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic-layout/foo/__layout.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic-layout/foo/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic-layout/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic/about.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic/blog/[slug].json.ts (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic/blog/[slug].svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic/blog/_default.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic/blog/index.json.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic/blog/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/basic/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/custom-extension/about.jazz (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/custom-extension/blog/[slug].beebop (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/custom-extension/blog/[slug].json.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/custom-extension/blog/_default.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/custom-extension/blog/index.json.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/custom-extension/blog/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/custom-extension/index.funk (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/encoding/#.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/.a.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/.well-known/dnt-policy.txt.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/__error.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/__layout.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/_a.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/a.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/a.md (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/a.spec.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.a.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.well-known/dnt-policy.txt.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__error.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__layout.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/_a.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.md (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.spec.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-dot/.unknown/foo.txt.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-dot/.well-known/dnt-policy.txt.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-underscore/_foo.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-underscore/a/_b/c/d.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-underscore/e/f/g/h.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-underscore/i/_j.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/hidden-underscore/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/illegal-dunder/__foo.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/invalid-params/[foo][bar].js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/invalid-qualifier/[foo([a-z]([0-9]))].js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/layout-reset/foo/__layout.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/layout-reset/foo/bar/__error.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/layout-reset/foo/bar/__layout.reset.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/layout-reset/foo/bar/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/layout-reset/foo/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/layout-reset/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/lockfiles/foo.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/lockfiles/foo.js_tmp (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/multiple-slugs/[file].[ext].js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/nested-errors/foo/__layout.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/nested-errors/foo/bar/__error.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__error.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__layout.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/nested-errors/foo/bar/baz/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/rest-prefix-suffix/[...rest].json.js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/rest-prefix-suffix/prefix-[...rest].svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/rest/a/[...rest].js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/rest/a/[...rest].svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/rest/b/[...rest].svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/rest/b/[...rest].ts (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/[...anotherrest]/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/[...rest]/abc.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/xyz.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/[...rest]/deep/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/[...rest]/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/[endpoint].js (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/[wildcard].svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/_layout.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/about.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/post/[id].svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/post/_default.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/post/bar.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/post/f[xx].svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/post/f[yy].svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/post/f[zz].ts (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/post/foo.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/samples/sorting/post/index.svelte (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/static/bar/baz.txt (100%) rename packages/kit/src/core/{ => sync}/create_manifest_data/test/static/foo.txt (100%) create mode 100644 packages/kit/src/core/sync/sync.js create mode 100644 packages/kit/src/core/sync/utils.js create mode 100644 packages/kit/src/core/sync/write_manifest.js create mode 100644 packages/kit/src/core/sync/write_root.js rename packages/kit/src/core/{tsconfig.js => sync/write_tsconfig.js} (96%) create mode 100644 packages/kit/src/core/sync/write_types.js diff --git a/.changeset/five-poems-pretend.md b/.changeset/five-poems-pretend.md new file mode 100644 index 000000000000..f858fff7777b --- /dev/null +++ b/.changeset/five-poems-pretend.md @@ -0,0 +1,6 @@ +--- +'create-svelte': patch +'@sveltejs/kit': patch +--- + +Add sync CLI command diff --git a/packages/create-svelte/templates/default/package.template.json b/packages/create-svelte/templates/default/package.template.json index c7cdcf5f7566..a21711a0c781 100644 --- a/packages/create-svelte/templates/default/package.template.json +++ b/packages/create-svelte/templates/default/package.template.json @@ -5,7 +5,8 @@ "dev": "svelte-kit dev", "build": "svelte-kit build", "package": "svelte-kit package", - "preview": "svelte-kit preview" + "preview": "svelte-kit preview", + "prepare": "svelte-kit sync" }, "devDependencies": { "@sveltejs/adapter-auto": "next", diff --git a/packages/create-svelte/templates/skeleton/package.template.json b/packages/create-svelte/templates/skeleton/package.template.json index f163debb2f00..26ce591ed495 100644 --- a/packages/create-svelte/templates/skeleton/package.template.json +++ b/packages/create-svelte/templates/skeleton/package.template.json @@ -5,7 +5,8 @@ "dev": "svelte-kit dev", "build": "svelte-kit build", "package": "svelte-kit package", - "preview": "svelte-kit preview" + "preview": "svelte-kit preview", + "prepare": "svelte-kit sync" }, "devDependencies": { "@sveltejs/adapter-auto": "workspace:*", diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js index a8a7a9f9be28..43328e1077a4 100755 --- a/packages/kit/src/cli.js +++ b/packages/kit/src/cli.js @@ -160,6 +160,19 @@ prog } }); +prog + .command('sync') + .describe('Synchronise generated files') + .action(async () => { + try { + const config = await load_config(); + const sync = await import('./core/sync/sync.js'); + sync.all(config); + } catch (error) { + handle_error(error); + } + }); + prog.parse(process.argv, { unknown: (arg) => `Unknown option: ${arg}` }); /** @param {number} port */ diff --git a/packages/kit/src/core/build/build_client.js b/packages/kit/src/core/build/build_client.js index 2678ce98b33d..735b57b87164 100644 --- a/packages/kit/src/core/build/build_client.js +++ b/packages/kit/src/core/build/build_client.js @@ -3,8 +3,7 @@ import path from 'path'; import { svelte } from '@sveltejs/vite-plugin-svelte'; import { deep_merge } from '../../utils/object.js'; import { print_config_conflicts } from '../config/index.js'; -import { create_app } from '../create_app/index.js'; -import { copy_assets, get_aliases } from '../utils.js'; +import { get_aliases } from '../utils.js'; import { create_build, find_deps } from './utils.js'; import { posixify } from '../../utils/filesystem.js'; @@ -32,14 +31,6 @@ export async function build_client({ process.env.VITE_SVELTEKIT_APP_VERSION_FILE = `${config.kit.appDir}/version.json`; process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = `${config.kit.version.pollInterval}`; - create_app({ - config, - manifest_data, - cwd - }); - - copy_assets(path.join(config.kit.outDir, 'runtime')); - process.env.VITE_SVELTEKIT_AMP = config.kit.amp ? 'true' : ''; const client_out_dir = `${output_dir}/client/${config.kit.appDir}`; diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 5515f727b6fa..e9ba0d3c34eb 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -1,13 +1,12 @@ import fs from 'fs'; import path from 'path'; import { mkdirp, rimraf, posixify } from '../../utils/filesystem.js'; -import create_manifest_data from '../create_manifest_data/index.js'; +import * as sync from '../sync/sync.js'; import { get_runtime_path, resolve_entry } from '../utils.js'; import { generate_manifest } from '../generate_manifest/index.js'; import { build_service_worker } from './build_service_worker.js'; import { build_client } from './build_client.js'; import { build_server } from './build_server.js'; -import { generate_tsconfig } from '../tsconfig.js'; /** * @param {import('types').ValidatedConfig} config @@ -24,7 +23,7 @@ export async function build(config) { rimraf(output_dir); mkdirp(output_dir); - generate_tsconfig(config); + const { manifest_data } = sync.all(config); const options = { cwd, @@ -35,10 +34,7 @@ export async function build(config) { // used relative paths, I _think_ this could get fixed. Issue here: // https://github.com/vitejs/vite/issues/2009 assets_base: `${config.kit.paths.assets || config.kit.paths.base}/${config.kit.appDir}/`, - manifest_data: create_manifest_data({ - config, - cwd - }), + manifest_data, output_dir, client_entry_file: path.relative(cwd, `${get_runtime_path(config)}/client/start.js`), service_worker_entry_file: resolve_entry(config.kit.files.serviceWorker), diff --git a/packages/kit/src/core/create_app/index.js b/packages/kit/src/core/create_app/index.js deleted file mode 100644 index 0ee3ee10c0ef..000000000000 --- a/packages/kit/src/core/create_app/index.js +++ /dev/null @@ -1,263 +0,0 @@ -import fs from 'fs'; -import path from 'path'; -import { s } from '../../utils/misc.js'; -import { mkdirp } from '../../utils/filesystem.js'; - -/** @type {Map} */ -const previous_contents = new Map(); - -/** - * @param {string} file - * @param {string} code - */ -export function write_if_changed(file, code) { - if (code !== previous_contents.get(file)) { - previous_contents.set(file, code); - mkdirp(path.dirname(file)); - fs.writeFileSync(file, code); - } -} - -/** @typedef {import('types').ManifestData} ManifestData */ - -/** - * @param {{ - * config: import('types').ValidatedConfig; - * manifest_data: ManifestData; - * cwd: string; - * }} options - */ -export function create_app({ config, manifest_data, cwd = process.cwd() }) { - const output = path.join(config.kit.outDir, 'generated'); - const base = path.relative(cwd, output); - - write_if_changed(`${output}/manifest.js`, generate_client_manifest(manifest_data, base)); - write_if_changed(`${output}/root.svelte`, generate_app(manifest_data)); - - create_types(config, manifest_data); -} - -/** - * @param {string} str - */ -function trim(str) { - return str.replace(/^\t\t/gm, '').trim(); -} - -/** - * @param {ManifestData} manifest_data - * @param {string} base - */ -function generate_client_manifest(manifest_data, base) { - /** @type {Record} */ - const component_indexes = {}; - - /** @param {string} c */ - const get_path = (c) => path.relative(base, c); - - const components = `[ - ${manifest_data.components - .map((component, i) => { - component_indexes[component] = i; - - return `() => import(${s(get_path(component))})`; - }) - .join(',\n\t\t\t\t')} - ]`.replace(/^\t/gm, ''); - - /** @param {string[]} parts */ - const get_indices = (parts) => - `[${parts.map((part) => (part ? `c[${component_indexes[part]}]` : '')).join(', ')}]`; - - const routes = `[ - ${manifest_data.routes - .map((route) => { - if (route.type === 'page') { - const params = - route.params.length > 0 && - '(m) => ({ ' + - route.params - .map((param, i) => { - return param.startsWith('...') - ? `${param.slice(3)}: d(m[${i + 1}] || '')` - : `${param}: d(m[${i + 1}])`; - }) - .join(', ') + - '})'; - - const tuple = [route.pattern, get_indices(route.a), get_indices(route.b)]; - - // optional items - if (params || route.shadow) tuple.push(params || 'null'); - if (route.shadow) tuple.push('1'); - - return `// ${route.a[route.a.length - 1]}\n\t\t[${tuple.join(', ')}]`; - } - }) - .filter(Boolean) - .join(',\n\n\t\t')} - ]`.replace(/^\t/gm, ''); - - return trim(` - const c = ${components}; - - const d = decodeURIComponent; - - export const routes = ${routes}; - - // we import the root layout/error components eagerly, so that - // connectivity errors after initialisation don't nuke the app - export const fallback = [c[0](), c[1]()]; - `); -} - -/** - * @param {ManifestData} manifest_data - */ -function generate_app(manifest_data) { - // TODO remove default layout altogether - - const max_depth = Math.max( - ...manifest_data.routes.map((route) => - route.type === 'page' ? route.a.filter(Boolean).length : 0 - ), - 1 - ); - - const levels = []; - for (let i = 0; i <= max_depth; i += 1) { - levels.push(i); - } - - let l = max_depth; - - let pyramid = ``; - - while (l--) { - pyramid = ` - {#if components[${l + 1}]} - - ${pyramid.replace(/\n/g, '\n\t\t\t\t\t')} - - {:else} - - {/if} - ` - .replace(/^\t\t\t/gm, '') - .trim(); - } - - return trim(` - - - - ${pyramid.replace(/\n/g, '\n\t\t')} - - {#if mounted} -
- {#if navigated} - {title} - {/if} -
- {/if} - `); -} - -/** - * @param {import('types').ValidatedConfig} config - * @param {ManifestData} manifest_data - */ -function create_types(config, manifest_data) { - /** @type {Map} */ - const shadow_types = new Map(); - - /** @param {string} key */ - function extract_params(key) { - /** @type {string[]} */ - const params = []; - - const pattern = /\[([^\]]+)\]/g; - let match; - - while ((match = pattern.exec(key))) { - params.push(match[1]); - } - - return params; - } - - manifest_data.routes.forEach((route) => { - if (route.type === 'endpoint') { - const key = route.file.slice(0, -path.extname(route.file).length); - shadow_types.set(key, { params: extract_params(key), type: 'endpoint' }); - } else if (route.shadow) { - const key = route.shadow.slice(0, -path.extname(route.shadow).length); - shadow_types.set(key, { params: extract_params(key), type: 'both' }); - } - }); - - manifest_data.components.forEach((component) => { - if (component.startsWith('.')) return; // exclude fallback components - - const ext = /** @type {string} */ (config.extensions.find((ext) => component.endsWith(ext))); - const key = component.slice(0, -ext.length); - - if (!shadow_types.has(key)) { - shadow_types.set(key, { params: extract_params(key), type: 'page' }); - } - }); - - shadow_types.forEach(({ params, type }, key) => { - const arg = `{ ${params.map((param) => `${param}: string`).join('; ')} }`; - - const imports = [ - type !== 'page' && 'RequestHandler as GenericRequestHandler', - type !== 'endpoint' && 'Load as GenericLoad' - ] - .filter(Boolean) - .join(', '); - - const file = `${config.kit.outDir}/types/${key || 'index'}.d.ts`; - const content = [ - '// this file is auto-generated', - `import type { ${imports} } from '@sveltejs/kit';`, - type !== 'page' && `export type RequestHandler = GenericRequestHandler<${arg}>;`, - type !== 'endpoint' && - `export type Load> = GenericLoad<${arg}, Props>;` - ] - .filter(Boolean) - .join('\n'); - - write_if_changed(file, content); - }); -} diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index 59f2c244275b..6d16d30b72b6 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -3,9 +3,9 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'; import vite from 'vite'; import { deep_merge } from '../../utils/object.js'; import { print_config_conflicts } from '../config/index.js'; -import { copy_assets, get_aliases, get_runtime_path } from '../utils.js'; +import { get_aliases, get_runtime_path } from '../utils.js'; import { create_plugin } from './plugin.js'; -import { generate_tsconfig } from '../tsconfig.js'; +import * as sync from '../sync/sync.js'; /** * @typedef {{ @@ -20,9 +20,7 @@ import { generate_tsconfig } from '../tsconfig.js'; /** @param {Options} opts */ export async function dev({ cwd, port, host, https, config }) { - copy_assets(path.join(config.kit.outDir, 'runtime')); - - generate_tsconfig(config); + sync.init(config); const [vite_config] = deep_merge( { diff --git a/packages/kit/src/core/dev/plugin.js b/packages/kit/src/core/dev/plugin.js index f53877ea397d..c0fb8321aefb 100644 --- a/packages/kit/src/core/dev/plugin.js +++ b/packages/kit/src/core/dev/plugin.js @@ -4,8 +4,7 @@ import { URL } from 'url'; import colors from 'kleur'; import sirv from 'sirv'; import { installFetch } from '../../install-fetch.js'; -import { create_app } from '../create_app/index.js'; -import create_manifest_data from '../create_manifest_data/index.js'; +import * as sync from '../sync/sync.js'; import { getRequest, setResponse } from '../../node.js'; import { SVELTE_KIT_ASSETS } from '../constants.js'; import { get_mime_lookup, get_runtime_path, resolve_entry } from '../utils.js'; @@ -45,9 +44,7 @@ export async function create_plugin(config, cwd) { let manifest; function update_manifest() { - const manifest_data = create_manifest_data({ config, cwd }); - - create_app({ config, manifest_data, cwd }); + const { manifest_data } = sync.update(config); manifest = { appDir: config.kit.appDir, diff --git a/packages/kit/src/core/sync/copy_assets.js b/packages/kit/src/core/sync/copy_assets.js new file mode 100644 index 000000000000..10401aa25b54 --- /dev/null +++ b/packages/kit/src/core/sync/copy_assets.js @@ -0,0 +1,25 @@ +import fs from 'fs'; +import path from 'path'; +import { copy } from '../../utils/filesystem.js'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +/** @param {string} dest */ +export function copy_assets(dest) { + if (!process.env.BUNDLED) return; + let prefix = '..'; + do { + // we jump through these hoops so that this function + // works whether or not it's been bundled + const resolved = path.resolve(__dirname, `${prefix}/assets`); + + if (fs.existsSync(resolved)) { + copy(resolved, dest); + return; + } + + prefix = `../${prefix}`; + } while (true); // eslint-disable-line +} diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/sync/create_manifest_data/index.js similarity index 99% rename from packages/kit/src/core/create_manifest_data/index.js rename to packages/kit/src/core/sync/create_manifest_data/index.js index 3c83f4d39559..51e6cf3ffdd6 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.js @@ -1,8 +1,8 @@ import fs from 'fs'; import path from 'path'; import mime from 'mime'; -import { get_runtime_path } from '../utils.js'; -import { posixify } from '../../utils/filesystem.js'; +import { get_runtime_path } from '../../utils.js'; +import { posixify } from '../../../utils/filesystem.js'; /** * A portion of a file or directory name where the name has been split into diff --git a/packages/kit/src/core/create_manifest_data/index.spec.js b/packages/kit/src/core/sync/create_manifest_data/index.spec.js similarity index 99% rename from packages/kit/src/core/create_manifest_data/index.spec.js rename to packages/kit/src/core/sync/create_manifest_data/index.spec.js index be4200430a0c..9fef2bf45347 100644 --- a/packages/kit/src/core/create_manifest_data/index.spec.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.spec.js @@ -3,7 +3,7 @@ import { fileURLToPath } from 'url'; import { test } from 'uvu'; import * as assert from 'uvu/assert'; import create_manifest_data from './index.js'; -import options from '../config/options.js'; +import options from '../../config/options.js'; const cwd = fileURLToPath(new URL('./test', import.meta.url)); diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic-layout/__layout.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic-layout/__layout.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic-layout/__layout.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic-layout/__layout.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic-layout/foo/__layout.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic-layout/foo/__layout.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic-layout/foo/__layout.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic-layout/foo/__layout.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic-layout/foo/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic-layout/foo/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic-layout/foo/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic-layout/foo/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic-layout/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic-layout/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic-layout/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic-layout/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic/about.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic/about.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic/about.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic/about.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic/blog/[slug].json.ts b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/[slug].json.ts similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic/blog/[slug].json.ts rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/[slug].json.ts diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic/blog/[slug].svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/[slug].svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic/blog/[slug].svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/[slug].svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic/blog/_default.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/_default.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic/blog/_default.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/_default.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic/blog/index.json.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/index.json.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic/blog/index.json.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/index.json.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic/blog/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic/blog/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic/blog/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/basic/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/basic/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/basic/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/basic/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/custom-extension/about.jazz b/packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/about.jazz similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/custom-extension/about.jazz rename to packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/about.jazz diff --git a/packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/[slug].beebop b/packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/[slug].beebop similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/[slug].beebop rename to packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/[slug].beebop diff --git a/packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/[slug].json.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/[slug].json.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/[slug].json.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/[slug].json.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/_default.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/_default.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/_default.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/_default.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/index.json.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/index.json.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/index.json.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/index.json.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/custom-extension/blog/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/blog/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/custom-extension/index.funk b/packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/index.funk similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/custom-extension/index.funk rename to packages/kit/src/core/sync/create_manifest_data/test/samples/custom-extension/index.funk diff --git a/packages/kit/src/core/create_manifest_data/test/samples/encoding/#.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/encoding/#.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/encoding/#.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/encoding/#.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/.a.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/.a.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/.a.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/.a.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/.well-known/dnt-policy.txt.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/.well-known/dnt-policy.txt.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/.well-known/dnt-policy.txt.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/.well-known/dnt-policy.txt.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/__error.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/__error.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/__error.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/__error.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/__layout.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/__layout.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/__layout.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/__layout.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/_a.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/_a.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/_a.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/_a.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/a.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/a.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/a.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/a.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/a.md b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/a.md similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/a.md rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/a.md diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/a.spec.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/a.spec.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/a.spec.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/a.spec.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.a.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.a.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.a.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.a.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.well-known/dnt-policy.txt.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.well-known/dnt-policy.txt.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.well-known/dnt-policy.txt.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/.well-known/dnt-policy.txt.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__error.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__error.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__error.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__error.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__layout.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__layout.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__layout.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/__layout.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/_a.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/_a.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/_a.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/_a.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.md b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.md similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.md rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.md diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.spec.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.spec.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.spec.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-by-excludes-config/subdir/a.spec.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-dot/.unknown/foo.txt.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-dot/.unknown/foo.txt.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-dot/.unknown/foo.txt.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-dot/.unknown/foo.txt.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-dot/.well-known/dnt-policy.txt.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-dot/.well-known/dnt-policy.txt.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-dot/.well-known/dnt-policy.txt.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-dot/.well-known/dnt-policy.txt.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/_foo.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/_foo.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/_foo.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/_foo.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/a/_b/c/d.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/a/_b/c/d.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/a/_b/c/d.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/a/_b/c/d.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/e/f/g/h.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/e/f/g/h.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/e/f/g/h.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/e/f/g/h.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/i/_j.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/i/_j.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/i/_j.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/i/_j.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/hidden-underscore/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/hidden-underscore/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/illegal-dunder/__foo.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/illegal-dunder/__foo.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/illegal-dunder/__foo.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/illegal-dunder/__foo.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/invalid-params/[foo][bar].js b/packages/kit/src/core/sync/create_manifest_data/test/samples/invalid-params/[foo][bar].js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/invalid-params/[foo][bar].js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/invalid-params/[foo][bar].js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/invalid-qualifier/[foo([a-z]([0-9]))].js b/packages/kit/src/core/sync/create_manifest_data/test/samples/invalid-qualifier/[foo([a-z]([0-9]))].js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/invalid-qualifier/[foo([a-z]([0-9]))].js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/invalid-qualifier/[foo([a-z]([0-9]))].js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/__layout.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/__layout.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/__layout.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/__layout.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/__error.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/bar/__error.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/__error.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/bar/__error.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/__layout.reset.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/bar/__layout.reset.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/__layout.reset.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/bar/__layout.reset.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/bar/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/bar/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/bar/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/layout-reset/foo/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/foo/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/layout-reset/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/layout-reset/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/layout-reset/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/lockfiles/foo.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/lockfiles/foo.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/lockfiles/foo.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/lockfiles/foo.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/lockfiles/foo.js_tmp b/packages/kit/src/core/sync/create_manifest_data/test/samples/lockfiles/foo.js_tmp similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/lockfiles/foo.js_tmp rename to packages/kit/src/core/sync/create_manifest_data/test/samples/lockfiles/foo.js_tmp diff --git a/packages/kit/src/core/create_manifest_data/test/samples/multiple-slugs/[file].[ext].js b/packages/kit/src/core/sync/create_manifest_data/test/samples/multiple-slugs/[file].[ext].js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/multiple-slugs/[file].[ext].js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/multiple-slugs/[file].[ext].js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/__layout.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/__layout.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/__layout.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/__layout.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/bar/__error.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/bar/__error.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/bar/__error.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/bar/__error.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__error.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__error.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__error.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__error.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__layout.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__layout.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__layout.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/bar/baz/__layout.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/bar/baz/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/bar/baz/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/nested-errors/foo/bar/baz/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/nested-errors/foo/bar/baz/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/rest-prefix-suffix/[...rest].json.js b/packages/kit/src/core/sync/create_manifest_data/test/samples/rest-prefix-suffix/[...rest].json.js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/rest-prefix-suffix/[...rest].json.js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/rest-prefix-suffix/[...rest].json.js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/rest-prefix-suffix/prefix-[...rest].svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/rest-prefix-suffix/prefix-[...rest].svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/rest-prefix-suffix/prefix-[...rest].svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/rest-prefix-suffix/prefix-[...rest].svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/rest/a/[...rest].js b/packages/kit/src/core/sync/create_manifest_data/test/samples/rest/a/[...rest].js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/rest/a/[...rest].js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/rest/a/[...rest].js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/rest/a/[...rest].svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/rest/a/[...rest].svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/rest/a/[...rest].svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/rest/a/[...rest].svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/rest/b/[...rest].svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/rest/b/[...rest].svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/rest/b/[...rest].svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/rest/b/[...rest].svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/rest/b/[...rest].ts b/packages/kit/src/core/sync/create_manifest_data/test/samples/rest/b/[...rest].ts similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/rest/b/[...rest].ts rename to packages/kit/src/core/sync/create_manifest_data/test/samples/rest/b/[...rest].ts diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/[...anotherrest]/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...anotherrest]/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/[...anotherrest]/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...anotherrest]/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/abc.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/abc.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/abc.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/abc.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/xyz.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/xyz.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/xyz.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/deep/[...deep_rest]/xyz.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/deep/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/deep/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/deep/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/deep/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/[...rest]/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[...rest]/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/[endpoint].js b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[endpoint].js similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/[endpoint].js rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[endpoint].js diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/[wildcard].svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[wildcard].svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/[wildcard].svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/[wildcard].svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/_layout.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/_layout.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/_layout.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/_layout.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/about.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/about.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/about.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/about.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/post/[id].svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/[id].svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/post/[id].svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/[id].svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/post/_default.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/_default.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/post/_default.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/_default.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/post/bar.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/bar.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/post/bar.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/bar.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/post/f[xx].svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/f[xx].svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/post/f[xx].svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/f[xx].svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/post/f[yy].svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/f[yy].svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/post/f[yy].svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/f[yy].svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/post/f[zz].ts b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/f[zz].ts similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/post/f[zz].ts rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/f[zz].ts diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/post/foo.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/foo.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/post/foo.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/foo.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/samples/sorting/post/index.svelte b/packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/index.svelte similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/samples/sorting/post/index.svelte rename to packages/kit/src/core/sync/create_manifest_data/test/samples/sorting/post/index.svelte diff --git a/packages/kit/src/core/create_manifest_data/test/static/bar/baz.txt b/packages/kit/src/core/sync/create_manifest_data/test/static/bar/baz.txt similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/static/bar/baz.txt rename to packages/kit/src/core/sync/create_manifest_data/test/static/bar/baz.txt diff --git a/packages/kit/src/core/create_manifest_data/test/static/foo.txt b/packages/kit/src/core/sync/create_manifest_data/test/static/foo.txt similarity index 100% rename from packages/kit/src/core/create_manifest_data/test/static/foo.txt rename to packages/kit/src/core/sync/create_manifest_data/test/static/foo.txt diff --git a/packages/kit/src/core/sync/sync.js b/packages/kit/src/core/sync/sync.js new file mode 100644 index 000000000000..0703944d2fc6 --- /dev/null +++ b/packages/kit/src/core/sync/sync.js @@ -0,0 +1,33 @@ +import path from 'path'; +import create_manifest_data from './create_manifest_data/index.js'; +import { copy_assets } from './copy_assets.js'; +import { write_manifest } from './write_manifest.js'; +import { write_root } from './write_root.js'; +import { write_tsconfig } from './write_tsconfig.js'; +import { write_types } from './write_types.js'; + +/** @param {import('types').ValidatedConfig} config */ +export function init(config) { + copy_assets(path.join(config.kit.outDir, 'runtime')); + write_tsconfig(config); +} + +/** @param {import('types').ValidatedConfig} config */ +export function update(config) { + const manifest_data = create_manifest_data({ config }); + + const output = path.join(config.kit.outDir, 'generated'); + const base = path.relative('.', output); + + write_manifest(manifest_data, base, output); + write_root(manifest_data, output); + write_types(config, manifest_data); + + return { manifest_data }; +} + +/** @param {import('types').ValidatedConfig} config */ +export function all(config) { + init(config); + return update(config); +} diff --git a/packages/kit/src/core/sync/utils.js b/packages/kit/src/core/sync/utils.js new file mode 100644 index 000000000000..aaf27a3ef74c --- /dev/null +++ b/packages/kit/src/core/sync/utils.js @@ -0,0 +1,25 @@ +import fs from 'fs'; +import path from 'path'; +import { mkdirp } from '../../utils/filesystem.js'; + +/** @type {Map} */ +const previous_contents = new Map(); + +/** + * @param {string} file + * @param {string} code + */ +export function write_if_changed(file, code) { + if (code !== previous_contents.get(file)) { + previous_contents.set(file, code); + mkdirp(path.dirname(file)); + fs.writeFileSync(file, code); + } +} + +/** @param {string} str */ +export function trim(str) { + const indentation = /** @type {RegExpExecArray} */ (/\n?(\s*)/.exec(str))[1]; + const pattern = new RegExp(`^${indentation}`, 'gm'); + return str.replace(pattern, '').trim(); +} diff --git a/packages/kit/src/core/sync/write_manifest.js b/packages/kit/src/core/sync/write_manifest.js new file mode 100644 index 000000000000..66527c3b43de --- /dev/null +++ b/packages/kit/src/core/sync/write_manifest.js @@ -0,0 +1,74 @@ +import path from 'path'; +import { s } from '../../utils/misc.js'; +import { trim, write_if_changed } from './utils.js'; + +/** + * @param {import('types').ManifestData} manifest_data + * @param {string} base + * @param {string} output + */ +export function write_manifest(manifest_data, base, output) { + /** @type {Record} */ + const component_indexes = {}; + + /** @param {string} c */ + const get_path = (c) => path.relative(base, c); + + const components = `[ + ${manifest_data.components + .map((component, i) => { + component_indexes[component] = i; + + return `() => import(${s(get_path(component))})`; + }) + .join(',\n\t\t\t\t\t')} + ]`.replace(/^\t/gm, ''); + + /** @param {string[]} parts */ + const get_indices = (parts) => + `[${parts.map((part) => (part ? `c[${component_indexes[part]}]` : '')).join(', ')}]`; + + const routes = `[ + ${manifest_data.routes + .map((route) => { + if (route.type === 'page') { + const params = + route.params.length > 0 && + '(m) => ({ ' + + route.params + .map((param, i) => { + return param.startsWith('...') + ? `${param.slice(3)}: d(m[${i + 1}] || '')` + : `${param}: d(m[${i + 1}])`; + }) + .join(', ') + + '})'; + + const tuple = [route.pattern, get_indices(route.a), get_indices(route.b)]; + + // optional items + if (params || route.shadow) tuple.push(params || 'null'); + if (route.shadow) tuple.push('1'); + + return `// ${route.a[route.a.length - 1]}\n\t\t[${tuple.join(', ')}]`; + } + }) + .filter(Boolean) + .join(',\n\n\t\t')} + ]`.replace(/^\t/gm, ''); + + write_if_changed( + `${output}/manifest.js`, + trim(` + const c = ${components}; + + const d = decodeURIComponent; + + export const routes = ${routes}; + + // we import the root layout/error components eagerly, so that + // connectivity errors after initialisation don't nuke the app + export const fallback = [c[0](), c[1]()]; + `) + ); +} diff --git a/packages/kit/src/core/sync/write_root.js b/packages/kit/src/core/sync/write_root.js new file mode 100644 index 000000000000..9aa745236ffb --- /dev/null +++ b/packages/kit/src/core/sync/write_root.js @@ -0,0 +1,87 @@ +import { trim, write_if_changed } from './utils.js'; + +/** + * @param {import('types').ManifestData} manifest_data + * @param {string} output + */ +export function write_root(manifest_data, output) { + // TODO remove default layout altogether + + const max_depth = Math.max( + ...manifest_data.routes.map((route) => + route.type === 'page' ? route.a.filter(Boolean).length : 0 + ), + 1 + ); + + const levels = []; + for (let i = 0; i <= max_depth; i += 1) { + levels.push(i); + } + + let l = max_depth; + + let pyramid = ``; + + while (l--) { + pyramid = ` + {#if components[${l + 1}]} + + ${pyramid.replace(/\n/g, '\n\t\t\t\t\t')} + + {:else} + + {/if} + ` + .replace(/^\t\t\t/gm, '') + .trim(); + } + + write_if_changed( + `${output}/root.svelte`, + trim(` + + + + ${pyramid.replace(/\n/g, '\n\t\t\t')} + + {#if mounted} +
+ {#if navigated} + {title} + {/if} +
+ {/if} + `) + ); +} diff --git a/packages/kit/src/core/tsconfig.js b/packages/kit/src/core/sync/write_tsconfig.js similarity index 96% rename from packages/kit/src/core/tsconfig.js rename to packages/kit/src/core/sync/write_tsconfig.js index 5ecd6ee9f265..8d3880131543 100644 --- a/packages/kit/src/core/tsconfig.js +++ b/packages/kit/src/core/sync/write_tsconfig.js @@ -1,20 +1,19 @@ import fs from 'fs'; import path from 'path'; import colors from 'kleur'; -import { mkdirp, posixify } from '../utils/filesystem.js'; +import { posixify } from '../../utils/filesystem.js'; +import { write_if_changed } from './utils.js'; /** @param {string} file */ const exists = (file) => fs.existsSync(file) && file; /** @param {import('types').ValidatedConfig} config */ -export function generate_tsconfig(config) { +export function write_tsconfig(config) { const out = path.join(config.kit.outDir, 'tsconfig.json'); const user_file = exists('tsconfig.json') || exists('jsconfig.json'); if (user_file) validate(config, out, user_file); - mkdirp(config.kit.outDir); - /** @param {string} file */ const project_relative = (file) => posixify(path.relative('.', file)); @@ -34,7 +33,7 @@ export function generate_tsconfig(config) { include.push(config_relative(`${dir}/**/*.svelte`)); }); - fs.writeFileSync( + write_if_changed( out, JSON.stringify( { diff --git a/packages/kit/src/core/sync/write_types.js b/packages/kit/src/core/sync/write_types.js new file mode 100644 index 000000000000..2da467d4fb87 --- /dev/null +++ b/packages/kit/src/core/sync/write_types.js @@ -0,0 +1,72 @@ +import path from 'path'; +import { write_if_changed } from './utils.js'; + +/** + * @param {import('types').ValidatedConfig} config + * @param {import('types').ManifestData} manifest_data + */ +export function write_types(config, manifest_data) { + /** @type {Map} */ + const shadow_types = new Map(); + + /** @param {string} key */ + function extract_params(key) { + /** @type {string[]} */ + const params = []; + + const pattern = /\[([^\]]+)\]/g; + let match; + + while ((match = pattern.exec(key))) { + params.push(match[1]); + } + + return params; + } + + manifest_data.routes.forEach((route) => { + if (route.type === 'endpoint') { + const key = route.file.slice(0, -path.extname(route.file).length); + shadow_types.set(key, { params: extract_params(key), type: 'endpoint' }); + } else if (route.shadow) { + const key = route.shadow.slice(0, -path.extname(route.shadow).length); + shadow_types.set(key, { params: extract_params(key), type: 'both' }); + } + }); + + manifest_data.components.forEach((component) => { + if (component.startsWith('.')) return; // exclude fallback components + + const ext = /** @type {string} */ (config.extensions.find((ext) => component.endsWith(ext))); + const key = component.slice(0, -ext.length); + + if (!shadow_types.has(key)) { + shadow_types.set(key, { params: extract_params(key), type: 'page' }); + } + }); + + shadow_types.forEach(({ params, type }, key) => { + const arg = + params.length > 0 ? `{ ${params.map((param) => `${param}: string`).join('; ')} }` : '{}'; + + const imports = [ + type !== 'page' && 'RequestHandler as GenericRequestHandler', + type !== 'endpoint' && 'Load as GenericLoad' + ] + .filter(Boolean) + .join(', '); + + const file = `${config.kit.outDir}/types/${key || 'index'}.d.ts`; + const content = [ + '// this file is auto-generated', + `import type { ${imports} } from '@sveltejs/kit';`, + type !== 'page' && `export type RequestHandler = GenericRequestHandler<${arg}>;`, + type !== 'endpoint' && + `export type Load> = GenericLoad<${arg}, Props>;` + ] + .filter(Boolean) + .join('\n'); + + write_if_changed(file, content); + }); +} diff --git a/packages/kit/src/core/utils.js b/packages/kit/src/core/utils.js index 380f6bfaaeb0..934afd0618b1 100644 --- a/packages/kit/src/core/utils.js +++ b/packages/kit/src/core/utils.js @@ -1,12 +1,8 @@ import fs from 'fs'; import path from 'path'; import colors from 'kleur'; -import { copy } from '../utils/filesystem.js'; import { fileURLToPath } from 'url'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - export const get_runtime_path = process.env.BUNDLED ? /** @param {import('types').ValidatedConfig} config */ (config) => posixify_path(path.join(config.kit.outDir, 'runtime')) @@ -18,25 +14,6 @@ function posixify_path(str) { return `/${parsed.dir.slice(parsed.root.length).split(path.sep).join('/')}/${parsed.base}`; } -/** @param {string} dest */ -export function copy_assets(dest) { - if (process.env.BUNDLED) { - let prefix = '..'; - do { - // we jump through these hoops so that this function - // works whether or not it's been bundled - const resolved = path.resolve(__dirname, `${prefix}/assets`); - - if (fs.existsSync(resolved)) { - copy(resolved, dest); - return; - } - - prefix = `../${prefix}`; - } while (true); // eslint-disable-line - } -} - function noop() {} /** @param {{ verbose: boolean }} opts */ @@ -86,7 +63,7 @@ export function resolve_entry(entry) { return null; } -/** @param {import('./create_app/index.js').ManifestData} manifest_data */ +/** @param {import('types').ManifestData} manifest_data */ export function get_mime_lookup(manifest_data) { /** @type {Record} */ const mime = {};