@amaster.ai/vite-plugins 1.0.0-beta.39 → 1.0.0-beta.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -8,7 +8,7 @@ declare function componentIdPlugin(): Plugin;
8
8
 
9
9
  /**
10
10
  * Vite plugin: Inject editor bridge script
11
- * Injects bridge.js in development mode by reading the built bridge script
11
+ * Injects minimal bridge that loads external script from platform
12
12
  */
13
13
  declare function editorBridgePlugin(): Plugin;
14
14
 
@@ -20,6 +20,69 @@ declare function routesExposePlugin(options?: {
20
20
  routesFilePath?: string;
21
21
  }): Plugin;
22
22
 
23
+ interface TaroStyleAdapterOptions {
24
+ /**
25
+ * Design width for responsive scaling (default: 375)
26
+ * Should match Taro config's designWidth
27
+ */
28
+ designWidth?: number;
29
+ /**
30
+ * Maximum viewport width to scale (default: 750)
31
+ * Beyond this width, font-size stays fixed
32
+ */
33
+ maxWidth?: number;
34
+ /**
35
+ * Base font size in pixels (default: 12)
36
+ * Should match Taro config's baseFontSize
37
+ */
38
+ baseFontSize?: number;
39
+ /**
40
+ * Minimum root font size in pixels (default: 12)
41
+ * Should match Taro config's minRootSize
42
+ */
43
+ minRootSize?: number;
44
+ /**
45
+ * Maximum root font size in pixels (default: 24)
46
+ * Should match Taro config's maxRootSize
47
+ */
48
+ maxRootSize?: number;
49
+ }
50
+ /**
51
+ * Taro H5 样式适配插件
52
+ *
53
+ * 功能:
54
+ * 1. 动态计算根字号,使 H5 的 rem 与小程序的 rpx 行为对齐
55
+ * 2. 隐藏 Taro 页面容器的滚动条,用于编辑器 iframe 预览
56
+ *
57
+ * 原理:
58
+ * - 小程序 rpx: 1rpx = 屏幕宽度 / 750
59
+ * - H5 rem: Taro 把 rpx 转成 rem,基于 baseFontSize
60
+ * - 本插件动态设置 html font-size,使 rem 的实际像素值与 rpx 对齐
61
+ *
62
+ * 注意:此插件会覆盖 Taro 默认注入的根字号脚本,配置需与 Taro pxtransform 保持一致
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * // In Taro config/dev.ts
67
+ * import { taroStyleAdapterPlugin } from '@amaster.ai/vite-plugins'
68
+ *
69
+ * export default {
70
+ * compiler: {
71
+ * type: 'vite',
72
+ * vitePlugins: [
73
+ * taroStyleAdapterPlugin({
74
+ * designWidth: 375,
75
+ * baseFontSize: 12,
76
+ * minRootSize: 12,
77
+ * maxRootSize: 24
78
+ * })
79
+ * ]
80
+ * }
81
+ * }
82
+ * ```
83
+ */
84
+ declare function taroStyleAdapterPlugin(options?: TaroStyleAdapterOptions): Plugin;
85
+
23
86
  /**
24
87
  * Taro Environment Variables Injection Helper
25
88
  *
@@ -71,35 +134,133 @@ declare function injectTaroEnv(options?: TaroEnvInjectOptions): Record<string, s
71
134
  declare function injectAmasterEnv(): Record<string, string>;
72
135
 
73
136
  /**
74
- * Taro H5 样式适配插件
137
+ * PostCSS 插件:H5 模式下将 rpx 预转换为 px
75
138
  *
76
- * 在开发环境下自动注入样式,用于隐藏 Taro 页面容器的滚动条,同时保持滚动功能。
77
- * 主要用于编辑器 iframe 预览时优化显示效果。
139
+ * 问题背景:
140
+ * - Taro 的 pxtransform 在 H5 模式下会处理 rpx,但它把 rpx 值直接除以 rootValue
141
+ * - 导致 96rpx 被转成 8rem(96/12),实际显示为 96px
142
+ * - 而正确应该是:96rpx = 48px = 4rem = 48px
143
+ *
144
+ * 解决方案:
145
+ * - 在 pxtransform 之前,先把 rpx 转成 px(rpx / 2)
146
+ * - 然后 pxtransform 会正确地把 px 转成 rem(px / 12)
147
+ *
148
+ * 转换链路:96rpx → 48px → 4rem → 48px(正确)
78
149
  *
79
150
  * @example
80
151
  * ```ts
81
- * // In Taro config/dev.ts
82
- * import { taroStyleAdapterPlugin } from '@amaster.ai/vite-plugins'
152
+ * // In Taro config/index.ts
153
+ * import { rpx2pxPlugin } from '@amaster.ai/vite-plugins'
83
154
  *
84
- * export default {
85
- * compiler: {
86
- * type: 'vite',
87
- * vitePlugins: [
88
- * taroStyleAdapterPlugin()
89
- * ]
155
+ * // postcss-config-loader-plugin 中使用
156
+ * {
157
+ * name: 'postcss-config-loader-plugin',
158
+ * config(config) {
159
+ * if (typeof config.css?.postcss === 'object') {
160
+ * config.css?.postcss.plugins?.unshift(tailwindcss())
161
+ * // H5 模式下添加 rpx2px 插件
162
+ * if (process.env.TARO_ENV === 'h5') {
163
+ * config.css?.postcss.plugins?.unshift(rpx2pxPlugin())
164
+ * }
165
+ * }
90
166
  * }
91
167
  * }
92
168
  * ```
93
169
  */
