From baa9c83cfad3354982eff4238b7e72c0a339b4e6 Mon Sep 17 00:00:00 2001 From: Elecmonkey Date: Tue, 26 May 2026 18:44:17 +0800 Subject: [PATCH] feat: add rslib svelte example --- pnpm-lock.yaml | 45 ++++++++++++ rslib/svelte/package.json | 34 +++++++++ rslib/svelte/rslib.config.ts | 15 ++++ .../svelte/scripts/rslib-plugin-svelte-dts.ts | 46 +++++++++++++ rslib/svelte/src/Button.svelte | 69 +++++++++++++++++++ rslib/svelte/src/index.ts | 1 + rslib/svelte/tsconfig.json | 17 +++++ 7 files changed, 227 insertions(+) create mode 100644 rslib/svelte/package.json create mode 100644 rslib/svelte/rslib.config.ts create mode 100644 rslib/svelte/scripts/rslib-plugin-svelte-dts.ts create mode 100644 rslib/svelte/src/Button.svelte create mode 100644 rslib/svelte/src/index.ts create mode 100644 rslib/svelte/tsconfig.json diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6868e88a..6b2552c2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1415,6 +1415,33 @@ importers: specifier: ^5.9.3 version: 5.9.3 + rslib/svelte: + devDependencies: + '@rsbuild/plugin-svelte': + specifier: ^1.1.1 + version: 1.1.1(@babel/core@7.29.0)(@rsbuild/core@2.0.7)(less@4.6.4)(postcss-load-config@6.0.1)(postcss@8.5.15)(pug@3.0.2)(sass@1.100.0)(stylus@0.64.0)(svelte@5.55.9)(typescript@5.9.3) + '@rslib/core': + specifier: ^0.21.5 + version: 0.21.5(@module-federation/runtime-tools@2.5.0)(core-js@3.49.0)(typescript@5.9.3) + '@tsconfig/svelte': + specifier: ^5.0.8 + version: 5.0.8 + '@types/node': + specifier: ^24.12.4 + version: 24.12.4 + svelte: + specifier: ^5.55.9 + version: 5.55.9(@typescript-eslint/types@8.60.0) + svelte-check: + specifier: ^4.4.8 + version: 4.4.8(picomatch@4.0.4)(svelte@5.55.9)(typescript@5.9.3) + svelte2tsx: + specifier: ^0.7.55 + version: 0.7.55(svelte@5.55.9)(typescript@5.9.3) + typescript: + specifier: ^5.9.3 + version: 5.9.3 + rslib/umd: devDependencies: '@rsbuild/plugin-react': @@ -9757,6 +9784,9 @@ packages: resolution: {integrity: sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==} engines: {node: '>=10'} + dedent-js@1.0.1: + resolution: {integrity: sha512-OUepMozQULMLUmhxS95Vudo0jb0UchLimi3+pQ2plj61Fcy8axbP9hbiD4Sz6DPqn6XG3kfmziVfQ1rSys5AJQ==} + dedent@0.7.0: resolution: {integrity: sha512-Q6fKUPqnAHAyhiUgFU7BUzLiv0kd8saH9al7tnu5Q/okj6dnupxyTgFIBjVzJATdfIAm9NAsvXNzjaKa+bxVyA==} @@ -14425,6 +14455,12 @@ packages: typescript: optional: true + svelte2tsx@0.7.55: + resolution: {integrity: sha512-JWzgeM3lqySRNfqcsesvVEh8LhTWBxQJ9RMjzJ+VepdmXtVnNd0SbtGctG6+/fbHq0N6mhwSd823gszw9JHeGQ==} + peerDependencies: + svelte: ^3.55 || ^4.0.0-next.0 || ^4.0 || ^5.0.0-next.0 + typescript: ^4.9.4 || ^5.0.0 || ^6.0.0 + svelte@5.55.9: resolution: {integrity: sha512-fTjjT8cHLDwigcu2j3pv7Jq04LklXevPB8uBgyHNiTXv+RMNvVnrjS4UEYrLMkhuq1vpCodHjiW+z/95SDs/fg==} engines: {node: '>=18'} @@ -21994,6 +22030,8 @@ snapshots: mimic-response: 3.1.0 optional: true + dedent-js@1.0.1: {} + dedent@0.7.0: {} dedent@1.7.0(babel-plugin-macros@3.1.0): @@ -27903,6 +27941,13 @@ snapshots: stylus: 0.64.0 typescript: 5.9.3 + svelte2tsx@0.7.55(svelte@5.55.9)(typescript@5.9.3): + dependencies: + dedent-js: 1.0.1 + scule: 1.3.0 + svelte: 5.55.9(@typescript-eslint/types@8.60.0) + typescript: 5.9.3 + svelte@5.55.9(@typescript-eslint/types@8.60.0): dependencies: '@jridgewell/remapping': 2.3.5 diff --git a/rslib/svelte/package.json b/rslib/svelte/package.json new file mode 100644 index 00000000..e4635df4 --- /dev/null +++ b/rslib/svelte/package.json @@ -0,0 +1,34 @@ +{ + "name": "rslib-svelte-ts", + "version": "0.0.0", + "type": "module", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js" + }, + "./style.css": "./dist/index.css" + }, + "types": "./dist/index.d.ts", + "files": [ + "dist" + ], + "scripts": { + "build": "rslib", + "check": "svelte-check", + "dev": "rslib --watch" + }, + "devDependencies": { + "@rsbuild/plugin-svelte": "^1.1.1", + "@rslib/core": "^0.21.5", + "@tsconfig/svelte": "^5.0.8", + "@types/node": "^24.12.4", + "svelte": "^5.55.9", + "svelte-check": "^4.4.8", + "svelte2tsx": "^0.7.55", + "typescript": "^5.9.3" + }, + "peerDependencies": { + "svelte": "^5.0.0" + } +} diff --git a/rslib/svelte/rslib.config.ts b/rslib/svelte/rslib.config.ts new file mode 100644 index 00000000..d0ca3927 --- /dev/null +++ b/rslib/svelte/rslib.config.ts @@ -0,0 +1,15 @@ +import { pluginSvelte } from '@rsbuild/plugin-svelte'; +import { defineConfig } from '@rslib/core'; +import { svelteDtsPlugin } from './scripts/rslib-plugin-svelte-dts'; + +export default defineConfig({ + lib: [ + { + format: 'esm', + }, + ], + output: { + target: 'web', + }, + plugins: [pluginSvelte(), svelteDtsPlugin()], +}); diff --git a/rslib/svelte/scripts/rslib-plugin-svelte-dts.ts b/rslib/svelte/scripts/rslib-plugin-svelte-dts.ts new file mode 100644 index 00000000..4565a0fd --- /dev/null +++ b/rslib/svelte/scripts/rslib-plugin-svelte-dts.ts @@ -0,0 +1,46 @@ +import type { RsbuildPlugin } from '@rslib/core'; +import { resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { emitDts, type EmitDtsConfig } from 'svelte2tsx'; + +type SvelteDtsPluginOptions = Partial; + +const resolveProjectPath = (rootPath: string, path: string): string => resolve(rootPath, path); + +export function svelteDtsPlugin(options: SvelteDtsPluginOptions = {}): RsbuildPlugin { + return { + name: 'rslib-plugin-svelte-dts', + setup(api) { + api.onAfterBuild(async ({ isWatch }) => { + if (!isWatch) { + api.logger.start('generating declaration files...'); + } + + const { declarationDir = './dist', libRoot = './src', tsconfig } = options; + const rootPath = api.context.rootPath; + const tsconfigPath = resolveProjectPath( + rootPath, + tsconfig ?? api.getNormalizedConfig().source.tsconfigPath ?? './tsconfig.json', + ); + const svelteShimsPath = options.svelteShimsPath + ? resolveProjectPath(rootPath, options.svelteShimsPath) + : fileURLToPath(import.meta.resolve('svelte2tsx/svelte-shims-v4.d.ts')); + + try { + await emitDts({ + declarationDir: resolveProjectPath(rootPath, declarationDir), + svelteShimsPath, + libRoot: resolveProjectPath(rootPath, libRoot), + tsconfig: tsconfigPath, + }); + + api.logger.ready(`declaration files generated with svelte2tsx.`); + } catch (error) { + api.logger.error('Failed to generate declaration files with svelte2tsx.'); + api.logger.error(error); + throw error; + } + }); + }, + }; +} diff --git a/rslib/svelte/src/Button.svelte b/rslib/svelte/src/Button.svelte new file mode 100644 index 00000000..583367dd --- /dev/null +++ b/rslib/svelte/src/Button.svelte @@ -0,0 +1,69 @@ + + + + + diff --git a/rslib/svelte/src/index.ts b/rslib/svelte/src/index.ts new file mode 100644 index 00000000..ae34e39c --- /dev/null +++ b/rslib/svelte/src/index.ts @@ -0,0 +1 @@ +export { default as Button } from './Button.svelte'; diff --git a/rslib/svelte/tsconfig.json b/rslib/svelte/tsconfig.json new file mode 100644 index 00000000..f94e1e7a --- /dev/null +++ b/rslib/svelte/tsconfig.json @@ -0,0 +1,17 @@ +{ + "extends": "@tsconfig/svelte/tsconfig.json", + "compilerOptions": { + "target": "ES2022", + "lib": ["DOM", "ES2022"], + "module": "preserve", + "moduleDetection": "force", + "moduleResolution": "bundler", + "resolveJsonModule": true, + "types": ["node"], + "declaration": true, + "emitDeclarationOnly": true, + "isolatedModules": true, + "noUnusedLocals": true + }, + "include": ["src", "scripts", "*.config.ts"] +}