File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -100,4 +100,5 @@ $ npm install -D typescript vite
100100# 当然,如果你使用 js 来编写则不需要这一步,直接仿照官方的 js 示例插件来编写就可以了
101101
102102# (2) 创建和编写 src/ 文件夹内的内容 (主要程序代码和样式文件)
103+ # 特殊:这里使用的特殊的方式将 css 文件转为字符串附加到 js 上,这样能方便你更好地利用代码着色功能编辑 js 文件
103104```
Original file line number Diff line number Diff line change 11{
2- "name" : " anymenu -plugin-simple" ,
2+ "name" : " any-menu -plugin-simple" ,
33 "version" : " 1.0.0" ,
44 "description" : " AnyMenu simple plugin example" ,
55 "keywords" : [
6- " anymenu"
6+ " any-menu" ,
7+ " anymenu" ,
8+ " plugin"
79 ],
810 "license" : " MIT" ,
911 "author" : " " ,
Original file line number Diff line number Diff line change 55 * Implements PluginInterface with TypeScript class syntax.
66 */
77
8- import './style.css' ;
8+ // 插件自定义样式
9+ //
10+ // 另一个做法不太推荐,但也说一下。
11+ // 使用库: import cssInjectedByJs from 'vite-plugin-css-injected-by-js';
12+ // 使用: 放到 defineConfig plugins 中
13+ // 功能: 可以将 CSS 内联到 JS 中,插件只需分发单个 .js 文件
14+ import cssText from './style.css?inline' ;
15+
916import type { PluginInterface , PluginInterfaceCtx } from '../types/any-menu' ;
1017
1118export default class HelloWorldPlugin implements PluginInterface {
@@ -17,6 +24,7 @@ export default class HelloWorldPlugin implements PluginInterface {
1724 author : 'your-name' ,
1825 description : 'A minimal AnyMenu plugin template that prints Hello World.' ,
1926 icon : 'lucide-hand-wave' ,
27+ css : cssText ,
2028 } ;
2129
2230 onLoad ( ) : void {
Original file line number Diff line number Diff line change @@ -26,6 +26,10 @@ export interface PluginMetadata {
2626 * - 不填则用名字自动生成
2727 */
2828 icon ?: string ;
29+ /** CSS 字符串,插件加载时自动注入到 <head>,卸载时自动移除
30+ * 若使用 TypeScript 模板仓库开发,build 工具会自动将 .css 文件内容填入此字段
31+ */
32+ css ?: string ;
2933}
3034
3135export interface PluginInterfaceCtx {
Original file line number Diff line number Diff line change 1+ // <reference types="vite/client" />
2+
3+ // Vite 原生支持
4+ // 但还是要让 TypeScript 认识 `?inline` 后缀的 CSS 导入,类型为 string
5+ declare module '*.css?inline' {
6+ const content : string ;
7+ export default content ;
8+ }
Original file line number Diff line number Diff line change 11import { defineConfig } from 'vite' ;
22
3- // 备用。目前弃用,转而使用新的 PluginInterface css 接口。简化对插件的开发,不需要插件开发者管理 css
4- // import cssInjectedByJs from 'vite-plugin-css-injected-by-js';
5- // 使用: 放到 defineConfig plugins 中
6- // 功能: 可以将 CSS 内联到 JS 中,插件只需分发单个 .js 文件
7-
83export default defineConfig ( {
94 build : {
105 lib : { // Library 模式:输出单个 ES 模块文件
@@ -15,6 +10,10 @@ export default defineConfig({
1510 rollupOptions : {
1611 external : [ ] ,
1712 } ,
13+ output : {
14+ inlineDynamicImports : true , // 确保产物是单文件,不拆分 chunk
15+ } ,
16+ cssCodeSplit : false , // 不单独输出 .css 文件,这里采用的 ?inline 字符串嵌入方案
1817 outDir : 'dist' ,
1918 minify : false ,
2019 } ,
You can’t perform that action at this time.
0 commit comments