diff --git a/.changeset/four-pillows-give.md b/.changeset/four-pillows-give.md new file mode 100644 index 000000000000..6043eaaa70cb --- /dev/null +++ b/.changeset/four-pillows-give.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Add svelte-kit package command diff --git a/.changeset/odd-ligers-swim.md b/.changeset/odd-ligers-swim.md new file mode 100644 index 000000000000..18f2206a3fda --- /dev/null +++ b/.changeset/odd-ligers-swim.md @@ -0,0 +1,7 @@ +--- +'@sveltejs/adapter-node': patch +'@sveltejs/adapter-vercel': patch +'@sveltejs/kit': patch +--- + +ensure `content-length` limit respected; handle `getRawBody` error(s) diff --git a/.changeset/pre.json b/.changeset/pre.json index e54cbf021ca8..edb22713568c 100644 --- a/.changeset/pre.json +++ b/.changeset/pre.json @@ -89,6 +89,7 @@ "fluffy-dryers-jump", "forty-months-prove", "four-cheetahs-yawn", + "four-pillows-give", "friendly-rivers-argue", "funny-needles-talk", "fuzzy-cougars-compare", @@ -169,6 +170,7 @@ "nine-starfishes-destroy", "nine-tomatoes-poke", "ninety-singers-invite", + "odd-ligers-swim", "old-files-jump", "olive-frogs-drive", "orange-boats-love", @@ -202,6 +204,7 @@ "rare-ways-watch", "real-bikes-matter", "real-cherries-exercise", + "rich-clocks-chew", "rotten-cats-pump", "rotten-singers-taste", "rude-olives-wink", @@ -209,6 +212,7 @@ "selfish-avocados-develop", "selfish-owls-own", "serious-books-push", + "seven-bags-sniff", "seven-impalas-march", "seven-jars-argue", "shaggy-elephants-sell", @@ -225,6 +229,7 @@ "six-geese-study", "six-rules-collect", "sixty-terms-decide", + "slow-buses-beam", "slow-fans-own", "small-pigs-doubt", "smart-bears-leave", @@ -232,6 +237,7 @@ "smart-pears-accept", "smooth-dryers-heal", "smooth-items-exercise", + "smooth-items-march", "smooth-rockets-sneeze", "smooth-shoes-mix", "smooth-shrimps-fly", @@ -284,6 +290,7 @@ "tricky-buses-punch", "tricky-walls-approve", "tricky-wolves-confess", + "twelve-feet-deny", "twelve-goats-knock", "twelve-onions-burn", "two-crabs-tease", @@ -298,6 +305,7 @@ "warm-cameras-fix", "warm-cheetahs-wash", "warm-jars-move", + "weak-parents-guess", "weak-swans-lie", "wet-bees-exist", "wet-dingos-perform", diff --git a/.changeset/rich-clocks-chew.md b/.changeset/rich-clocks-chew.md new file mode 100644 index 000000000000..4de7b7fbce98 --- /dev/null +++ b/.changeset/rich-clocks-chew.md @@ -0,0 +1,8 @@ +--- +'@sveltejs/adapter-begin': patch +'@sveltejs/adapter-node': patch +'@sveltejs/adapter-static': patch +'@sveltejs/kit': patch +--- + +bump `polka` and `sirv` dependency versions diff --git a/.changeset/seven-bags-sniff.md b/.changeset/seven-bags-sniff.md new file mode 100644 index 000000000000..1e1c948b013a --- /dev/null +++ b/.changeset/seven-bags-sniff.md @@ -0,0 +1,5 @@ +--- +'create-svelte': patch +--- + +Add svelte-check to TS templates diff --git a/.changeset/slow-buses-beam.md b/.changeset/slow-buses-beam.md new file mode 100644 index 000000000000..c202a946c6f1 --- /dev/null +++ b/.changeset/slow-buses-beam.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Upgrade to Vite 2.3.4 diff --git a/.changeset/smooth-items-march.md b/.changeset/smooth-items-march.md new file mode 100644 index 000000000000..1453d83ed8fa --- /dev/null +++ b/.changeset/smooth-items-march.md @@ -0,0 +1,11 @@ +--- +'@sveltejs/adapter-begin': patch +'@sveltejs/adapter-cloudflare-workers': patch +'@sveltejs/adapter-netlify': patch +'@sveltejs/adapter-node': patch +'@sveltejs/adapter-static': patch +'@sveltejs/adapter-vercel': patch +'@sveltejs/kit': patch +--- + +Pass validated svelte config to adapter adapt function diff --git a/.changeset/twelve-feet-deny.md b/.changeset/twelve-feet-deny.md new file mode 100644 index 000000000000..25e40588a3c6 --- /dev/null +++ b/.changeset/twelve-feet-deny.md @@ -0,0 +1,5 @@ +--- +'create-svelte': patch +--- + +gitignore package directory diff --git a/.changeset/weak-parents-guess.md b/.changeset/weak-parents-guess.md new file mode 100644 index 000000000000..5495cd813a93 --- /dev/null +++ b/.changeset/weak-parents-guess.md @@ -0,0 +1,5 @@ +--- +'create-svelte': patch +--- + +Adjust build-template script to include package.json diff --git a/.github/ISSUE_TEMPLATE/questions-and-help.md b/.github/ISSUE_TEMPLATE/questions-and-help.md index 6b79c5b5f68a..b2c8c6ef3fb6 100644 --- a/.github/ISSUE_TEMPLATE/questions-and-help.md +++ b/.github/ISSUE_TEMPLATE/questions-and-help.md @@ -9,4 +9,4 @@ assignees: '' This issue tracker is intended to collect bug reports and feature requests. -For help with installation, information on how features work, or questions about specific features of SvelteKit, please come and join us in the [Svelte Discord](https://svelte.dev/chat), or ask your question on [Stack Overflow](https://stackoverflow.com/questions/tagged/svelte). Any issues open for help requests will be closed to keep from clogging up the issue tracker. +For help with installation, information on how features work, or questions about specific features of SvelteKit, please come and join us in the [Svelte Discord](https://svelte.dev/chat), or ask your question on [Stack Overflow](https://stackoverflow.com/questions/tagged/sveltekit). Any issues open for help requests will be closed to keep from clogging up the issue tracker. diff --git a/documentation/docs/12-packaging.md b/documentation/docs/12-packaging.md new file mode 100644 index 000000000000..d9e33ecd5528 --- /dev/null +++ b/documentation/docs/12-packaging.md @@ -0,0 +1,44 @@ +--- +title: Packaging +--- + +You can use SvelteKit to build component libraries as well as apps. + +When you're creating an app, the contents of `src/routes` is the public-facing stuff; [`src/lib`](#modules-lib) contains your app's internal library. + +A SvelteKit component library has the exact same structure as a SvelteKit app, except that `src/lib` is the public-facing bit. `src/routes` might be a documentation or demo site that accompanies the library, or it might just be a sandbox you use during development. + +Running `svelte-kit package` will take the contents of `src/lib` and generate a `package` directory (which can be [configured](#configuration-package)) containing the following: + +- All the files in `src/lib`, unless you [configure](#configuration-package) custom `include`/`exclude` options. Svelte components will be preprocessed (but note the [caveats](#packaging-caveats) below) +- A `package.json` that copies the `name`, `version`, `description`, `keywords`, `homepage`, `bugs`, `license`, `author`, `contributors`, `funding`, `repository`, `dependencies`, `private` and `publishConfig` fields from the root of the project, and adds a `"type": "module"` and an `"exports"` field + +The `"exports"` field contains the package's entry points. By default, all files in `src/lib` will be treated as an entry point unless they start with (or live in a directory that starts with) an underscore, but you can [configure](#configuration-package) this behaviour. If you have a `src/lib/index.js` or `src/lib/index.svelte` file, it will be treated as the package root. + +For example, if you had a `src/lib/Foo.svelte` component and a `src/lib/index.js` module that re-exported it, a consumer of your library could do either of the following: + +```js +import { Foo } from 'your-library'; +``` + +```js +import Foo from 'your-library/Foo.svelte'; +``` + +## Publishing + +To publish the generated package: + +``` +npm publish package +``` + +If you configure a custom [`package.dir`](#configuration-package), change `package` accordingly. + +## Caveats + +This is a relatively experimental feature and is not yet fully implemented: + +- if a preprocessor is specified, `.svelte` files are transformed (meaning they can contain TypeScript, for example), but `.d.ts` files are not generated +- `.ts` files are not currently transformed, and will cause the process to fail +- all other files are copied across as-is diff --git a/documentation/docs/12-cli.md b/documentation/docs/13-cli.md similarity index 100% rename from documentation/docs/12-cli.md rename to documentation/docs/13-cli.md diff --git a/documentation/docs/13-configuration.md b/documentation/docs/14-configuration.md similarity index 100% rename from documentation/docs/13-configuration.md rename to documentation/docs/14-configuration.md diff --git a/documentation/docs/99-troubleshooting.md b/documentation/docs/99-troubleshooting.md index 1c0d6fe8ed37..6cbc7b455632 100644 --- a/documentation/docs/99-troubleshooting.md +++ b/documentation/docs/99-troubleshooting.md @@ -8,4 +8,4 @@ Please see the [FAQ](/faq) for solutions to common issues and helpful tips and t ### Support -You can ask for help on [Discord](https://svelte.dev/chat) and [StackOverflow](https://stackoverflow.com/questions/tagged/svelte). Please first search for information related to your issue in the FAQ, Google or another search engine, issue tracker, and Discord chat history in order to be respectful of others' time. There are many more people asking questions than answering them, so this will help in allowing the community to grow in a scalable fashion. +You can ask for help on [Discord](https://svelte.dev/chat) and [StackOverflow](https://stackoverflow.com/questions/tagged/sveltekit). Please first search for information related to your issue in the FAQ, Google or another search engine, issue tracker, and Discord chat history in order to be respectful of others' time. There are many more people asking questions than answering them, so this will help in allowing the community to grow in a scalable fashion. diff --git a/packages/adapter-begin/CHANGELOG.md b/packages/adapter-begin/CHANGELOG.md index 9ac87a9766d4..dd0cf07a6495 100644 --- a/packages/adapter-begin/CHANGELOG.md +++ b/packages/adapter-begin/CHANGELOG.md @@ -1,5 +1,18 @@ # @sveltejs/adapter-begin +## 1.0.0-next.10 + +### Patch Changes + +- bf77940: bump `polka` and `sirv` dependency versions +- 028abd9: Pass validated svelte config to adapter adapt function +- Updated dependencies [6372690] +- Updated dependencies [c3d36a3] +- Updated dependencies [bf77940] +- Updated dependencies [2172469] +- Updated dependencies [028abd9] + - @sveltejs/kit@1.0.0-next.110 + ## 1.0.0-next.9 ### Patch Changes diff --git a/packages/adapter-begin/index.js b/packages/adapter-begin/index.js index e32bd8cacf6b..2233cd33be7d 100644 --- a/packages/adapter-begin/index.js +++ b/packages/adapter-begin/index.js @@ -25,7 +25,7 @@ export default function () { const adapter = { name: '@sveltejs/adapter-begin', - async adapt(utils) { + async adapt({ utils }) { utils.log.minor('Parsing app.arc file'); const { static: static_mount_point } = parse_arc('app.arc'); diff --git a/packages/adapter-begin/package.json b/packages/adapter-begin/package.json index 074d8e3c8044..f673302cc424 100644 --- a/packages/adapter-begin/package.json +++ b/packages/adapter-begin/package.json @@ -1,6 +1,6 @@ { "name": "@sveltejs/adapter-begin", - "version": "1.0.0-next.9", + "version": "1.0.0-next.10", "main": "index.cjs", "types": "index.d.ts", "scripts": { @@ -24,7 +24,7 @@ "devDependencies": { "@sveltejs/kit": "workspace:*", "rollup": "^2.47.0", - "sirv": "^1.0.11", + "sirv": "^1.0.12", "typescript": "^4.2.4" }, "exports": { diff --git a/packages/adapter-cloudflare-workers/CHANGELOG.md b/packages/adapter-cloudflare-workers/CHANGELOG.md index d436f781d093..2e7def7898ed 100644 --- a/packages/adapter-cloudflare-workers/CHANGELOG.md +++ b/packages/adapter-cloudflare-workers/CHANGELOG.md @@ -1,5 +1,17 @@ # @sveltejs/adapter-cloudflare-workers +## 1.0.0-next.10 + +### Patch Changes + +- 028abd9: Pass validated svelte config to adapter adapt function +- Updated dependencies [6372690] +- Updated dependencies [c3d36a3] +- Updated dependencies [bf77940] +- Updated dependencies [2172469] +- Updated dependencies [028abd9] + - @sveltejs/kit@1.0.0-next.110 + ## 1.0.0-next.9 ### Patch Changes diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index 4ed029f6e869..856d162d3f27 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -8,7 +8,7 @@ export default function () { /** @type {import('@sveltejs/kit').Adapter} */ const adapter = { name: '@sveltejs/adapter-cloudflare-workers', - async adapt(utils) { + async adapt({ utils }) { const { site } = validate_config(utils); const bucket = site.bucket; diff --git a/packages/adapter-cloudflare-workers/package.json b/packages/adapter-cloudflare-workers/package.json index 04df3a3be454..d2cdee4995f9 100644 --- a/packages/adapter-cloudflare-workers/package.json +++ b/packages/adapter-cloudflare-workers/package.json @@ -1,6 +1,6 @@ { "name": "@sveltejs/adapter-cloudflare-workers", - "version": "1.0.0-next.9", + "version": "1.0.0-next.10", "type": "module", "exports": { "import": "./index.js" diff --git a/packages/adapter-netlify/CHANGELOG.md b/packages/adapter-netlify/CHANGELOG.md index aef4046d5ec2..49363a61fc4c 100644 --- a/packages/adapter-netlify/CHANGELOG.md +++ b/packages/adapter-netlify/CHANGELOG.md @@ -1,5 +1,17 @@ # @sveltejs/adapter-netlify +## 1.0.0-next.15 + +### Patch Changes + +- 028abd9: Pass validated svelte config to adapter adapt function +- Updated dependencies [6372690] +- Updated dependencies [c3d36a3] +- Updated dependencies [bf77940] +- Updated dependencies [2172469] +- Updated dependencies [028abd9] + - @sveltejs/kit@1.0.0-next.110 + ## 1.0.0-next.14 ### Patch Changes diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index 0a2243b29cb5..c5a9fb48cfa5 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -9,7 +9,7 @@ export default function () { const adapter = { name: '@sveltejs/adapter-netlify', - async adapt(utils) { + async adapt({ utils }) { const { publish, functions } = validate_config().build; utils.rimraf(publish); diff --git a/packages/adapter-netlify/package.json b/packages/adapter-netlify/package.json index 225e7b9072a5..e10521e2094a 100644 --- a/packages/adapter-netlify/package.json +++ b/packages/adapter-netlify/package.json @@ -1,6 +1,6 @@ { "name": "@sveltejs/adapter-netlify", - "version": "1.0.0-next.14", + "version": "1.0.0-next.15", "type": "module", "exports": { "import": "./index.js" diff --git a/packages/adapter-node/CHANGELOG.md b/packages/adapter-node/CHANGELOG.md index 5593e02f0712..fb20d8234f68 100644 --- a/packages/adapter-node/CHANGELOG.md +++ b/packages/adapter-node/CHANGELOG.md @@ -1,5 +1,19 @@ # @sveltejs/adapter-node +## 1.0.0-next.23 + +### Patch Changes + +- c3d36a3: ensure `content-length` limit respected; handle `getRawBody` error(s) +- bf77940: bump `polka` and `sirv` dependency versions +- 028abd9: Pass validated svelte config to adapter adapt function +- Updated dependencies [6372690] +- Updated dependencies [c3d36a3] +- Updated dependencies [bf77940] +- Updated dependencies [2172469] +- Updated dependencies [028abd9] + - @sveltejs/kit@1.0.0-next.110 + ## 1.0.0-next.22 ### Patch Changes diff --git a/packages/adapter-node/index.d.ts b/packages/adapter-node/index.d.ts index 4316b65ad712..fc87605e6137 100644 --- a/packages/adapter-node/index.d.ts +++ b/packages/adapter-node/index.d.ts @@ -1,3 +1,3 @@ -declare function plugin(options: { out?: string }): import('@sveltejs/kit').Adapter; +declare function plugin(options?: { out?: string }): import('@sveltejs/kit').Adapter; export = plugin; diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index 238b29133bc4..6b7192aa6136 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -12,7 +12,7 @@ export default function ({ out = 'build' } = {}) { const adapter = { name: '@sveltejs/adapter-node', - async adapt(utils) { + async adapt({ utils }) { utils.log.minor('Copying assets'); const static_directory = join(out, 'assets'); utils.copy_client_files(static_directory); diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index b140d46ccf85..f6f4f4c6948e 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -1,6 +1,6 @@ { "name": "@sveltejs/adapter-node", - "version": "1.0.0-next.22", + "version": "1.0.0-next.23", "type": "module", "exports": { "import": "./index.js" @@ -26,9 +26,9 @@ "c8": "^7.7.2", "compression": "^1.7.4", "node-fetch": "^3.0.0-beta.9", - "polka": "^1.0.0-next.14", + "polka": "^1.0.0-next.15", "rollup": "^2.47.0", - "sirv": "^1.0.11", + "sirv": "^1.0.12", "typescript": "^4.2.4", "uvu": "^0.5.1" }, diff --git a/packages/adapter-node/src/index.js b/packages/adapter-node/src/index.js index a9872be1c373..57f241238fc5 100644 --- a/packages/adapter-node/src/index.js +++ b/packages/adapter-node/src/index.js @@ -4,12 +4,8 @@ import * as app from './app.js'; const { HOST = '0.0.0.0', PORT = 3000 } = process.env; -const instance = createServer({ render: app.render }).listen(PORT, HOST, (err) => { - if (err) { - console.log('error', err); - } else { - console.log(`Listening on port ${PORT}`); - } +const instance = createServer({ render: app.render }).listen(PORT, HOST, () => { + console.log(`Listening on port ${PORT}`); }); export { instance }; diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index 0f94fa32b3e6..266dd10df1e0 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -40,12 +40,22 @@ export function createServer({ render }) { prerendered_handler, async (req, res) => { const parsed = new URL(req.url || '', 'http://localhost'); + + let body; + + try { + body = await getRawBody(req); + } catch (err) { + res.statusCode = err.status || 400; + return res.end(err.reason || 'Invalid request body'); + } + const rendered = await render({ method: req.method, headers: req.headers, // TODO: what about repeated headers, i.e. string[] path: parsed.pathname, - rawBody: await getRawBody(req), - query: parsed.searchParams + query: parsed.searchParams, + rawBody: body }); if (rendered) { diff --git a/packages/adapter-static/CHANGELOG.md b/packages/adapter-static/CHANGELOG.md index 67e9d3d3bfdc..d5e5b4c705a9 100644 --- a/packages/adapter-static/CHANGELOG.md +++ b/packages/adapter-static/CHANGELOG.md @@ -1,5 +1,18 @@ # @sveltejs/adapter-static +## 1.0.0-next.12 + +### Patch Changes + +- bf77940: bump `polka` and `sirv` dependency versions +- 028abd9: Pass validated svelte config to adapter adapt function +- Updated dependencies [6372690] +- Updated dependencies [c3d36a3] +- Updated dependencies [bf77940] +- Updated dependencies [2172469] +- Updated dependencies [028abd9] + - @sveltejs/kit@1.0.0-next.110 + ## 1.0.0-next.11 ### Patch Changes diff --git a/packages/adapter-static/index.d.ts b/packages/adapter-static/index.d.ts index 00ab5e9a3e02..72296a4e0531 100644 --- a/packages/adapter-static/index.d.ts +++ b/packages/adapter-static/index.d.ts @@ -1,4 +1,4 @@ -declare function plugin(options: { +declare function plugin(options?: { pages?: string; assets?: string; fallback?: string; diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index 6546a923f976..5155fec1b8a6 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -5,12 +5,12 @@ * fallback?: string; * }} [opts] */ -export default function ({ pages = 'build', assets = pages, fallback = null } = {}) { +export default function ({ pages = 'build', assets = pages, fallback } = {}) { /** @type {import('@sveltejs/kit').Adapter} */ const adapter = { name: '@sveltejs/adapter-static', - async adapt(utils) { + async adapt({ utils }) { utils.copy_static_files(assets); utils.copy_client_files(assets); diff --git a/packages/adapter-static/package.json b/packages/adapter-static/package.json index 1fd13e760766..af4f0b25f86f 100644 --- a/packages/adapter-static/package.json +++ b/packages/adapter-static/package.json @@ -1,6 +1,6 @@ { "name": "@sveltejs/adapter-static", - "version": "1.0.0-next.11", + "version": "1.0.0-next.12", "type": "module", "main": "index.js", "exports": { @@ -21,7 +21,7 @@ "@sveltejs/kit": "workspace:*", "playwright-chromium": "^1.10.0", "port-authority": "^1.1.2", - "sirv": "^1.0.11", + "sirv": "^1.0.12", "typescript": "^4.2.4" } } diff --git a/packages/adapter-vercel/CHANGELOG.md b/packages/adapter-vercel/CHANGELOG.md index 016ba9ea4fda..583eae69c2a0 100644 --- a/packages/adapter-vercel/CHANGELOG.md +++ b/packages/adapter-vercel/CHANGELOG.md @@ -1,5 +1,18 @@ # @sveltejs/adapter-vercel +## 1.0.0-next.20 + +### Patch Changes + +- c3d36a3: ensure `content-length` limit respected; handle `getRawBody` error(s) +- 028abd9: Pass validated svelte config to adapter adapt function +- Updated dependencies [6372690] +- Updated dependencies [c3d36a3] +- Updated dependencies [bf77940] +- Updated dependencies [2172469] +- Updated dependencies [028abd9] + - @sveltejs/kit@1.0.0-next.110 + ## 1.0.0-next.19 ### Patch Changes diff --git a/packages/adapter-vercel/files/entry.js b/packages/adapter-vercel/files/entry.js index d0dd9f29d0ae..4fa041c47fee 100644 --- a/packages/adapter-vercel/files/entry.js +++ b/packages/adapter-vercel/files/entry.js @@ -7,12 +7,21 @@ import { render } from '../output/server/app.js'; // eslint-disable-line import/ export default async (req, res) => { const { pathname, searchParams } = new URL(req.url || '', 'http://localhost'); + let body; + + try { + body = await getRawBody(req); + } catch (err) { + res.statusCode = err.status || 400; + return res.end(err.reason || 'Invalid request body'); + } + const rendered = await render({ method: req.method, headers: req.headers, path: pathname, query: searchParams, - rawBody: await getRawBody(req) + rawBody: body }); if (rendered) { diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 10bd4e63b1ad..ffdedfe3ceeb 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -8,7 +8,7 @@ export default function () { const adapter = { name: '@sveltejs/adapter-vercel', - async adapt(utils) { + async adapt({ utils }) { const dir = '.vercel_build_output'; utils.rimraf(dir); diff --git a/packages/adapter-vercel/package.json b/packages/adapter-vercel/package.json index bfd74e0269fc..5bbd6e4b70b4 100644 --- a/packages/adapter-vercel/package.json +++ b/packages/adapter-vercel/package.json @@ -1,6 +1,6 @@ { "name": "@sveltejs/adapter-vercel", - "version": "1.0.0-next.19", + "version": "1.0.0-next.20", "type": "module", "exports": { "import": "./index.js" diff --git a/packages/create-svelte/CHANGELOG.md b/packages/create-svelte/CHANGELOG.md index 674df7991be3..ee7660dbb128 100644 --- a/packages/create-svelte/CHANGELOG.md +++ b/packages/create-svelte/CHANGELOG.md @@ -1,5 +1,13 @@ # create-svelte +## 2.0.0-next.72 + +### Patch Changes + +- 1739443: Add svelte-check to TS templates +- 6372690: gitignore package directory +- f211906: Adjust build-template script to include package.json + ## 2.0.0-next.71 ### Patch Changes diff --git a/packages/create-svelte/bin.js b/packages/create-svelte/bin.js index 0dea68b0d536..ca02e73de111 100755 --- a/packages/create-svelte/bin.js +++ b/packages/create-svelte/bin.js @@ -184,8 +184,8 @@ function write_common_files(cwd, options) { const pkg = /** @type {any} */ (JSON.parse(fs.readFileSync(pkg_file, 'utf-8'))); files.forEach((file) => { - const include = file.include.every((condition) => options[condition]); - const exclude = file.exclude.some((condition) => options[condition]); + const include = file.include.every((condition) => matchesCondition(condition, options)); + const exclude = file.exclude.some((condition) => matchesCondition(condition, options)); if (exclude || !include) return; @@ -205,6 +205,17 @@ function write_common_files(cwd, options) { fs.writeFileSync(pkg_file, JSON.stringify(pkg, null, ' ')); } +/** + * @param {import('./types/internal').Condition} condition + * @param {import('./types/internal').Options} options + * @returns {boolean} + */ +function matchesCondition(condition, options) { + return condition === 'default' || condition === 'skeleton' + ? options.template === condition + : options[condition]; +} + /** * @param {any} target * @param {any} source diff --git a/packages/create-svelte/package.json b/packages/create-svelte/package.json index 77ed12e4a5c4..99637eb4eddf 100644 --- a/packages/create-svelte/package.json +++ b/packages/create-svelte/package.json @@ -1,6 +1,6 @@ { "name": "create-svelte", - "version": "2.0.0-next.71", + "version": "2.0.0-next.72", "bin": "./bin.js", "dependencies": { "kleur": "^4.1.4", diff --git a/packages/create-svelte/scripts/build-templates.js b/packages/create-svelte/scripts/build-templates.js index ae653acfdb14..ab53da5409a1 100644 --- a/packages/create-svelte/scripts/build-templates.js +++ b/packages/create-svelte/scripts/build-templates.js @@ -32,28 +32,29 @@ async function generate_templates(shared) { const ts = []; glob('**/*', { cwd, filesOnly: true, dot: true }).forEach((name) => { + // the package.template.json thing is a bit annoying — basically we want + // to be able to develop and deploy the app from here, but have a different + // package.json in newly created projects (based on package.template.json) + if (name === 'package.template.json') { + let contents = fs.readFileSync(path.join(cwd, name), 'utf8'); + // TODO package-specific versions + contents = contents.replace(/workspace:\*/g, 'next'); + fs.writeFileSync(`${dir}/package.json`, contents); + return; + } + // ignore files that are written conditionally if (shared.has(name)) return; // ignore contents of .gitignore or .ignore if (!gitignore.accepts(name) || !ignore.accepts(name) || name === '.ignore') return; - // the package.template.json thing is a bit annoying — basically we want - // to be able to develop and deploy the app from here, but have a different - // package.json in newly created projects (based on package.template.json) - if (/\.(js|ts|svelte|svelte\.md)$/.test(name) || name === 'package.template.json') { - let contents = fs.readFileSync(path.join(cwd, name), 'utf8'); - - if (name === 'package.template.json') { - // TODO package-specific versions - contents = contents.replace(/workspace:\*/g, 'next'); - fs.writeFileSync(`${dir}/package.json`, contents); - } else { - ts.push({ - name, - contents - }); - } + if (/\.(js|ts|svelte|svelte\.md)$/.test(name)) { + const contents = fs.readFileSync(path.join(cwd, name), 'utf8'); + ts.push({ + name, + contents + }); } else { const dest = path.join(assets, name).replace('.gitignore', 'gitignore'); // npm does wacky stuff to gitignores mkdirp(path.dirname(dest)); diff --git a/packages/create-svelte/shared/+default+typescript/package.json b/packages/create-svelte/shared/+default+typescript/package.json new file mode 100644 index 000000000000..9e7682209403 --- /dev/null +++ b/packages/create-svelte/shared/+default+typescript/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "@types/cookie": "^0.4.0" + } +} diff --git a/packages/create-svelte/shared/+typescript/package.json b/packages/create-svelte/shared/+typescript/package.json index 343a1d723cb6..334b96b584bf 100644 --- a/packages/create-svelte/shared/+typescript/package.json +++ b/packages/create-svelte/shared/+typescript/package.json @@ -1,7 +1,12 @@ { + "scripts": { + "check": "svelte-check --tsconfig ./tsconfig.json", + "check:watch": "svelte-check --tsconfig ./tsconfig.json --watch" + }, "devDependencies": { "typescript": "^4.0.0", "tslib": "^2.0.0", + "svelte-check": "^2.0.0", "svelte-preprocess": "^4.0.0" } } diff --git a/packages/create-svelte/templates/default/.gitignore b/packages/create-svelte/templates/default/.gitignore index 0a4623184983..2d66ec056330 100644 --- a/packages/create-svelte/templates/default/.gitignore +++ b/packages/create-svelte/templates/default/.gitignore @@ -1,5 +1,4 @@ .DS_Store node_modules /.svelte-kit -/build -/functions +/package diff --git a/packages/create-svelte/templates/default/package.json b/packages/create-svelte/templates/default/package.json index e2508f211361..03189ad635bc 100644 --- a/packages/create-svelte/templates/default/package.json +++ b/packages/create-svelte/templates/default/package.json @@ -13,8 +13,7 @@ "@sveltejs/kit": "next", "svelte": "^3.38.2", "svelte-preprocess": "^4.7.3", - "typescript": "^4.2.4", - "vite": "^2.3.1" + "typescript": "^4.2.4" }, "type": "module", "dependencies": { diff --git a/packages/create-svelte/templates/skeleton/.gitignore b/packages/create-svelte/templates/skeleton/.gitignore index 0a4623184983..2d66ec056330 100644 --- a/packages/create-svelte/templates/skeleton/.gitignore +++ b/packages/create-svelte/templates/skeleton/.gitignore @@ -1,5 +1,4 @@ .DS_Store node_modules /.svelte-kit -/build -/functions +/package diff --git a/packages/create-svelte/types/internal.d.ts b/packages/create-svelte/types/internal.d.ts index c5e70871a7ae..81a10b96fd07 100644 --- a/packages/create-svelte/types/internal.d.ts +++ b/packages/create-svelte/types/internal.d.ts @@ -10,11 +10,13 @@ export type File = { contents: string; }; +export type Condition = 'eslint' | 'prettier' | 'typescript' | 'skeleton' | 'default'; + export type Common = { files: Array<{ name: string; - include: Array<'eslint' | 'prettier' | 'typescript'>; - exclude: Array<'eslint' | 'prettier' | 'typescript'>; + include: Array; + exclude: Array; contents: string; }>; }; diff --git a/packages/kit/CHANGELOG.md b/packages/kit/CHANGELOG.md index 6b788bbd91f8..063c34f6e4bc 100644 --- a/packages/kit/CHANGELOG.md +++ b/packages/kit/CHANGELOG.md @@ -1,5 +1,15 @@ # @sveltejs/kit +## 1.0.0-next.110 + +### Patch Changes + +- 6372690: Add svelte-kit package command +- c3d36a3: ensure `content-length` limit respected; handle `getRawBody` error(s) +- bf77940: bump `polka` and `sirv` dependency versions +- 2172469: Upgrade to Vite 2.3.4 +- 028abd9: Pass validated svelte config to adapter adapt function + ## 1.0.0-next.109 ### Patch Changes diff --git a/packages/kit/package.json b/packages/kit/package.json index a2d132977f90..8ca817ed096c 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -1,18 +1,18 @@ { "name": "@sveltejs/kit", - "version": "1.0.0-next.109", + "version": "1.0.0-next.110", "type": "module", "dependencies": { "@sveltejs/vite-plugin-svelte": "^1.0.0-next.10", "cheap-watch": "^1.0.3", "sade": "^1.7.4", - "vite": "^2.3.1" + "vite": "2.3.3" }, "devDependencies": { "@rollup/plugin-replace": "^2.4.2", - "@sveltejs/kit": "workspace:*", "@types/amphtml-validator": "^1.0.1", "@types/cookie": "^0.4.0", + "@types/globrex": "^0.1.0", "@types/marked": "^2.0.2", "@types/mime": "^2.0.3", "@types/node": "^14.14.43", @@ -22,14 +22,16 @@ "cookie": "^0.4.1", "devalue": "^2.0.1", "eslint": "^7.25.0", + "globrex": "^0.1.2", "kleur": "^4.1.4", + "locate-character": "^2.0.5", "marked": "^2.0.3", "node-fetch": "^3.0.0-beta.9", "port-authority": "^1.1.2", "rimraf": "^3.0.2", "rollup": "^2.47.0", "selfsigned": "^1.10.11", - "sirv": "^1.0.11", + "sirv": "^1.0.12", "svelte": "^3.38.2", "svelte-check": "^1.5.2", "tiny-glob": "^0.2.8", diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js index 97b44bac0836..f80e01a461b2 100644 --- a/packages/kit/src/cli.js +++ b/packages/kit/src/cli.js @@ -178,6 +178,22 @@ prog ); }); +prog + .command('package') + .describe('Create a package') + .option('-d, --dir', 'Destination directory', 'package') + .action(async () => { + const config = await get_config(); + + const { make_package } = await import('./core/make_package/index.js'); + + try { + await make_package(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/adapt/index.js b/packages/kit/src/core/adapt/index.js index 8e24387dcb4f..475a9578a9da 100644 --- a/packages/kit/src/core/adapt/index.js +++ b/packages/kit/src/core/adapt/index.js @@ -14,7 +14,7 @@ export async function adapt(config, build_data, { cwd = process.cwd(), verbose } const log = logger({ verbose }); const utils = get_utils({ cwd, config, build_data, log }); - await adapt(utils); + await adapt({ utils, config }); log.success('done'); } diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index 349673bfe358..9885b30d0e7c 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -153,7 +153,14 @@ class Watcher extends EventEmitter { const root = (await this.vite.ssrLoadModule(`/${this.dir}/generated/root.svelte`)) .default; - const rawBody = await getRawBody(req); + let body; + + try { + body = await getRawBody(req); + } catch (err) { + res.statusCode = err.status || 400; + return res.end(err.reason || 'Invalid request body'); + } const host = /** @type {string} */ (this.config.kit.host || req.headers[this.config.kit.hostHeader || 'host']); @@ -165,7 +172,7 @@ class Watcher extends EventEmitter { host, path: parsed.pathname, query: new URLSearchParams(parsed.query), - rawBody + rawBody: body }, { amp: this.config.kit.amp, diff --git a/packages/kit/src/core/load_config/index.spec.js b/packages/kit/src/core/load_config/index.spec.js index 4678a7300126..d1138752f849 100644 --- a/packages/kit/src/core/load_config/index.spec.js +++ b/packages/kit/src/core/load_config/index.spec.js @@ -27,6 +27,17 @@ test('fills in defaults', () => { host: null, hostHeader: null, hydrate: true, + package: { + dir: 'package', + exports: { + include: ['**'], + exclude: ['_*', '**/_*'] + }, + files: { + include: ['**'], + exclude: [] + } + }, paths: { base: '', assets: '/.' @@ -111,6 +122,17 @@ test('fills in partial blanks', () => { host: null, hostHeader: null, hydrate: true, + package: { + dir: 'package', + exports: { + include: ['**'], + exclude: ['_*', '**/_*'] + }, + files: { + include: ['**'], + exclude: [] + } + }, paths: { base: '', assets: '/.' diff --git a/packages/kit/src/core/load_config/options.js b/packages/kit/src/core/load_config/options.js index 8c63e9748885..c763465039e9 100644 --- a/packages/kit/src/core/load_config/options.js +++ b/packages/kit/src/core/load_config/options.js @@ -80,6 +80,27 @@ const options = { hydrate: expect_boolean(true), + package: { + type: 'branch', + children: { + dir: expect_string('package'), + exports: { + type: 'branch', + children: { + include: expect_array_of_strings(['**']), + exclude: expect_array_of_strings(['_*', '**/_*']) + } + }, + files: { + type: 'branch', + children: { + include: expect_array_of_strings(['**']), + exclude: expect_array_of_strings([]) + } + } + } + }, + paths: { type: 'branch', children: { @@ -171,6 +192,23 @@ function expect_string(string, allow_empty = true) { }; } +/** + * @param {string[]} array + * @returns {ConfigDefinition} + */ +function expect_array_of_strings(array) { + return { + type: 'leaf', + default: array, + validate: (option, keypath) => { + if (!Array.isArray(option) || !option.every((glob) => typeof glob === 'string')) { + throw new Error(`${keypath} must be an array of strings`); + } + return option; + } + }; +} + /** * @param {boolean} boolean * @returns {ConfigDefinition} diff --git a/packages/kit/src/core/load_config/test/index.js b/packages/kit/src/core/load_config/test/index.js index c86f90e55307..eb41d36ad697 100644 --- a/packages/kit/src/core/load_config/test/index.js +++ b/packages/kit/src/core/load_config/test/index.js @@ -37,6 +37,17 @@ async function testLoadDefaultConfig(path) { host: null, hostHeader: null, hydrate: true, + package: { + dir: 'package', + exports: { + include: ['**'], + exclude: ['_*', '**/_*'] + }, + files: { + include: ['**'], + exclude: [] + } + }, paths: { base: '', assets: '/.' }, prerender: { crawl: true, enabled: true, force: false, pages: ['*'] }, router: true, diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js new file mode 100644 index 000000000000..c081bb5dd1f9 --- /dev/null +++ b/packages/kit/src/core/make_package/index.js @@ -0,0 +1,150 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { preprocess } from 'svelte/compiler'; +import globrex from 'globrex'; +import { mkdirp, rimraf } from '../filesystem'; + +/** + * @param {import('types/config').ValidatedConfig} config + * @param {string} cwd + */ +export async function make_package(config, cwd = process.cwd()) { + rimraf(path.join(cwd, config.kit.package.dir)); + + const files_filter = create_filter(config.kit.package.files); + const exports_filter = create_filter(config.kit.package.exports); + + const files = walk(config.kit.files.lib); + + const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8')); + + const package_pkg = { + name: pkg.name, + version: pkg.version, + description: pkg.description, + keywords: pkg.keywords, + homepage: pkg.homepage, + bugs: pkg.bugs, + license: pkg.license, + author: pkg.author, + contributors: pkg.contributors, + funding: pkg.funding, + repository: pkg.repository, + dependencies: pkg.dependencies, + private: pkg.private, + publishConfig: pkg.publishConfig, + type: 'module', + /** @type {Record} */ + exports: { + './package.json': './package.json' + } + }; + + for (const file of files) { + if (!files_filter(file)) continue; + + const filename = path.join(config.kit.files.lib, file); + const source = fs.readFileSync(filename, 'utf8'); + + const ext = path.extname(file); + const svelte_ext = config.extensions.find((ext) => file.endsWith(ext)); // unlike `ext`, could be e.g. `.svelte.md` + + /** @type {string} */ + let out_file; + + /** @type {string} */ + let out_contents; + + if (svelte_ext) { + // it's a Svelte component + // TODO how to emit types? + out_file = file.slice(0, -svelte_ext.length) + '.svelte'; + out_contents = config.preprocess + ? (await preprocess(source, config.preprocess, { filename })).code + : source; + } else if (ext === '.ts' && !file.endsWith('.d.ts')) { + // TODO transpile TS file and emit types + // also, we want to emit types from JSDoc annotations in .js files + throw new Error('svelte-kit package does not yet support TypeScript'); + } else { + out_file = file; + out_contents = source; + } + + write(path.join(cwd, config.kit.package.dir, out_file), out_contents); + + if (exports_filter(file)) { + const entry = `./${out_file}`; + package_pkg.exports[entry] = entry; + } + } + + const main = package_pkg.exports['./index.js'] || package_pkg.exports['./index.svelte']; + + if (main) { + package_pkg.exports['.'] = main; + } + + write( + path.join(cwd, config.kit.package.dir, 'package.json'), + JSON.stringify(package_pkg, null, ' ') + ); + + const project_readme = path.join(cwd, 'README.md'); + const package_readme = path.join(cwd, config.kit.package.dir, 'README.md'); + + if (fs.existsSync(project_readme) && !fs.existsSync(package_readme)) { + fs.copyFileSync(project_readme, package_readme); + } +} + +/** + * @param {{ + * include: string[]; + * exclude: string[]; + * }} options + */ +function create_filter(options) { + const include = options.include.map((str) => str && globrex(str)); + const exclude = options.exclude.map((str) => str && globrex(str)); + + /** @param {string} str */ + const filter = (str) => + include.some((glob) => glob.regex.test(str)) && !exclude.some((glob) => glob.regex.test(str)); + + return filter; +} + +/** @param {string} cwd */ +function walk(cwd) { + /** @type {string[]} */ + const all_files = []; + + /** @param {string} dir */ + function walk_dir(dir) { + const files = fs.readdirSync(path.join(cwd, dir)); + + for (const file of files) { + const joined = path.join(dir, file); + const stats = fs.statSync(path.join(cwd, joined)); + + if (stats.isDirectory()) { + walk_dir(joined); + } else { + all_files.push(joined); + } + } + } + + walk_dir(''); + return all_files; +} + +/** + * @param {string} file + * @param {string} contents + */ +function write(file, contents) { + mkdirp(path.dirname(file)); + fs.writeFileSync(file, contents); +} diff --git a/packages/kit/src/core/node/index.js b/packages/kit/src/core/node/index.js index 21e6de345f7a..66f03aa53998 100644 --- a/packages/kit/src/core/node/index.js +++ b/packages/kit/src/core/node/index.js @@ -1,44 +1,45 @@ /** * @param {import('http').IncomingMessage} req - * @returns {Promise} + * @returns {Promise} */ export function getRawBody(req) { return new Promise((fulfil, reject) => { const h = req.headers; if (!h['content-type']) { - fulfil(null); - return; + return fulfil(null); } req.on('error', reject); const length = Number(h['content-length']); - /** @type {Uint8Array} */ - let data; - - if (!isNaN(length)) { - data = new Uint8Array(length); + // https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95 + if (isNaN(length) && h['transfer-encoding'] == null) { + return fulfil(null); + } - let i = 0; + let data = new Uint8Array(length || 0); + if (length > 0) { + let offset = 0; req.on('data', (chunk) => { - data.set(chunk, i); - i += chunk.length; - }); - } else { - // https://github.com/jshttp/type-is/blob/c1f4388c71c8a01f79934e68f630ca4a15fffcd6/index.js#L81-L95 - if (h['transfer-encoding'] === undefined) { - fulfil(null); - return; - } + const new_len = offset + Buffer.byteLength(chunk); - data = new Uint8Array(0); + if (new_len > length) { + return reject({ + status: 413, + reason: 'Exceeded "Content-Length" limit' + }); + } + data.set(chunk, offset); + offset = new_len; + }); + } else { req.on('data', (chunk) => { const new_data = new Uint8Array(data.length + chunk.length); - new_data.set(data); + new_data.set(data, 0); new_data.set(chunk, data.length); data = new_data; }); @@ -48,11 +49,11 @@ export function getRawBody(req) { const [type] = h['content-type'].split(/;\s*/); if (type === 'application/octet-stream') { - fulfil(data); + return fulfil(data); } - const decoder = new TextDecoder(h['content-encoding'] || 'utf-8'); - fulfil(decoder.decode(data)); + const encoding = h['content-encoding'] || 'utf-8'; + fulfil(new TextDecoder(encoding).decode(data)); }); }); } diff --git a/packages/kit/src/core/start/index.js b/packages/kit/src/core/start/index.js index 10b3a134a813..e0682cdd8043 100644 --- a/packages/kit/src/core/start/index.js +++ b/packages/kit/src/core/start/index.js @@ -53,14 +53,23 @@ export async function start({ port, host, config, https: use_https = false, cwd assets_handler(req, res, () => { static_handler(req, res, async () => { + let body; + + try { + body = await getRawBody(req); + } catch (err) { + res.statusCode = err.status || 400; + return res.end(err.reason || 'Invalid request body'); + } + const rendered = await app.render({ host: /** @type {string} */ (config.kit.host || req.headers[config.kit.hostHeader || 'host']), method: req.method, headers: /** @type {import('types/helper').Headers} */ (req.headers), path: decodeURIComponent(parsed.pathname), - rawBody: await getRawBody(req), - query: new URLSearchParams(parsed.query || '') + query: new URLSearchParams(parsed.query || ''), + rawBody: body }); if (rendered) { diff --git a/packages/kit/src/runtime/hash.js b/packages/kit/src/runtime/hash.js index d88fa0a56f50..0b50be42c5f8 100644 --- a/packages/kit/src/runtime/hash.js +++ b/packages/kit/src/runtime/hash.js @@ -1,4 +1,4 @@ -/** @param {string | Uint8Array} value */ +/** @param {import('types/hooks').StrictBody} value */ export function hash(value) { let hash = 5381; let i = value.length; diff --git a/packages/kit/src/runtime/server/endpoint.js b/packages/kit/src/runtime/server/endpoint.js index 4bd9daeac7e1..155e81c182f0 100644 --- a/packages/kit/src/runtime/server/endpoint.js +++ b/packages/kit/src/runtime/server/endpoint.js @@ -10,7 +10,7 @@ function error(body) { } /** - * @param {import('types/endpoint').ServerRequest} request + * @param {import('types/hooks').ServerRequest} request * @param {import('types/internal').SSREndpoint} route * @returns {Promise} */ @@ -51,14 +51,14 @@ export default async function render_route(request, route) { ); } - /** @type {string | Uint8Array} */ + /** @type {import('types/hooks').StrictBody} */ let normalized_body; if (typeof body === 'object' && (!type || type === 'application/json')) { headers = { ...headers, 'content-type': 'application/json' }; normalized_body = JSON.stringify(body); } else { - normalized_body = /** @type {string | Uint8Array} */ (body); + normalized_body = /** @type {import('types/hooks').StrictBody} */ (body); } return { status, body: normalized_body, headers }; diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js index df62567e1a6d..d14dd8b09a90 100644 --- a/packages/kit/src/runtime/server/page/index.js +++ b/packages/kit/src/runtime/server/page/index.js @@ -2,7 +2,7 @@ import { respond } from './respond.js'; import { respond_with_error } from './respond_with_error.js'; /** - * @param {import('types/endpoint').ServerRequest} request + * @param {import('types/hooks').ServerRequest} request * @param {import('types/internal').SSRPage} route * @param {import('types/internal').SSRRenderOptions} options * @param {import('types/internal').SSRRenderState} state diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index 0c728e6df4e5..053905ee30ec 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -7,7 +7,7 @@ const s = JSON.stringify; /** * * @param {{ - * request: import('types/endpoint').ServerRequest; + * request: import('types/hooks').ServerRequest; * options: import('types/internal').SSRRenderOptions; * state: import('types/internal').SSRRenderState; * route: import('types/internal').SSRPage; diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index d4b39b0e62bb..29810c25899f 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -6,7 +6,7 @@ import { respond_with_error } from './respond_with_error.js'; /** * @param {{ - * request: import('types/endpoint').ServerRequest; + * request: import('types/hooks').ServerRequest; * options: import('types/internal').SSRRenderOptions; * state: import('types/internal').SSRRenderState; * $session: any; diff --git a/packages/kit/src/runtime/server/page/respond_with_error.js b/packages/kit/src/runtime/server/page/respond_with_error.js index beb06728a908..2126e6a41b1c 100644 --- a/packages/kit/src/runtime/server/page/respond_with_error.js +++ b/packages/kit/src/runtime/server/page/respond_with_error.js @@ -3,7 +3,7 @@ import { load_node } from './load_node.js'; /** * @param {{ - * request: import('types/endpoint').ServerRequest; + * request: import('types/hooks').ServerRequest; * options: import('types/internal').SSRRenderOptions; * state: import('types/internal').SSRRenderState; * $session: any; diff --git a/packages/kit/test/types.d.ts b/packages/kit/test/types.d.ts index 5214190d8dac..f612487e9d2e 100644 --- a/packages/kit/test/types.d.ts +++ b/packages/kit/test/types.d.ts @@ -1,4 +1,4 @@ -/// +/// import { Page, Response as PlaywrightResponse } from 'playwright-chromium'; import { RequestInfo, RequestInit, Response as NodeFetchResponse } from 'node-fetch'; diff --git a/packages/kit/tsconfig.json b/packages/kit/tsconfig.json index 0d8a30793399..e8904ff2aaea 100644 --- a/packages/kit/tsconfig.json +++ b/packages/kit/tsconfig.json @@ -11,7 +11,7 @@ "paths": { "test": ["./test/types"], "types/*": ["./types/*"], - "@sveltejs/kit": ["../types"] + "@sveltejs/kit": ["./types/index"] } }, "include": ["src/**/*", "test/**/*", "types/**/*", "test/types.d.ts", "test/ambient.d.ts"] diff --git a/packages/kit/types/ambient-modules.d.ts b/packages/kit/types/ambient-modules.d.ts index 9e52c1f250bf..1683677b0408 100644 --- a/packages/kit/types/ambient-modules.d.ts +++ b/packages/kit/types/ambient-modules.d.ts @@ -34,7 +34,10 @@ declare module '$app/navigation' { */ export function invalidate(href: string): Promise; /** - * Programmatically prefetches the given page, which means a) ensuring that the code for the page is loaded, and b) calling the page's load function with the appropriate options. + * Programmatically prefetches the given page, which means + * 1. ensuring that the code for the page is loaded, and + * 2. calling the page's load function with the appropriate options. + * * This is the same behaviour that SvelteKit triggers when the user taps or mouses over an `` element with `sveltekit:prefetch`. * If the next navigation is to `href`, the values returned from load will be used, making navigation instantaneous. * Returns a Promise that resolves when the prefetch is complete. @@ -45,8 +48,11 @@ declare module '$app/navigation' { /** * Programmatically prefetches the code for routes that haven't yet been fetched. * Typically, you might call this to speed up subsequent navigation. - * If no argument is given, all routes will be fetched, otherwise you can specify routes by any matching pathname such as `/about` (to match `src/routes/about.svelte`) - * or `/blog/*` (to match `src/routes/blog/[slug].svelte`). Unlike prefetch, this won't call preload for individual pages. + * + * If no argument is given, all routes will be fetched, otherwise you can specify routes by any matching pathname + * such as `/about` (to match `src/routes/about.svelte`) or `/blog/*` (to match `src/routes/blog/[slug].svelte`). + * + * Unlike prefetch, this won't call preload for individual pages. * Returns a Promise that resolves when the routes have been prefetched. */ export function prefetchRoutes(routes?: string[]): Promise; @@ -65,14 +71,15 @@ declare module '$app/paths' { declare module '$app/stores' { import { Readable, Writable } from 'svelte/store'; - import { Page } from '@sveltejs/kit'; + type Page = import('@sveltejs/kit').Page; + type Navigating = { from: Page; to: Page }; /** * A convenience function around `getContext` that returns `{ navigating, page, session }`. * Most of the time, you won't need to use it. */ export function getStores(): { - navigating: Readable<{ from: Page; to: Page } | null>; + navigating: Readable; page: Readable; session: Writable; }; @@ -85,7 +92,7 @@ declare module '$app/stores' { * When navigating starts, its value is `{ from, to }`, where from and to both mirror the page store value. * When navigating finishes, its value reverts to `null`. */ - export const navigating: Readable<{ from: Page; to: Page } | null>; + export const navigating: Readable; /** * A writable store whose initial value is whatever was returned from `getSession`. * It can be written to, but this will not cause changes to persist on the server — this is something you must implement yourself. diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index cd3d7e54ade4..5471b5dbdc89 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -22,7 +22,7 @@ export type AdapterUtils = { export type Adapter = { name: string; - adapt: (utils: AdapterUtils) => Promise; + adapt: ({ utils, config }: { utils: AdapterUtils; config: ValidatedConfig }) => Promise; }; export type Config = { @@ -44,6 +44,17 @@ export type Config = { host?: string; hostHeader?: string; hydrate?: boolean; + package?: { + dir?: string; + exports?: { + include?: string[]; + exclude?: string[]; + }; + files?: { + include?: string[]; + exclude?: string[]; + }; + }; paths?: { base?: string; assets?: string; @@ -83,6 +94,17 @@ export type ValidatedConfig = { host: string; hostHeader: string; hydrate: boolean; + package: { + dir: string; + exports: { + include: string[]; + exclude: string[]; + }; + files: { + include: string[]; + exclude: string[]; + }; + }; paths: { base: string; assets: string; diff --git a/packages/kit/types/endpoint.d.ts b/packages/kit/types/endpoint.d.ts index 3c6d5586ce43..91ab6296a0f5 100644 --- a/packages/kit/types/endpoint.d.ts +++ b/packages/kit/types/endpoint.d.ts @@ -1,16 +1,5 @@ -import { Headers, ParameterizedBody } from './helper'; - -export type ServerRequest, Body = unknown> = { - method: string; - host: string; - headers: Headers; - path: string; - params: Record; - query: URLSearchParams; - rawBody: string | Uint8Array; - body: ParameterizedBody; - locals: Locals; -}; +import { ServerRequest } from './hooks'; +import { Headers } from './helper'; type JSONValue = | string @@ -24,7 +13,7 @@ type JSONValue = export type EndpointOutput = { status?: number; headers?: Partial; - body?: string | Uint8Array | JSONValue; + body?: JSONValue | Uint8Array; }; export type RequestHandler, Body = unknown> = ( diff --git a/packages/kit/types/helper.d.ts b/packages/kit/types/helper.d.ts index fde3a82f4897..764dc7c9d6f5 100644 --- a/packages/kit/types/helper.d.ts +++ b/packages/kit/types/helper.d.ts @@ -7,7 +7,7 @@ interface ReadOnlyFormData extends Iterator<[string, string]> { values: () => Iterator; } -export type BaseBody = string | Buffer | ReadOnlyFormData; +type BaseBody = string | Buffer | ReadOnlyFormData; export type ParameterizedBody = Body extends FormData ? ReadOnlyFormData : BaseBody & Body; @@ -17,3 +17,10 @@ export type ParameterizedBody = Body extends FormData // 'set-cookie' is a `string[]` (or at least `string | string[]`) // but this can't happen until TypeScript 4.3 export type Headers = Record; + +export type Location = { + host: string; + path: string; + params: Record; + query: URLSearchParams; +}; diff --git a/packages/kit/types/hooks.d.ts b/packages/kit/types/hooks.d.ts index 15a12c47bed9..5e3875492611 100644 --- a/packages/kit/types/hooks.d.ts +++ b/packages/kit/types/hooks.d.ts @@ -1,20 +1,26 @@ -import { BaseBody, Headers } from './helper'; -import { ServerRequest } from './endpoint'; +import { Headers, Location, ParameterizedBody } from './helper'; -export type Incoming = { +export type StrictBody = string | Uint8Array; + +export type Incoming = Omit & { + method: string; + headers: Headers; + rawBody: StrictBody | null; + body?: ParameterizedBody; +}; + +export type ServerRequest, Body = unknown> = Location & { method: string; - host: string; headers: Headers; - path: string; - query: URLSearchParams; - rawBody: string | Uint8Array; - body?: BaseBody; + rawBody: StrictBody | null; + body: ParameterizedBody; + locals: Locals; }; export type ServerResponse = { status: number; headers: Headers; - body?: string | Uint8Array; + body?: StrictBody; }; export type GetSession, Session = any> = { diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 832cbdb40fed..ff6d4d57ca94 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -3,7 +3,13 @@ import './ambient-modules'; -export { Adapter, AdapterUtils, Config } from './config'; +export { Adapter, AdapterUtils, Config, ValidatedConfig } from './config'; +export { EndpointOutput, RequestHandler } from './endpoint'; export { ErrorLoad, Load, Page } from './page'; -export { Incoming, GetSession, Handle, ServerResponse as Response } from './hooks'; -export { ServerRequest as Request, EndpointOutput, RequestHandler } from './endpoint'; +export { + Incoming, + GetSession, + Handle, + ServerRequest as Request, + ServerResponse as Response +} from './hooks'; diff --git a/packages/kit/types/page.d.ts b/packages/kit/types/page.d.ts index 967439ddac1b..5b80353cb7c0 100644 --- a/packages/kit/types/page.d.ts +++ b/packages/kit/types/page.d.ts @@ -1,3 +1,5 @@ +import { Location as Page } from './helper'; + export type LoadInput = { page: Page; fetch: (info: RequestInfo, init?: RequestInit) => Promise; @@ -22,9 +24,4 @@ export type LoadOutput = { /* Publicized Types */ export type Load = (input: LoadInput) => LoadOutput | Promise; export type ErrorLoad = (input: ErrorLoadInput) => LoadOutput | Promise; -export type Page = { - host: string; - path: string; - params: Record; - query: URLSearchParams; -}; +export { Page }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7026b60b70fb..2c3b81a3b828 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -55,14 +55,14 @@ importers: '@architect/parser': ^3.0.1 '@sveltejs/kit': workspace:* rollup: ^2.47.0 - sirv: ^1.0.11 + sirv: ^1.0.12 typescript: ^4.2.4 dependencies: '@architect/parser': 3.0.1 devDependencies: '@sveltejs/kit': link:../kit rollup: 2.47.0 - sirv: 1.0.11 + sirv: 1.0.12 typescript: 4.2.4 packages/adapter-cloudflare-workers: @@ -96,9 +96,9 @@ importers: c8: ^7.7.2 compression: ^1.7.4 node-fetch: ^3.0.0-beta.9 - polka: ^1.0.0-next.14 + polka: ^1.0.0-next.15 rollup: ^2.47.0 - sirv: ^1.0.11 + sirv: ^1.0.12 typescript: ^4.2.4 uvu: ^0.5.1 devDependencies: @@ -107,9 +107,9 @@ importers: c8: 7.7.2 compression: 1.7.4 node-fetch: 3.0.0-beta.9 - polka: 1.0.0-next.14 + polka: 1.0.0-next.15 rollup: 2.47.0 - sirv: 1.0.11 + sirv: 1.0.12 typescript: 4.2.4 uvu: 0.5.1 @@ -118,13 +118,13 @@ importers: '@sveltejs/kit': workspace:* playwright-chromium: ^1.10.0 port-authority: ^1.1.2 - sirv: ^1.0.11 + sirv: ^1.0.12 typescript: ^4.2.4 devDependencies: '@sveltejs/kit': link:../kit playwright-chromium: 1.10.0 port-authority: 1.1.2 - sirv: 1.0.11 + sirv: 1.0.12 typescript: 4.2.4 packages/adapter-vercel: @@ -181,7 +181,6 @@ importers: svelte: ^3.38.2 svelte-preprocess: ^4.7.3 typescript: ^4.2.4 - vite: ^2.3.1 dependencies: '@fontsource/fira-mono': 4.2.2 '@lukeed/uuid': 2.0.0 @@ -194,15 +193,14 @@ importers: svelte: 3.38.2 svelte-preprocess: 4.7.3_svelte@3.38.2+typescript@4.2.4 typescript: 4.2.4 - vite: 2.3.1 packages/kit: specifiers: '@rollup/plugin-replace': ^2.4.2 - '@sveltejs/kit': workspace:* '@sveltejs/vite-plugin-svelte': ^1.0.0-next.10 '@types/amphtml-validator': ^1.0.1 '@types/cookie': ^0.4.0 + '@types/globrex': ^0.1.0 '@types/marked': ^2.0.2 '@types/mime': ^2.0.3 '@types/node': ^14.14.43 @@ -213,7 +211,9 @@ importers: cookie: ^0.4.1 devalue: ^2.0.1 eslint: ^7.25.0 + globrex: ^0.1.2 kleur: ^4.1.4 + locate-character: ^2.0.5 marked: ^2.0.3 node-fetch: ^3.0.0-beta.9 port-authority: ^1.1.2 @@ -221,23 +221,23 @@ importers: rollup: ^2.47.0 sade: ^1.7.4 selfsigned: ^1.10.11 - sirv: ^1.0.11 + sirv: ^1.0.12 svelte: ^3.38.2 svelte-check: ^1.5.2 tiny-glob: ^0.2.8 typescript: ^4.2.4 uvu: ^0.5.1 - vite: ^2.3.1 + vite: 2.3.3 dependencies: - '@sveltejs/vite-plugin-svelte': 1.0.0-next.10_fa3f9f81b9199d842185ad66f92f1626 + '@sveltejs/vite-plugin-svelte': 1.0.0-next.10_92cabc58848f43a8976d66b1396ccdc0 cheap-watch: 1.0.3 sade: 1.7.4 - vite: 2.3.1 + vite: 2.3.3 devDependencies: '@rollup/plugin-replace': 2.4.2_rollup@2.47.0 - '@sveltejs/kit': 'link:' '@types/amphtml-validator': 1.0.1 '@types/cookie': 0.4.0 + '@types/globrex': 0.1.0 '@types/marked': 2.0.2 '@types/mime': 2.0.3 '@types/node': 14.14.43 @@ -247,14 +247,16 @@ importers: cookie: 0.4.1 devalue: 2.0.1 eslint: 7.25.0 + globrex: 0.1.2 kleur: 4.1.4 + locate-character: 2.0.5 marked: 2.0.3 node-fetch: 3.0.0-beta.9 port-authority: 1.1.2 rimraf: 3.0.2 rollup: 2.47.0 selfsigned: 1.10.11 - sirv: 1.0.11 + sirv: 1.0.12 svelte: 3.38.2 svelte-check: 1.5.2_svelte@3.38.2 tiny-glob: 0.2.8 @@ -558,8 +560,8 @@ packages: fastq: 1.11.0 dev: true - /@polka/url/1.0.0-next.12: - resolution: {integrity: sha512-6RglhutqrGFMO1MNUXp95RBuYIuc8wTnMAV5MUhLmjTOy78ncwOw7RgeQ/HeymkKXRhZd0s2DNrM1rL7unk3MQ==} + /@polka/url/1.0.0-next.15: + resolution: {integrity: sha512-15spi3V28QdevleWBNXE4pIls3nFZmBbUGrW9IVPwiQczuSb9n76TCB4bsk8TSel+I1OkHEdPhu5QKMfY6rQHA==} dev: true /@rollup/plugin-commonjs/17.1.0_rollup@2.47.0: @@ -635,7 +637,7 @@ packages: rollup: 2.47.0 dev: false - /@sveltejs/vite-plugin-svelte/1.0.0-next.10_fa3f9f81b9199d842185ad66f92f1626: + /@sveltejs/vite-plugin-svelte/1.0.0-next.10_92cabc58848f43a8976d66b1396ccdc0: resolution: {integrity: sha512-ImvxbhPePm2hWNTKBSA3LHAYGwiEjHjvvgfPLXm4R87sfZ+BMXql9jBmDpzUC/URBLT4BB3Jxos/i523qkJBHg==} engines: {node: '>=12.0.0'} peerDependencies: @@ -651,7 +653,7 @@ packages: source-map: 0.7.3 svelte: 3.38.2 svelte-hmr: 0.14.3_svelte@3.38.2 - vite: 2.3.1 + vite: 2.3.3 transitivePeerDependencies: - rollup - supports-color @@ -686,6 +688,10 @@ packages: '@types/node': 14.14.43 dev: true + /@types/globrex/0.1.0: + resolution: {integrity: sha512-aBkxDgp/UbnluE+CIT3V3PoNewwOlLCzXSF3ipD86Slv8xVjwxrDAfSGbsfGgMzPo/fEMPXc+gNUJbtiugwfoA==} + dev: true + /@types/istanbul-lib-coverage/2.0.3: resolution: {integrity: sha512-sz7iLqvVUg1gIedBOvlkxPlc8/uVzyS5OwGz1cKjXzkl3FpL3al0crU8YGU1WoHkxn0Wxbw5tyi6hvzJKNzFsw==} dev: true @@ -1251,6 +1257,7 @@ packages: /colorette/1.2.2: resolution: {integrity: sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==} + dev: false /colors/1.4.0: resolution: {integrity: sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==} @@ -1524,10 +1531,11 @@ packages: requiresBuild: true dev: false - /esbuild/0.11.20: - resolution: {integrity: sha512-QOZrVpN/Yz74xfat0H6euSgn3RnwLevY1mJTEXneukz1ln9qB+ieaerRMzSeETpz/UJWsBMzRVR/andBht5WKw==} + /esbuild/0.11.23: + resolution: {integrity: sha512-iaiZZ9vUF5wJV8ob1tl+5aJTrwDczlvGP0JoMmnpC2B0ppiMCu8n8gmy5ZTGl5bcG081XBVn+U+jP+mPFm5T5Q==} hasBin: true requiresBuild: true + dev: false /escalade/3.1.1: resolution: {integrity: sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==} @@ -2343,6 +2351,10 @@ packages: strip-bom: 3.0.0 dev: true + /locate-character/2.0.5: + resolution: {integrity: sha512-n2GmejDXtOPBAZdIiEFy5dJ5N38xBCXLNOtw2WpB9kGh6pnrEuKlwYI+Tkpofc4wDtVXHtoAOJaMRlYG/oYaxg==} + dev: true + /locate-path/2.0.0: resolution: {integrity: sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=} engines: {node: '>=4'} @@ -2528,6 +2540,7 @@ packages: resolution: {integrity: sha512-/2ZUaJX2ANuLtTvqTlgqBQNJoQO398KyJgZloL0PZkC0dpysjncRUPsFe3DUPzz/y3h+u7C46np8RMuvF3jsSQ==} engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} hasBin: true + dev: false /natural-compare/1.4.0: resolution: {integrity: sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=} @@ -2847,11 +2860,11 @@ packages: engines: {node: '>=10.13.0'} dev: true - /polka/1.0.0-next.14: - resolution: {integrity: sha512-nRxh6lifEred8F973wMJ3YA7z8EBAf+lWFLbhIh4MnYJXmZododD8FDGj+R81xnuNNG8b4PDqNXvrB5ShKcStA==} - engines: {node: '>=6'} + /polka/1.0.0-next.15: + resolution: {integrity: sha512-zBCZO40+USkSj0GDHMqufthqk4TIRc9xVGd50LbMvYNEwGHK8dZczLBQtw9pPHBM+i/Xg7ed7+c+r6J68XkWLg==} + engines: {node: '>=8'} dependencies: - '@polka/url': 1.0.0-next.12 + '@polka/url': 1.0.0-next.15 trouter: 3.2.0 dev: true @@ -2866,6 +2879,7 @@ packages: colorette: 1.2.2 nanoid: 3.1.22 source-map: 0.6.1 + dev: false /preferred-pm/3.0.3: resolution: {integrity: sha512-+wZgbxNES/KlJs9q40F/1sfOd/j7f1O9JaHcW5Dsn3aUUOZg3L2bjpVUcKV2jvtElYfoTuQiNeMfQJ4kwUAhCQ==} @@ -3168,11 +3182,11 @@ packages: resolution: {integrity: sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA==} dev: true - /sirv/1.0.11: - resolution: {integrity: sha512-SR36i3/LSWja7AJNRBz4fF/Xjpn7lQFI30tZ434dIy+bitLYSP+ZEenHg36i23V2SGEz+kqjksg0uOGZ5LPiqg==} + /sirv/1.0.12: + resolution: {integrity: sha512-+jQoCxndz7L2tqQL4ZyzfDhky0W/4ZJip3XoOuxyQWnAwMxindLl3Xv1qT4x1YX/re0leShvTm8Uk0kQspGhBg==} engines: {node: '>= 10'} dependencies: - '@polka/url': 1.0.0-next.12 + '@polka/url': 1.0.0-next.15 mime: 2.5.2 totalist: 1.1.0 dev: true @@ -3215,6 +3229,7 @@ packages: /source-map/0.6.1: resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} engines: {node: '>=0.10.0'} + dev: false /source-map/0.7.3: resolution: {integrity: sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==} @@ -3725,17 +3740,18 @@ packages: engines: {node: '>= 0.8'} dev: true - /vite/2.3.1: - resolution: {integrity: sha512-yvcLclv/G0syHL+bBtahdaymH0x6La8kbTlnAssRH+XFvDNb8VPRruHhILCCmZgtx6Zu/UKNFTQnQ4Gk/omyUA==} + /vite/2.3.3: + resolution: {integrity: sha512-eO1iwRbn3/BfkNVMNJDeANAFCZ5NobYOFPu7IqfY7DcI7I9nFGjJIZid0EViTmLDGwwSUPmRAq3cRBbO3+DsMA==} engines: {node: '>=12.0.0'} hasBin: true dependencies: - esbuild: 0.11.20 + esbuild: 0.11.23 postcss: 8.2.14 resolve: 1.20.0 rollup: 2.47.0 optionalDependencies: fsevents: 2.3.2 + dev: false /wcwidth/1.0.1: resolution: {integrity: sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g=}