94
- declare function taroStyleAdapterPlugin(): Plugin;
170
+ interface Rpx2pxPluginOptions {
171
+ /**
172
+ * rpx 到 px 的转换比例(默认 2)
173
+ * 即 1rpx = 1/ratio px
174
+ * 默认值 2 表示 2rpx = 1px(基于 375 设计稿)
175
+ */
176
+ ratio?: number;
177
+ }
178
+ interface PostCSSDeclaration {
179
+ value: string;
180
+ }
181
+ interface PostCSSPlugin {
182
+ postcssPlugin: string;
183
+ Declaration: (decl: PostCSSDeclaration) => void;
184
+ }
185
+ declare function rpx2pxPlugin(options?: Rpx2pxPluginOptions): PostCSSPlugin;
186
+
187
+ /**
188
+ * Vite plugin: JSX Source Tagger
189
+ * Intercepts react/jsx-dev-runtime to add source information to DOM elements
190
+ * Only enabled in development mode
191
+ */
192
+ declare function jsxSourceTaggerPlugin(): Plugin;
193
+
194
+ /**
195
+ * Vite plugin: Tailwind Config Sync
196
+ * Generates a JSON file from tailwind.config.ts for runtime access
197
+ * Only enabled in development mode
198
+ */
199
+ declare function tailwindConfigSyncPlugin(options?: {
200
+ configPath?: string;
201
+ }): Plugin;
95
202
 
96
203
  interface DevToolsOptions {
204
+ /**
205
+ * 是否为 Taro 项目
206
+ * - true: 启用 Taro 相关插件(taroStyleAdapterPlugin、rpx2pxPlugin)
207
+ * - false: 禁用 Taro 相关插件
208
+ * - undefined: 自动检测(通过 process.env.TARO_ENV 判断,默认为 false)
209
+ */
210
+ isTaro?: boolean;
211
+ /**
212
+ * Taro style adapter options
213
+ * Controls H5 responsive scaling to match mini-program rpx behavior
214
+ * 仅在 isTaro 为 true 时生效
215
+ */
216
+ styleAdapter?: TaroStyleAdapterOptions;
217
+ /**
218
+ * 是否启用 JSX Source Tagger(通过 Symbol 标记元素源码位置)
219
+ * 默认:开发模式启用
220
+ */
221
+ jsxSourceTagger?: boolean;
222
+ /**
223
+ * 是否启用 Tailwind Config 同步(生成 JSON 文件)
224
+ * 默认:开发模式启用
225
+ */
226
+ tailwindConfigSync?: boolean;
227
+ /**
228
+ * Tailwind 配置文件路径
229
+ * 默认:./tailwind.config.ts
230
+ */
231
+ tailwindConfigPath?: string;
97
232
  }
