Skip to content
Draft
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
52 changes: 51 additions & 1 deletion .github/scripts/packed-consumer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
mkdtempSync,
readFileSync,
readdirSync,
realpathSync,
rmSync,
writeFileSync,
} from 'node:fs';
import { createRequire } from 'node:module';
import { tmpdir } from 'node:os';
import { join } from 'node:path/posix';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -357,9 +359,57 @@ try {
mkdirSync(join(fixture, 'public'), { recursive: true });
writeFileSync(join(fixture, 'public/packed.txt'), label);
await run(process.execPath, [vite, 'build'], fixture);
if (framework === 'svelte') {
const adapter = join(fixture, 'node_modules/@void/svelte');
const manifest = JSON.parse(readFileSync(join(adapter, 'package.json'), 'utf8'));
const rootExport = manifest.exports['.'];
assert.equal(Object.keys(rootExport)[0], 'types');
mkdirSync(join(fixture, 'src'), { recursive: true });
const probe = join(fixture, 'src/packed-svelte-root.mts');
writeFileSync(
probe,
`import { Form, Link } from '@void/svelte';
import type { ComponentProps } from 'svelte';
// @ts-expect-error Form requires a model
export const missingForm: ComponentProps<typeof Form> = {};
// @ts-expect-error Link href excludes numbers
export const wrongHref: ComponentProps<typeof Link>['href'] = 123;
export const href: ComponentProps<typeof Link>['href'] = '/';
`,
);
const require = createRequire(join(root, 'packages/svelte/package.json'));
const ts = createRequire(require.resolve('svelte2tsx'))('typescript');
const program = ts.createProgram([probe], {
module: ts.ModuleKind.Node16,
target: ts.ScriptTarget.ES2022,
strict: true,
customConditions: ['svelte'],
});
const directory = realpathSync(adapter);
assert(program.getSourceFile(join(directory, rootExport.types)));
for (const file of program.getSourceFiles()) {
if (file.fileName.startsWith(directory + '/')) {
assert(file.isDeclarationFile, file.fileName);
} else if (file.fileName !== probe) {
continue;
}
const errors = program.getSemanticDiagnostics(file);
assert.equal(
errors.length,
0,
errors
.map((error) => String(ts.flattenDiagnosticMessageText(error.messageText, '\n')))
.join('\n'),
);
}
}
await run(
process.execPath,
[join(fixture, 'node_modules/typescript/bin/tsc'), '--noEmit'],
[
join(fixture, 'node_modules/typescript/bin/tsc'),
'--noEmit',
...(framework === 'svelte' ? ['--customConditions', 'svelte'] : []),
],
fixture,
);
if (useVitePlus) {
Expand Down
4 changes: 3 additions & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"type": "module",
"exports": {
".": {
"types": "./dist/runtime/index.d.ts",
"svelte": "./src/runtime/index.ts",
"default": "./src/runtime/index.ts"
},
Expand All @@ -36,7 +37,7 @@
},
"scripts": {
"build": "vp pack",
"dev": "vp pack --watch",
"dev": "vp pack --watch src/runtime",
"typecheck": "tsrs --noEmit --rootDir ../.."
},
"dependencies": {
Expand All @@ -46,6 +47,7 @@
"@types/node": "catalog:",
"pathslash": "catalog:",
"svelte": "catalog:",
"svelte2tsx": "catalog:",
"vite": "catalog:",
"vite-plus": "catalog:",
"void": "workspace:*"
Expand Down
5 changes: 5 additions & 0 deletions packages/svelte/src/tsconfig.runtime.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../tsconfig.json",
"include": [],
"files": ["runtime/index.ts"]
}
32 changes: 32 additions & 0 deletions packages/svelte/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { access, glob, mkdtempDisposable, readFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
import { join, toSlash } from 'pathslash';
import { emitDts } from 'svelte2tsx';
import { defineConfig } from 'vite-plus';

export default defineConfig({
Expand All @@ -6,6 +11,33 @@ export default defineConfig({
format: 'esm',
dts: true,
clean: true,
plugins: [
{
name: 'void-svelte:runtime-types',
async generateBundle(_options, _bundle, isWrite) {
if (!isWrite) {
return;
}
await using temporary = await mkdtempDisposable(join(tmpdir(), 'void-svelte-dts-'));
await emitDts({
declarationDir: temporary.path,
libRoot: join(import.meta.dirname, 'src'),
svelteShimsPath: fileURLToPath(import.meta.resolve('svelte2tsx/svelte-shims-v4.d.ts')),
tsconfig: 'tsconfig.runtime.json',
});
await access(join(temporary.path, 'runtime/index.d.ts'));
for await (const file of glob('**/*.d.ts', { cwd: temporary.path })) {
const source = await readFile(join(temporary.path, file), 'utf8');
this.emitFile({
type: 'asset',
fileName: toSlash(file),
// Node16 resolution needs a JS specifier to find the corresponding .svelte.d.ts.
source: source.replaceAll(/\bfrom (['"])(\.\/[^'"]+\.svelte)\1/g, 'from $1$2.js$1'),
});
}
},
},
],
deps: {
resolveDepSubpath: true,
neverBundle: ['svelte', 'svelte/*', 'vite', 'void', 'void/*', '@sveltejs/vite-plugin-svelte'],
Expand Down
27 changes: 26 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ catalog:
tsrs: 7.1.0-beta.tsrs4
'@voidzero-dev/vitepress-theme': ^5.0.7
pathslash: ^0.1.0
svelte2tsx: ^0.7.63
vite: npm:@voidzero-dev/vite-plus-core@8.3.8001
vite-plus: 1.8.0
wrangler: ^4.168.0
Expand Down Expand Up @@ -144,6 +145,10 @@ packageExtensions:
'@analogjs/vite-plugin-angular@*':
peerDependencies:
typescript: '*'
# Keep the declaration emitter's TS 6 separate from Vite's workspace peer.
svelte2tsx:
dependencies:
typescript: '*'

overrides:
# Keep Vite+'s pinned Vitest internals on the same patch as the runner.
Expand All @@ -154,6 +159,8 @@ overrides:
'@vitest/utils': 5.1.1
esbuild: ^0.28.2
react-server-dom-webpack: 19.3.1
'svelte2tsx>typescript:peer': '-'
'svelte2tsx>typescript:prod': 'catalog:sveltekit'
vite@*: 'catalog:'

peerDependencyRules:
Expand Down