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 .d.ts bundle from source #9992

Merged
merged 43 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
2fdff69
use dts-buddy
May 19, 2023
a7ecb57
updates
May 19, 2023
9c0712e
fixes
May 19, 2023
ae74981
tidy up
May 19, 2023
595d003
rename
May 19, 2023
ed7ffcd
format
May 19, 2023
a252c27
bump dts-buddy
May 19, 2023
1f4160d
bump
May 22, 2023
db03744
add prepublishOnly script
May 22, 2023
b802f6c
build types before running check
May 22, 2023
6f7a4fe
Invalid should be Failure
May 22, 2023
a664437
ugh
May 22, 2023
c10a9a3
fix
May 22, 2023
ccd520f
fix
May 22, 2023
1b4a0cb
fix
May 22, 2023
0f7a61f
get site building
May 22, 2023
e563689
try this
May 22, 2023
e220595
Merge branch 'master' into dts-buddy
Rich-Harris May 23, 2023
1017a49
fix
May 23, 2023
38c9e4f
reinstate code
May 23, 2023
05bd2b8
appease eslint
May 23, 2023
7c9ca73
fix some stuff
May 23, 2023
96f0d25
prettier
May 23, 2023
d7e15d1
fix
May 23, 2023
c428829
appease typescript
May 23, 2023
2bab43c
fix
May 23, 2023
8e7d00e
"eslint: so you can remember why you hate programming"
May 23, 2023
998b5a1
remove unused types
May 23, 2023
b9ea65b
use proper imports for ambient types
May 23, 2023
4a041fe
jfc eslint fuck OFF
May 23, 2023
2a054e1
try this
May 23, 2023
9368ee2
bump dts-buddy
May 23, 2023
e8c945f
try this
May 23, 2023
1a4211d
function interfaces need to be types now. who the hell knows why
May 23, 2023
4376994
hide-the-pain-harold.jpg
May 23, 2023
2c4b8ab
juggle some stuff
May 23, 2023
c36447d
jfc
May 23, 2023
8a92705
expose __sveltekit/paths and __sveltekit/environment
May 24, 2023
d01d60c
exclude internal module declarations
May 24, 2023
eee8be0
fix docs
May 24, 2023
721fc90
Update packages/kit/src/exports/public.d.ts
Rich-Harris May 24, 2023
09c13ff
format
May 24, 2023
536f474
rename message to body
May 25, 2023
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
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.
benmccann marked this conversation as resolved.
Show resolved Hide resolved
* 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