98
233
  /**
99
234
  * 开发工具插件集合,简化调用,vite.config.ts 直接 devTools() 即可
100
- * @param _options 预留参数,用于未来扩展配置(例如可选择性启用某些插件)
235
+ *
236
+ * @param options 配置项
237
+ * @param options.isTaro 是否为 Taro 项目(默认根据 TARO_ENV 自动判断)
238
+ * @param options.styleAdapter Taro H5 样式适配配置,designWidth 应与 Taro config 一致
101
239
  * @returns Plugin[] Vite 插件数组
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * // Taro 项目 - config/dev.ts
244
+ * import devTools from "@amaster.ai/vite-plugins";
245
+ *
246
+ * export default {
247
+ * compiler: {
248
+ * type: 'vite',
249
+ * vitePlugins: [devTools({ isTaro: true })]
250
+ * }
251
+ * }
252
+ * ```
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * // React 项目 - vite.config.ts
257
+ * import devTools from "@amaster.ai/vite-plugins";
258
+ *
259
+ * export default {
260
+ * plugins: [devTools()] // isTaro 默认为 false
261
+ * }
262
+ * ```
102
263
  */
103
- declare function devTools(_options?: DevToolsOptions): Plugin[];
264
+ declare function devTools(options?: DevToolsOptions): Plugin[];
104
265
 
105
- export { type DevToolsOptions, componentIdPlugin, devTools as default, editorBridgePlugin, injectAmasterEnv, injectTaroEnv, routesExposePlugin, taroStyleAdapterPlugin };
266
+ export { type DevToolsOptions, type Rpx2pxPluginOptions, type TaroStyleAdapterOptions, componentIdPlugin, devTools as default, editorBridgePlugin, injectAmasterEnv, injectTaroEnv, jsxSourceTaggerPlugin, routesExposePlugin, rpx2pxPlugin, tailwindConfigSyncPlugin, taroStyleAdapterPlugin };
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ declare function componentIdPlugin(): Plugin;
8
8
 
9
9
  /**
10
10
  * Vite plugin: Inject editor bridge script
11
- * Injects bridge.js in development mode by reading the built bridge script
11
+ * Injects minimal bridge that loads external script from platform
12
12
  */
13
13
  declare function editorBridgePlugin(): Plugin;
14
14
 
@@ -20,6 +20,69 @@ declare function routesExposePlugin(options?: {
20
20
  routesFilePath?: string;
21
21
  }): Plugin;
22
22
 
