Skip to content

Commit dfedcd1

Browse files
committed
feat(rslib): add svelte examples
1 parent 1478e26 commit dfedcd1

19 files changed

Lines changed: 517 additions & 4 deletions

pnpm-lock.yaml

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

rslib/svelte-basic/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Local
2+
.DS_Store
3+
*.local
4+
*.log*
5+
6+
# Dist
7+
node_modules
8+
dist/
9+
storybook-static
10+
doc_build/
11+
12+
# IDE
13+
.vscode/*
14+
!.vscode/extensions.json
15+
.idea

rslib/svelte-basic/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Rslib project
2+
3+
## Setup
4+
5+
Install the dependencies:
6+
7+
```bash
8+
npm install
9+
```
10+
11+
## Get started
12+
13+
Build the library:
14+
15+
```bash
16+
npm run build
17+
```
18+
19+
Build the library in watch mode:
20+
21+
```bash
22+
npm run dev
23+
```

rslib/svelte-basic/package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@rslib-example/svelte-basic",
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 --tsconfig ./tsconfig.json",
19+
"dev": "rslib -w"
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.5",
27+
"svelte-check": "^4.4.8",
28+
"svelte-preprocess": "^6.0.3",
29+
"svelte2tsx": "^0.7.55",
30+
"typescript": "^5.9.3"
31+
},
32+
"peerDependencies": {
33+
"svelte": "^5.0.0"
34+
}
35+
}

rslib/svelte-basic/rslib.config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
resolve: {
7+
conditionNames: ['svelte', 'browser', '...'],
8+
},
9+
lib: [
10+
{
11+
format: 'esm',
12+
dts: false,
13+
},
14+
],
15+
output: {
16+
target: 'web',
17+
},
18+
plugins: [
19+
pluginSvelte(),
20+
svelteDtsPlugin({
21+
declarationDir: './dist',
22+
libRoot: './src',
23+
tsconfig: 'tsconfig.json',
24+
}),
25+
],
26+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { createRequire } from 'node:module';
2+
import { resolve } from 'node:path';
3+
import type { RsbuildPlugin } from '@rslib/core';
4+
import { emitDts } from 'svelte2tsx';
5+
6+
const require = createRequire(import.meta.url);
7+
8+
export interface SvelteDtsPluginOptions {
9+
declarationDir?: string;
10+
libRoot?: string;
11+
tsconfig?: string;
12+
svelteShimsPath?: string;
13+
}
14+
15+
const resolveProjectPath = (rootPath: string, path: string): string => resolve(rootPath, path);
16+
17+
export function svelteDtsPlugin(options: SvelteDtsPluginOptions = {}): RsbuildPlugin {
18+
return {
19+
name: 'rslib-plugin-svelte-dts',
20+
setup(api) {
21+
api.onAfterBuild(async () => {
22+
const {
23+
declarationDir = './dist',
24+
libRoot = './src',
25+
tsconfig = 'tsconfig.json',
26+
} = options;
27+
28+
const rootPath = api.context.rootPath;
29+
const svelteShimsPath = options.svelteShimsPath
30+
? resolveProjectPath(rootPath, options.svelteShimsPath)
31+
: require.resolve('svelte2tsx/svelte-shims-v4.d.ts');
32+
33+
try {
34+
await emitDts({
35+
declarationDir: resolveProjectPath(rootPath, declarationDir),
36+
svelteShimsPath,
37+
libRoot: resolveProjectPath(rootPath, libRoot),
38+
tsconfig: resolveProjectPath(rootPath, tsconfig),
39+
});
40+
41+
console.log('Svelte DTS generation complete');
42+
} catch (error) {
43+
console.error('Svelte DTS generation failed:', error);
44+
throw error;
45+
}
46+
});
47+
},
48+
};
49+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<script lang="ts">
2+
interface Props {
3+
label?: string
4+
onclick?: (count: number) => void
5+
}
6+
7+
let { label = 'Demo Button', onclick }: Props = $props()
8+
let count = $state(0)
9+
10+
function increment() {
11+
count++
12+
onclick?.(count)
13+
}
14+
</script>
15+
16+
<button class="demo-button" onclick={increment}>
17+
{label}: {count}
18+
</button>
19+
20+
<style>
21+
.demo-button {
22+
padding: 0.6em 1.2em;
23+
font-size: 1em;
24+
border: 1px solid #ff3e00;
25+
border-radius: 4px;
26+
background-color: #ff3e00;
27+
color: white;
28+
cursor: pointer;
29+
transition: all 0.2s;
30+
}
31+
32+
.demo-button:hover {
33+
background-color: #ff5722;
34+
border-color: #ff5722;
35+
}
36+
37+
.demo-button:active {
38+
transform: scale(0.98);
39+
}
40+
</style>

rslib/svelte-basic/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';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { sveltePreprocess } from 'svelte-preprocess';
2+
3+
export default {
4+
preprocess: sveltePreprocess(),
5+
};

rslib/svelte-basic/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
"strict": true,
12+
"declaration": true,
13+
"emitDeclarationOnly": true,
14+
"esModuleInterop": true,
15+
"isolatedModules": true,
16+
"verbatimModuleSyntax": true,
17+
"skipLibCheck": true,
18+
"noUnusedLocals": true
19+
},
20+
"include": ["src", "scripts", "*.config.ts"]
21+
}

0 commit comments

Comments
 (0)