Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate declaration files #9895

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"rollup": "^3.7.0",
"svelte": "^3.56.0",
"tiny-glob": "^0.2.9",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"packageManager": "pnpm@8.4.0",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-auto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@types/node": "^16.18.6",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"dependencies": {
"import-meta-resolve": "^3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"devDependencies": {
"@cloudflare/kv-asset-handler": "^0.3.0",
"@types/node": "^16.18.6",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"peerDependencies": {
"@sveltejs/kit": "^1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"devDependencies": {
"@types/node": "^16.18.6",
"@types/ws": "^8.5.3",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
},
"peerDependencies": {
"@sveltejs/kit": "^1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@types/set-cookie-parser": "^2.4.2",
"rimraf": "^5.0.0",
"rollup": "^3.7.0",
"typescript": "^4.9.4",
"typescript": "^5.0.4",
"uvu": "^0.5.6"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"polka": "^1.0.0-next.22",
"rimraf": "^5.0.0",
"sirv": "^2.0.3",
"typescript": "^4.9.4",
"typescript": "^5.0.4",
"uvu": "^0.5.6"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"@types/node": "^16.18.6",
"sirv": "^2.0.3",
"svelte": "^3.56.0",
"typescript": "^4.9.4",
"typescript": "^5.0.4",
"vite": "^4.3.0"
},
"peerDependencies": {
"@sveltejs/kit": "^1.5.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/adapter-vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"devDependencies": {
"@sveltejs/kit": "workspace:^",
"@types/node": "^16.18.6",
"typescript": "^4.9.4",
"typescript": "^5.0.4",
"uvu": "^0.5.6"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@sveltejs/adapter-auto": "workspace:*",
"@sveltejs/kit": "workspace:*",
"svelte": "^3.56.0",
"typescript": "^5.0.0",
"typescript": "^5.0.4",
"vite": "^4.3.0"
},
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions packages/kit/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
!/test/node_modules
/test/apps/basics/test/errors.json
.custom-out-dir
/types

# these are already ignored by the top level .gitignore
# repeating them here as a faux prettier ignore
Expand Down
35 changes: 28 additions & 7 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"sade": "^1.8.1",
"set-cookie-parser": "^2.6.0",
"sirv": "^2.0.2",
"tiny-glob": "^0.2.9",
"undici": "~5.22.0"
},
"devDependencies": {
Expand All @@ -37,7 +36,8 @@
"rollup": "^3.7.0",
"svelte": "^3.56.0",
"svelte-preprocess": "^5.0.3",
"typescript": "^4.9.4",
"tiny-glob": "^0.2.9",
"typescript": "^5.0.4",
"uvu": "^0.5.6",
"vite": "^4.3.0"
},
Expand Down Expand Up @@ -67,29 +67,50 @@
"test:cross-platform:dev": "pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test:cross-platform:dev",
"test:cross-platform:build": "pnpm test:unit && pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test:cross-platform:build",
"test:unit": "uvu src \"(spec\\.js|test[\\\\/]index\\.js)\"",
"postinstall": "node postinstall.js"
"postinstall": "node postinstall.js",
"prepublishOnly": "tsc -p tsconfig.build.json && node scripts/process-declaration-files"
},
"exports": {
"./package.json": "./package.json",
".": {
"types": "./types/index.d.ts",
"types": "./types/exports/types.d.ts",
"import": "./src/exports/index.js"
},
"./node": {
"types": "./types/exports/node/index.d.ts",
"import": "./src/exports/node/index.js"
},
"./node/polyfills": {
"import": "./src/exports/node/polyfills.js"
"types": "./types/exports/node/polyfills/index.d.ts",
"import": "./src/exports/node/polyfills/index.js"
},
"./hooks": {
"types": "./types/exports/hooks/index.d.ts",
"import": "./src/exports/hooks/index.js"
},
"./vite": {
"types": "./types/exports/vite/index.d.ts",
"import": "./src/exports/vite/index.js"
}
},
"types": "types/index.d.ts",
"types": "./types/exports/types.d.ts",
"typesVersions": {
">4.0": {
"hooks": [
"./types/exports/hooks/index.d.ts"
],
"node": [
"./types/exports/node/index.d.ts"
],
"node/polyfills": [
"./types/exports/node/polyfills/index.d.ts"
],
"vite": [
"./types/exports/vite/index.d.ts"
]
}
},
"engines": {
"node": "^16.14 || >=18"
}
}
}
5 changes: 0 additions & 5 deletions packages/kit/scripts/cp.js

This file was deleted.

23 changes: 23 additions & 0 deletions packages/kit/scripts/process-declaration-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from 'node:fs';
import path from 'node:path';
import glob from 'tiny-glob/sync.js';