23
+ interface TaroStyleAdapterOptions {
24
+ /**
25
+ * Design width for responsive scaling (default: 375)
26
+ * Should match Taro config's designWidth
27
+ */
28
+ designWidth?: number;
29
+ /**
30
+ * Maximum viewport width to scale (default: 750)
31
+ * Beyond this width, font-size stays fixed
32
+ */
33
+ maxWidth?: number;
34
+ /**
35
+ * Base font size in pixels (default: 12)
36
+ * Should match Taro config's baseFontSize
37
+ */
38
+ baseFontSize?: number;
39
+ /**
40
+ * Minimum root font size in pixels (default: 12)
41
+ * Should match Taro config's minRootSize
42
+ */
43
+ minRootSize?: number;
44
+ /**
45
+ * Maximum root font size in pixels (default: 24)
46
+ * Should match Taro config's maxRootSize
47
+ */
48
+ maxRootSize?: number;
49
+ }
50
+ /**
51
+ * Taro H5 样式适配插件
52
+ *
53
+ * 功能:
54
+ * 1. 动态计算根字号,使 H5 的 rem 与小程序的 rpx 行为对齐
55
+ * 2. 隐藏 Taro 页面容器的滚动条,用于编辑器 iframe 预览
56
+ *
57
+ * 原理:
58
+ * - 小程序 rpx: 1rpx = 屏幕宽度 / 750
59
+ * - H5 rem: Taro 把 rpx 转成 rem,基于 baseFontSize
60
+ * - 本插件动态设置 html font-size,使 rem 的实际像素值与 rpx 对齐
61
+ *
62
+ * 注意:此插件会覆盖 Taro 默认注入的根字号脚本,配置需与 Taro pxtransform 保持一致
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * // In Taro config/dev.ts
67
+ * import { taroStyleAdapterPlugin } from '@amaster.ai/vite-plugins'
68
+ *
69
+ * export default {
70
+ * compiler: {
71
+ * type: 'vite',
72
+ * vitePlugins: [
73
+ * taroStyleAdapterPlugin({
74
+ * designWidth: 375,
75
+ * baseFontSize: 12,
76
+ * minRootSize: 12,
77
+ * maxRootSize: 24
78
+ * })
79
+ * ]
80
+ * }
81
+ * }
82
+ * ```
83
+ */
84
+ declare function taroStyleAdapterPlugin(options?: TaroStyleAdapterOptions): Plugin;
85
+
23
86
  /**
24
87
  * Taro Environment Variables Injection Helper
25
88
  *
@@ -71,35 +134,133 @@ declare function injectTaroEnv(options?: TaroEnvInjectOptions): Record<string, s
71
134
  declare function injectAmasterEnv(): Record<string, string>;
72
135
 
73
136
  /**
74
- * Taro H5 样式适配插件
137
+ * PostCSS 插件:H5 模式下将 rpx 预转换为 px
75
138
  *
76
- * 在开发环境下自动注入样式,用于隐藏 Taro 页面容器的滚动条,同时保持滚动功能。
77
- * 主要用于编辑器 iframe 预览时优化显示效果。
139
+ * 问题背景:
140
+ * - Taro 的 pxtransform 在 H5 模式下会处理 rpx,但它把 rpx 值直接除以 rootValue
141
+ * - 导致 96rpx 被转成 8rem(96/12),实际显示为 96px
142
+ * - 而正确应该是:96rpx = 48px = 4rem = 48px
143
+ *
144
+ * 解决方案:
145
+ * - 在 pxtransform 之前,先把 rpx 转成 px(rpx / 2)
146
+ * - 然后 pxtransform 会正确地把 px 转成 rem(px / 12)
147
+ *
148
+ * 转换链路:96rpx → 48px → 4rem → 48px(正确)
78
149
  *
79
150
  * @example
80
151
  * ```ts
81
- * // In Taro config/dev.ts
82
- * import { taroStyleAdapterPlugin } from '@amaster.ai/vite-plugins'
152
+ * // In Taro config/index.ts
153
+ * import { rpx2pxPlugin } from '@amaster.ai/vite-plugins'
83
154
  *
84
- * export default {
85
- * compiler: {
86
- * type: 'vite',
87
- * vitePlugins: [
88
- * taroStyleAdapterPlugin()
89
- * ]
155
+ * // postcss-config-loader-plugin 中使用
156
+ * {
157
+ * name: 'postcss-config-loader-plugin',
158
+ * config(config) {
159
+ * if (typeof config.css?.postcss === 'object') {
160
+ * config.css?.postcss.plugins?.unshift(tailwindcss())
161
+ * // H5 模式下添加 rpx2px 插件
162
+ * if (process.env.TARO_ENV === 'h5') {
163
+ * config.css?.postcss.plugins?.unshift(rpx2pxPlugin())
164
+ * }
165
+ * }
90
166
  * }
91
167
  * }
92
168
  * ```
93
169
  */
