Svelte

Addfox 支持使用 Svelte 开发扩展,提供编译时优化的轻量体验。

安装

创建项目时选择 Svelte 模板:

pnpm create addfox-app --framework svelte

或在现有项目中安装:

pnpm
npm
yarn
bun
pnpm add @rsbuild/plugin-svelte

配置

// addfox.config.ts
import { defineConfig } from "addfox";
import { pluginSvelte } from "@rsbuild/plugin-svelte";

export default defineConfig({
  plugins: [pluginSvelte()],
});

项目结构

app/
├── background/
│   └── index.ts
├── content/
│   └── index.ts
├── popup/
│   ├── App.svelte
│   ├── index.html
│   └── index.ts
└── manifest.json

示例代码

<!-- app/popup/App.svelte -->
<script lang="ts">
  let count = 0;
  
  function increment() {
    count += 1;
  }
</script>

<div class="p-4">
  <h1 class="text-xl font-bold">Hello Svelte!</h1>
  <button 
    on:click={increment}
    class="mt-2 px-4 py-2 bg-blue-500 text-white rounded"
  >
    Count: {count}
  </button>
</div>

<style>
  h1 {
    color: #333;
  }
</style>
// app/popup/index.ts
import App from './App.svelte';
import './index.css';

new App({
  target: document.getElementById('app')!,
});

Store

使用 Svelte Store 进行状态管理:

// app/stores/count.ts
import { writable } from 'svelte/store';

export const count = writable(0);

export function increment() {
  count.update(n => n + 1);
}
<script>
  import { count, increment } from '../stores/count';
</script>

<button on:click={increment}>
  Count: {$count}
</button>

相关链接