glob('**/*.d.ts', { cwd: 'src' }).forEach((file) => {
fs.copyFileSync(`src/${file}`, `types/${file}`);
});

glob('types/**/*.d.ts').forEach((file) => {
// we need to rewrite `import('types')` as `import('../../types')` for anyone
// not using moduleResolution: 'bundler'

const dir = path.dirname(file);
const path_to_types = path.relative(dir, 'types/exports/types');
const path_to_public_types = path.relative(dir, 'types/types/public');

const contents = fs
.readFileSync(file, 'utf-8')
.replace(/import\('types'\)/g, `import('${path_to_types}')`)
.replace(/import\('@sveltejs\/kit'\)/g, `import('${path_to_public_types}')`);

fs.writeFileSync(file, contents);
});
2 changes: 1 addition & 1 deletion packages/kit/src/core/postbuild/analyse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { load_config } from '../config/index.js';
import { forked } from '../../utils/fork.js';
import { should_polyfill } from '../../utils/platform.js';
import { installPolyfills } from '../../exports/node/polyfills.js';
import { installPolyfills } from '../../exports/node/polyfills/index.js';
import { resolve_entry } from '../../utils/routing.js';

export default forked(import.meta.url, analyse);
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/postbuild/fallback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { installPolyfills } from '../../exports/node/polyfills.js';
import { installPolyfills } from '../../exports/node/polyfills/index.js';
import { load_config } from '../config/index.js';
import { forked } from '../../utils/fork.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { installPolyfills } from '../../exports/node/polyfills.js';
import { installPolyfills } from '../../exports/node/polyfills/index.js';
import { mkdirp, posixify, walk } from '../../utils/filesystem.js';
import { should_polyfill } from '../../utils/platform.js';
import { decode_uri, is_root_relative, resolve } from '../../utils/url.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/write_ambient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { write_if_changed } from './utils.js';

// TODO these types should be described in a neutral place, rather than
// inside either `packages/kit` or `kit.svelte.dev`
const descriptions_dir = fileURLToPath(new URL('../../../types/synthetic', import.meta.url));
const descriptions_dir = fileURLToPath(new URL('../../../src/types/synthetic', import.meta.url));

/** @param {string} filename */
function read_description(filename) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fail } from '../../../../../../types/internal.js';
import { fail } from '../../../../../../src/types/internal.js';

let condition = false;

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/sync/write_types/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"paths": {
"@sveltejs/kit": ["../../../../../types/index"]
"@sveltejs/kit": ["../../../../../src/types/index"]
}
},
"include": ["./**/*.js"],
Expand Down
66 changes: 65 additions & 1 deletion packages/kit/src/exports/hooks/sequence.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
/**
* @param {...import('types').Handle} handlers
* A helper function for sequencing multiple `handle` calls in a middleware-like manner.
* The behavior for the `handle` options is as follows:
* - `transformPageChunk` is applied in reverse order and merged
* - `preload` is applied in forward order, the first option "wins" and no `preload` options after it are called
* - `filterSerializedResponseHeaders` behaves the same as `preload`
*
* ```js
* /// file: src/hooks.server.js
* import { sequence } from '@sveltejs/kit/hooks';
*
* /// type: import('@sveltejs/kit').Handle
* async function first({ event, resolve }) {
* console.log('first pre-processing');
* const result = await resolve(event, {
* transformPageChunk: ({ html }) => {
* // transforms are applied in reverse order
* console.log('first transform');
* return html;
* },
* preload: () => {
* // this one wins as it's the first defined in the chain
* console.log('first preload');
* }
* });
* console.log('first post-processing');
* return result;
* }
*
* /// type: import('@sveltejs/kit').Handle
* async function second({ event, resolve }) {
* console.log('second pre-processing');
* const result = await resolve(event, {
* transformPageChunk: ({ html }) => {
* console.log('second transform');
* return html;
* },
* preload: () => {
* console.log('second preload');
* },
* filterSerializedResponseHeaders: () => {
* // this one wins as it's the first defined in the chain
* console.log('second filterSerializedResponseHeaders');
* }
* });
* console.log('second post-processing');
* return result;
* }
*
* export const handle = sequence(first, second);
* ```
*
* The example above would print:
*
* ```
* first pre-processing
* first preload
* second pre-processing
* second filterSerializedResponseHeaders
* second transform
* first transform
* second post-processing
* first post-processing
* ```
*
* @param {...import('types').Handle} handlers The chain of `handle` functions
* @returns {import('types').Handle}
*/
export function sequence(...handlers) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/hooks/sequence.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { sequence } from './sequence.js';
import { installPolyfills } from '../node/polyfills.js';
import { installPolyfills } from '../node/polyfills/index.js';

installPolyfills();

Expand Down
Loading