Skip to content

Commit dfcf7ed

Browse files
committed
feat: add rslib svelte example
1 parent 6784060 commit dfcf7ed

7 files changed

Lines changed: 227 additions & 0 deletions

File tree

pnpm-lock.yaml

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rslib/svelte/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "rslib-svelte-ts",
3+
"version": "0.0.0",
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"types": "./dist/index.d.ts",
8+
"import": "./dist/index.js"
9+
},
10+
"./style.css": "./dist/index.css"
11+
},
12+
"types": "./dist/index.d.ts",
13+
"files": [
14+
"dist"
15+
],
16+
"scripts": {
17+
"build": "rslib",
18+
"check": "svelte-check",
19+
"dev": "rslib --watch"
20+
},
21+
"devDependencies": {
22+
"@rsbuild/plugin-svelte": "^1.1.1",
23+
"@rslib/core": "^0.21.5",
24+
"@tsconfig/svelte": "^5.0.8",
25+
"@types/node": "^24.12.4",
26+
"svelte": "^5.55.9",
27+
"svelte-check": "^4.4.8",
28+
"svelte2tsx": "^0.7.55",
29+
"typescript": "^5.9.3"
30+
},
31+
"peerDependencies": {
32+
"svelte": "^5.0.0"
33+
}
34+
}

rslib/svelte/rslib.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { pluginSvelte } from '@rsbuild/plugin-svelte';
2+
import { defineConfig } from '@rslib/core';
3+
import { svelteDtsPlugin } from './scripts/rslib-plugin-svelte-dts';
4+
5+
export default defineConfig({
6+
lib: [
7+
{
8+
format: 'esm',
9+
},
10+
],
11+
output: {
12+
target: 'web',
13+
},
14+
plugins: [pluginSvelte(), svelteDtsPlugin()],
15+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { RsbuildPlugin } from '@rslib/core';
2+
import { resolve } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
import { emitDts, type EmitDtsConfig } from 'svelte2tsx';
5+
6+
type SvelteDtsPluginOptions = Partial<EmitDtsConfig>;
7+
8+
const resolveProjectPath = (rootPath: string, path: string): string => resolve(rootPath, path);
9+
10+
export function svelteDtsPlugin(options: SvelteDtsPluginOptions = {}): RsbuildPlugin {
11+
return {
12+
name: 'rslib-plugin-svelte-dts',
13+
setup(api) {
14+
api.onAfterBuild(async ({ isWatch }) => {
15+
if (!isWatch) {
16+
api.logger.start('generating declaration files...');
17+
}
18+
19+
const { declarationDir = './dist', libRoot = './src', tsconfig } = options;
20+
const rootPath = api.context.rootPath;
21+
const tsconfigPath = resolveProjectPath(
22+
rootPath,
23+
tsconfig ?? api.getNormalizedConfig().source.tsconfigPath ?? './tsconfig.json',
24+
);
25+
const svelteShimsPath = options.svelteShimsPath
26+
? resolveProjectPath(rootPath, options.svelteShimsPath)
27+
: fileURLToPath(import.meta.resolve('svelte2tsx/svelte-shims-v4.d.ts'));
28+
29+
try {
30+
await emitDts({
31+
declarationDir: resolveProjectPath(rootPath, declarationDir),
32+
svelteShimsPath,
33+
libRoot: resolveProjectPath(rootPath, libRoot),
34+
tsconfig: tsconfigPath,
35+
});
36+
37+
api.logger.ready(`declaration files generated with svelte2tsx.`);
38+
} catch (error) {
39+
api.logger.error('Failed to generate declaration files with svelte2tsx.');
40+
api.logger.error(error);
41+
throw error;
42+
}
43+
});
44+
},
45+
};
46+
}

rslib/svelte/src/Button.svelte

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<script lang="ts">
2+
type Size = 'small' | 'medium' | 'large'
3+
4+
interface Props {
5+
primary?: boolean
6+
backgroundColor?: string
7+
size?: Size
8+
label?: string
9+
onClick?: () => void
10+
}
11+
12+
let {
13+
primary = false,
14+
backgroundColor,
15+
size = 'medium',
16+
label = 'Demo Button',
17+
onClick,
18+
}: Props = $props()
19+
20+
const mode = $derived(
21+
primary ? 'demo-button--primary' : 'demo-button--secondary',
22+
)
23+
</script>
24+
25+
<button
26+
type="button"
27+
class={['demo-button', `demo-button--${size}`, mode].join(' ')}
28+
style:background-color={backgroundColor}
29+
onclick={onClick}
30+
>
31+
{label}
32+
</button>
33+
34+
<style>
35+
.demo-button {
36+
font-weight: 700;
37+
border: 0;
38+
border-radius: 3em;
39+
cursor: pointer;
40+
display: inline-block;
41+
line-height: 1;
42+
}
43+
44+
.demo-button--primary {
45+
color: white;
46+
background-color: #1ea7fd;
47+
}
48+
49+
.demo-button--secondary {
50+
color: #333;
51+
background-color: transparent;
52+
box-shadow: rgba(0, 0, 0, 0.15) 0px 0px 0px 1px inset;
53+
}
54+
55+
.demo-button--small {
56+
font-size: 12px;
57+
padding: 10px 16px;
58+
}
59+
60+
.demo-button--medium {
61+
font-size: 14px;
62+
padding: 11px 20px;
63+
}
64+
65+
.demo-button--large {
66+
font-size: 16px;
67+
padding: 12px 24px;
68+
}
69+
</style>

rslib/svelte/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as Button } from './Button.svelte';

rslib/svelte/tsconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "@tsconfig/svelte/tsconfig.json",
3+
"compilerOptions": {
4+
"target": "ES2022",
5+
"lib": ["DOM", "ES2022"],
6+
"module": "preserve",
7+
"moduleDetection": "force",
8+
"moduleResolution": "bundler",
9+
"resolveJsonModule": true,
10+
"types": ["node"],
11+
"declaration": true,
12+
"emitDeclarationOnly": true,
13+
"isolatedModules": true,
14+
"noUnusedLocals": true
15+
},
16+
"include": ["src", "scripts", "*.config.ts"]
17+
}

0 commit comments

Comments
 (0)