94
- declare function taroStyleAdapterPlugin(): Plugin;
170
+ interface Rpx2pxPluginOptions {
171
+ /**
172
+ * rpx 到 px 的转换比例(默认 2)
173
+ * 即 1rpx = 1/ratio px
174
+ * 默认值 2 表示 2rpx = 1px(基于 375 设计稿)
175
+ */
176
+ ratio?: number;
177
+ }
178
+ interface PostCSSDeclaration {
179
+ value: string;
180
+ }
181
+ interface PostCSSPlugin {
182
+ postcssPlugin: string;
183
+ Declaration: (decl: PostCSSDeclaration) => void;
184
+ }
185
+ declare function rpx2pxPlugin(options?: Rpx2pxPluginOptions): PostCSSPlugin;
186
+
187
+ /**
188
+ * Vite plugin: JSX Source Tagger
189
+ * Intercepts react/jsx-dev-runtime to add source information to DOM elements
190
+ * Only enabled in development mode
191
+ */
192
+ declare function jsxSourceTaggerPlugin(): Plugin;
193
+
194
+ /**
195
+ * Vite plugin: Tailwind Config Sync
196
+ * Generates a JSON file from tailwind.config.ts for runtime access
197
+ * Only enabled in development mode
198
+ */
199
+ declare function tailwindConfigSyncPlugin(options?: {
200
+ configPath?: string;
201
+ }): Plugin;
95
202
 
96
203
  interface DevToolsOptions {
204
+ /**
205
+ * 是否为 Taro 项目
206
+ * - true: 启用 Taro 相关插件(taroStyleAdapterPlugin、rpx2pxPlugin)
207
+ * - false: 禁用 Taro 相关插件
208
+ * - undefined: 自动检测(通过 process.env.TARO_ENV 判断,默认为 false)
209
+ */
210
+ isTaro?: boolean;
211
+ /**
212
+ * Taro style adapter options
213
+ * Controls H5 responsive scaling to match mini-program rpx behavior
214
+ * 仅在 isTaro 为 true 时生效
215
+ */
216
+ styleAdapter?: TaroStyleAdapterOptions;
217
+ /**
218
+ * 是否启用 JSX Source Tagger(通过 Symbol 标记元素源码位置)
219
+ * 默认:开发模式启用
220
+ */
221
+ jsxSourceTagger?: boolean;
222
+ /**
223
+ * 是否启用 Tailwind Config 同步(生成 JSON 文件)
224
+ * 默认:开发模式启用
225
+ */
226
+ tailwindConfigSync?: boolean;
227
+ /**
228
+ * Tailwind 配置文件路径
229
+ * 默认:./tailwind.config.ts
230
+ */
231
+ tailwindConfigPath?: string;
97
232
  }
98
233
  /**
99
234
  * 开发工具插件集合,简化调用,vite.config.ts 直接 devTools() 即可
100
- * @param _options 预留参数,用于未来扩展配置(例如可选择性启用某些插件)
235
+ *
236
+ * @param options 配置项
237
+ * @param options.isTaro 是否为 Taro 项目(默认根据 TARO_ENV 自动判断)
238
+ * @param options.styleAdapter Taro H5 样式适配配置,designWidth 应与 Taro config 一致
101
239
  * @returns Plugin[] Vite 插件数组
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * // Taro 项目 - config/dev.ts
244
+ * import devTools from "@amaster.ai/vite-plugins";
245
+ *
246
+ * export default {
247
+ * compiler: {
248
+ * type: 'vite',
249
+ * vitePlugins: [devTools({ isTaro: true })]
250
+ * }
251
+ * }
252
+ * ```
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * // React 项目 - vite.config.ts
257
+ * import devTools from "@amaster.ai/vite-plugins";
258
+ *
259
+ * export default {
260
+ * plugins: [devTools()] // isTaro 默认为 false
261
+ * }
262
+ * ```
102
263
  */
103
- declare function devTools(_options?: DevToolsOptions): Plugin[];
264
+ declare function devTools(options?: DevToolsOptions): Plugin[];
104
265
 
105
- export { type DevToolsOptions, componentIdPlugin, devTools as default, editorBridgePlugin, injectAmasterEnv, injectTaroEnv, routesExposePlugin, taroStyleAdapterPlugin };
266
+ export { type DevToolsOptions, type Rpx2pxPluginOptions, type TaroStyleAdapterOptions, componentIdPlugin, devTools as default, editorBridgePlugin, injectAmasterEnv, injectTaroEnv, jsxSourceTaggerPlugin, routesExposePlugin, rpx2pxPlugin, tailwindConfigSyncPlugin, taroStyleAdapterPlugin };