Skip to content

Commit

Permalink
Generate .d.ts bundle from source (#9992)
Browse files Browse the repository at this point in the history
* use dts-buddy

* updates

* fixes

* tidy up

* rename

* format

* bump dts-buddy

* bump

* add prepublishOnly script

* build types before running check

* Invalid should be Failure

* ugh

* fix

* fix

* fix

* get site building

* try this

* fix

* reinstate code

* appease eslint

* fix some stuff

* prettier

* fix

* appease typescript

* fix

* "eslint: so you can remember why you hate programming"

* remove unused types

* use proper imports for ambient types

* jfc eslint fuck OFF

* try this

* bump dts-buddy

* try this

* function interfaces need to be types now. who the hell knows why

* hide-the-pain-harold.jpg

* juggle some stuff

* jfc

* expose __sveltekit/paths and __sveltekit/environment

* exclude internal module declarations

* fix docs

* Update packages/kit/src/exports/public.d.ts

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

* format

---------

Co-authored-by: Rich Harris <git@rich-harris.dev>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
  • Loading branch information
3 people authored May 25, 2023
1 parent 81af6ba commit 9d8376f
Show file tree
Hide file tree
Showing 67 changed files with 804 additions and 836 deletions.
9 changes: 8 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
"env": {
"es2022": true
},
"ignorePatterns": ["packages/create-svelte/shared/"],
"ignorePatterns": [
"packages/create-svelte/shared/",
"packages/kit/test/prerendering/*/build",
"packages/adapter-static/test/apps/*/build",
"packages/adapter-cloudflare/files",
"packages/adapter-netlify/files",
"packages/adapter-node/files"
],
"rules": {
"no-undef": "off"
}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm run lint
- run: cd packages/kit && pnpm prepublishOnly
- run: pnpm run check
Tests:
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -136,4 +137,5 @@ jobs:
node-version: 16
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: cd packages/kit && pnpm prepublishOnly
- run: pnpm run test:create-svelte
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test:cross-platform:build": "pnpm run --dir packages/kit test:cross-platform:build",
"test:vite-ecosystem-ci": "pnpm test --dir packages/kit",
"test:create-svelte": "pnpm run --dir packages/create-svelte test",
"check": "pnpm -r check",
"check": "pnpm -r prepublishOnly && pnpm -r check",
"lint": "pnpm -r lint && eslint 'packages/**/*.js'",
"format": "pnpm -r format",
"precommit": "pnpm format && pnpm lint",
Expand Down
1 change: 1 addition & 0 deletions packages/kit/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
!/src/core/adapt/fixtures/*/.svelte-kit
!/test/node_modules
/test/apps/basics/test/errors.json
/types
.custom-out-dir

# these are already ignored by the top level .gitignore
Expand Down
8 changes: 7 additions & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/node": "^16.18.6",
"@types/sade": "^1.7.4",
"@types/set-cookie-parser": "^2.4.2",
"dts-buddy": "^0.0.9",
"marked": "^4.2.3",
"rollup": "^3.7.0",
"svelte": "^3.56.0",
Expand Down Expand Up @@ -67,7 +68,8 @@
"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": "vitest --config kit.vitest.config.js run",
"postinstall": "node postinstall.js"
"postinstall": "node postinstall.js",
"prepublishOnly": "node scripts/generate-dts.js"
},
"exports": {
"./package.json": "./package.json",
Expand All @@ -76,15 +78,19 @@
"import": "./src/exports/index.js"
},
"./node": {
"types": "./types/index.d.ts",
"import": "./src/exports/node/index.js"
},
"./node/polyfills": {
"types": "./types/index.d.ts",
"import": "./src/exports/node/polyfills.js"
},
"./hooks": {
"types": "./types/index.d.ts",
"import": "./src/exports/hooks/index.js"
},
"./vite": {
"types": "./types/index.d.ts",
"import": "./src/exports/vite/index.js"
}
},
Expand Down
18 changes: 18 additions & 0 deletions packages/kit/scripts/generate-dts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createBundle } from 'dts-buddy';

createBundle({
output: 'types/index.d.ts',
modules: {
'@sveltejs/kit': 'src/exports/public.d.ts',
'@sveltejs/kit/hooks': 'src/exports/hooks/index.js',
'@sveltejs/kit/node': 'src/exports/node/index.js',
'@sveltejs/kit/node/polyfills': 'src/exports/node/polyfills.js',
'@sveltejs/kit/vite': 'src/exports/vite/index.js',
'$app/environment': 'src/runtime/app/environment.js',
'$app/forms': 'src/runtime/app/forms.js',
'$app/navigation': 'src/runtime/app/navigation.js',
'$app/paths': 'src/runtime/app/paths.js',
'$app/stores': 'src/runtime/app/stores.js'
},
include: ['src']
});
6 changes: 3 additions & 3 deletions packages/kit/src/core/adapt/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const pipe = promisify(pipeline);
* prerender_map: import('types').PrerenderMap;
* log: import('types').Logger;
* }} opts
* @returns {import('types').Builder}
* @returns {import('@sveltejs/kit').Builder}
*/
export function create_builder({
config,
Expand All @@ -35,7 +35,7 @@ export function create_builder({
prerender_map,
log
}) {
/** @type {Map<import('types').RouteDefinition, import('types').RouteData>} */
/** @type {Map<import('@sveltejs/kit').RouteDefinition, import('types').RouteData>} */
const lookup = new Map();

/**
Expand All @@ -47,7 +47,7 @@ export function create_builder({
server_metadata.routes.get(route.id)
);

/** @type {import('types').RouteDefinition} */
/** @type {import('@sveltejs/kit').RouteDefinition} */
const facade = {
id: route.id,
api,
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/adapt/builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('copy files', () => {
const cwd = join(__dirname, 'fixtures/basic');
const outDir = join(cwd, '.svelte-kit');

/** @type {import('types').Config} */
/** @type {import('@sveltejs/kit').Config} */
const mocked = {
extensions: ['.svelte'],
kit: {
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function load_config({ cwd = process.cwd() } = {}) {
}

/**
* @param {import('types').Config} config
* @param {import('@sveltejs/kit').Config} config
* @returns {import('types').ValidatedConfig}
*/
function process_config(config, { cwd = process.cwd() } = {}) {
Expand All @@ -95,7 +95,7 @@ function process_config(config, { cwd = process.cwd() } = {}) {
}

/**
* @param {import('types').Config} config
* @param {import('@sveltejs/kit').Config} config
* @returns {import('types').ValidatedConfig}
*/
export function validate_config(config) {
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ test('fails if prerender.entries are invalid', () => {

/**
* @param {string} name
* @param {import('types').KitConfig['paths']} input
* @param {import('types').KitConfig['paths']} output
* @param {import('@sveltejs/kit').KitConfig['paths']} input
* @param {import('@sveltejs/kit').KitConfig['paths']} output
*/
function validate_paths(name, input, output) {
test(name, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/generate_manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function generate_manifest({ build_data, relative_path, routes }) {

// prettier-ignore
// String representation of
/** @type {import('types').SSRManifest} */
/** @type {import('@sveltejs/kit').SSRManifest} */
return dedent`
{
appDir: ${s(build_data.app_dir)},
Expand Down
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 @@ -23,7 +23,7 @@ export default forked(import.meta.url, analyse);
* }} opts
*/
async function analyse({ manifest_path, env }) {
/** @type {import('types').SSRManifest} */
/** @type {import('@sveltejs/kit').SSRManifest} */
const manifest = (await import(pathToFileURL(manifest_path).href)).manifest;

/** @type {import('types').ValidatedKitConfig} */
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
Expand Up @@ -27,7 +27,7 @@ async function generate_fallback({ manifest_path, env }) {
/** @type {import('types').ServerModule} */
const { Server } = await import(pathToFileURL(`${server_root}/server/index.js`).href);

/** @type {import('types').SSRManifest} */
/** @type {import('@sveltejs/kit').SSRManifest} */
const manifest = (await import(pathToFileURL(manifest_path).href)).manifest;

set_building(true);
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
Expand Up @@ -26,7 +26,7 @@ export default forked(import.meta.url, prerender);
* }} opts
*/
async function prerender({ out, manifest_path, metadata, verbose, env }) {
/** @type {import('types').SSRManifest} */
/** @type {import('@sveltejs/kit').SSRManifest} */
const manifest = (await import(pathToFileURL(manifest_path).href)).manifest;

/** @type {import('types').ServerInternalModule} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const cwd = fileURLToPath(new URL('./test', import.meta.url));

/**
* @param {string} dir
* @param {import('types').Config} config
* @param {import('@sveltejs/kit').Config} config
*/
const create = (dir, config = {}) => {
const initial = options(config, 'config');
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/exports/index.js';

const condition = false;

Expand Down Expand Up @@ -35,3 +35,57 @@ export const actions = {
return fail(400);
}
};

/**
* Ordinarily this would live in a +page.svelte, but to make it easy to run the tests, we put it here.
* The `export` is so that eslint doesn't throw a hissy fit about the unused variable
* @type {import('./.svelte-kit/types/src/core/sync/write_types/test/actions/$types').SubmitFunction}
*/
export const submit = () => {
return ({ result }) => {
if (result.type === 'success') {
// @ts-expect-error does only exist on `failure` result
result.data?.fail;
// @ts-expect-error unknown property
result.data?.something;

if (result.data && 'success' in result.data) {
result.data.success === true;
// @ts-expect-error should be of type `boolean`
result.data.success === 'success';
// @ts-expect-error does not exist in this branch
result.data.id;
}

if (result.data && 'id' in result.data) {
result.data.id === 42;
// @ts-expect-error should be of type `number`
result.data.id === 'John';
// @ts-expect-error does not exist in this branch
result.data.success;
}
}

if (result.type === 'failure') {
result.data;
// @ts-expect-error does only exist on `success` result
result.data.success;
// @ts-expect-error unknown property
result.data.unknown;

if (result.data && 'fail' in result.data) {
result.data.fail === '';
// @ts-expect-error does not exist in this branch
result.data.reason;
}

if (result.data && 'reason' in result.data) {
result.data.reason.error.code === 'VALIDATION_FAILED';
// @ts-expect-error should be a const
result.data.reason.error.code === '';
// @ts-expect-error does not exist in this branch
result.data.fail;
}
}
};
};
3 changes: 2 additions & 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,8 @@
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"paths": {
"@sveltejs/kit": ["../../../../../types/index"]
"@sveltejs/kit": ["../../../../exports/public"],
"types": ["../../../../types/internal"]
}
},
"include": ["./**/*.js"],
Expand Down
Loading

0 comments on commit 9d8376f

Please sign in to comment.