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

fix: backwards compatibility for types #8721

Merged
merged 19 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/mighty-suns-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure types are loaded with all TS settings
5 changes: 5 additions & 0 deletions .changeset/tame-peaches-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: export correct SvelteComponent type
9 changes: 9 additions & 0 deletions packages/svelte/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@
_actual*.*
_output
/types

action.d.ts
animate.d.ts
compiler.d.ts
easing.d.ts
index.d.ts
motion.d.ts
store.d.ts
transition.d.ts
2 changes: 0 additions & 2 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"compiler.*",
"register.js",
"index.d.ts",
"internal.d.ts",
"store.d.ts",
"animate.d.ts",
"transition.d.ts",
Expand Down Expand Up @@ -47,7 +46,6 @@
"import": "./src/runtime/easing/index.js"
},
"./internal": {
"types": "./types/index.d.ts",
"import": "./src/runtime/internal/index.js"
},
"./motion": {
Expand Down
23 changes: 23 additions & 0 deletions packages/svelte/scripts/generate-dts.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import * as fs from 'fs';
import { createBundle } from 'dts-buddy';

// It may look weird, but the imports MUST be ending with index.js to be properly resolved in all TS modes
for (const name of ['action', 'animate', 'easing', 'motion', 'store', 'transition']) {
fs.writeFileSync(`${name}.d.ts`, `import './types/index.js';`);
}

fs.writeFileSync('index.d.ts', `import './types/index.js';`);
fs.writeFileSync('compiler.d.ts', `import './types/index.js';`);

// TODO: some way to mark these as deprecated
fs.mkdirSync('./types/compiler', { recursive: true });
fs.writeFileSync('./types/compiler/preprocess.d.ts', `import '../index.js';`);
fs.writeFileSync('./types/compiler/interfaces.d.ts', `import '../index.js';`);

await createBundle({
output: 'types/index.d.ts',
modules: {
svelte: 'src/runtime/public.d.ts',
'svelte/compiler': 'src/compiler/public.d.ts',
'svelte/types/compiler/preprocess': 'src/compiler/preprocess/public.d.ts',
'svelte/types/compiler/interfaces': 'src/compiler/interfaces.d.ts',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What uses this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vite plugin svelte and at least one other thing on the CI list

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should update them to use svelte/compiler as soon as v4 is out, so that these entry points can be dropped in v5

'svelte/action': 'src/runtime/action/public.d.ts',
'svelte/animate': 'src/runtime/animate/public.d.ts',
'svelte/easing': 'src/runtime/easing/index.js',
Expand All @@ -14,3 +29,11 @@ await createBundle({
'svelte/transition': 'src/runtime/transition/public.d.ts'
}
});

// There's no way to tell in JS that a class can have arbitrary properties, so we need to add that manually
const types = fs.readFileSync('types/index.d.ts', 'utf-8');
fs.writeFileSync(
'types/index.d.ts',
// same line to not affect source map
types.replace(/export class SvelteComponent<[^{]*{/, '$& [prop: string]: any;')
);
2 changes: 1 addition & 1 deletion packages/svelte/src/runtime/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare module '*.svelte' {
export { SvelteComponentDev as default } from 'svelte/internal';
export { SvelteComponent as default } from 'svelte';
}
2 changes: 1 addition & 1 deletion packages/svelte/src/runtime/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {
SvelteComponentDev as SvelteComponent,
onMount,
onDestroy,
beforeUpdate,
Expand All @@ -9,6 +10,5 @@ export {
hasContext,
tick,
createEventDispatcher,
SvelteComponentDev as SvelteComponent,
SvelteComponentTyped
} from './internal/index.js';
1 change: 0 additions & 1 deletion packages/svelte/src/runtime/internal/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ export function create_custom_element(
*
* @template {Record<string, any>} [Props=any]
* @template {Record<string, any>} [Events=any]
* @implements Record<string, any>
*/
export class SvelteComponent {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/runtime/internal/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export function construct_svelte_component_dev(component, props) {
* @template {Record<string, any>} [Props=any]
* @template {Record<string, any>} [Events=any]
* @template {Record<string, any>} [Slots=any]
* @extends SvelteComponent<Props, Events>
* @extends {SvelteComponent<Props, Events>}
*/
export class SvelteComponentDev extends SvelteComponent {
/**
Expand Down Expand Up @@ -350,7 +350,7 @@ export class SvelteComponentDev extends SvelteComponent {
* @template {Record<string, any>} [Events=any]
* @template {Record<string, any>} [Slots=any]
* @deprecated Use `SvelteComponent` instead. See PR for more information: https://github.com/sveltejs/svelte/pull/8512
* @extends SvelteComponentDev<Props, Events, Slots>
* @extends {SvelteComponentDev<Props, Events, Slots>}
*/
export class SvelteComponentTyped extends SvelteComponentDev {}

Expand Down
2 changes: 0 additions & 2 deletions packages/svelte/src/runtime/internal/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,3 @@ export interface EventDispatcher<EventMap extends Record<string, any>> {
: [type: Type, parameter: EventMap[Type], options?: DispatchOptions]
): boolean;
}

export * from './index.js';
21 changes: 19 additions & 2 deletions packages/svelte/src/runtime/public.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import './ambient.js';

// Types written in this particular order to work around a dts-buddy bug where it doesn't handle the
// SvelteComponentDev as SvelteComponent alias correctly.
// If that's fixed, we can do export * from './index.js' instead.

export {
SvelteComponent,
SvelteComponentTyped,
onMount,
onDestroy,
beforeUpdate,
afterUpdate,
setContext,
getContext,
getAllContexts,
hasContext,
tick,
createEventDispatcher
} from './index.js';

export type {
ComponentConstructorOptions,
ComponentEvents,
ComponentProps,
ComponentType
} from './internal/public.js';

export * from './index.js';
1 change: 1 addition & 0 deletions packages/svelte/src/runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

"baseUrl": ".",
"paths": {
"svelte": ["."],
"svelte/*": ["*"]
},
"allowJs": true,
Expand Down