@amaster.ai/vite-plugins 1.0.0-beta.4 → 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,13 +20,247 @@ 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
+
86
+ /**
87
+ * Taro Environment Variables Injection Helper
88
+ *
89
+ * Automatically injects environment variables into Taro's defineConstants
90
+ * so that they will be replaced at build time.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * // In Taro config/index.ts
95
+ * import { injectTaroEnv } from '@amaster.ai/vite-plugins/taro-env-inject'
96
+ *
97
+ * export default defineConfig({
98
+ * defineConstants: {
99
+ * ...injectTaroEnv()
100
+ * }
101
+ * })
102
+ * ```
103
+ */
104
+ interface TaroEnvInjectOptions {
105
+ /**
106
+ * Additional environment variable names to inject
107
+ * @default []
108
+ */
109
+ additional?: string[];
110
+ /**
111
+ * Whether to inject all TARO_APP_* prefixed variables
112
+ * @default true
113
+ */
114
+ autoInjectTaroApp?: boolean;
115
+ /**
116
+ * Whether to inject all VITE_* prefixed variables
117
+ * @default true
118
+ */
119
+ autoInjectVite?: boolean;
120
+ }
121
+ /**
122
+ * Inject environment variables into Taro's defineConstants
123
+ *
124
+ * This function reads environment variables and formats them for Taro's defineConstants.
125
+ * Taro will replace these at build time, converting `process.env.XXX` to actual values.
126
+ */
127
+ declare function injectTaroEnv(options?: TaroEnvInjectOptions): Record<string, string>;
128
+ /**
129
+ * Inject specific environment variables for amaster.ai mini-program builds
130
+ *
131
+ * This is a convenience function that injects the standard environment variables
132
+ * used by @amaster.ai/http-client for API base URL configuration.
133
+ */
134
+ declare function injectAmasterEnv(): Record<string, string>;
135
+
136
+ /**
137
+ * PostCSS 插件:H5 模式下将 rpx 预转换为 px
138
+ *
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(正确)
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * // In Taro config/index.ts
153
+ * import { rpx2pxPlugin } from '@amaster.ai/vite-plugins'
154
+ *
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
+ * }
166
+ * }
167
+ * }
168
+ * ```
169
+ */
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;
202
+
23
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;
24
232
  }
25
233
  /**
26
234
  * 开发工具插件集合,简化调用,vite.config.ts 直接 devTools() 即可
27
- * @param _options 预留参数,用于未来扩展配置(例如可选择性启用某些插件)
235
+ *
236
+ * @param options 配置项
237
+ * @param options.isTaro 是否为 Taro 项目(默认根据 TARO_ENV 自动判断)
238
+ * @param options.styleAdapter Taro H5 样式适配配置,designWidth 应与 Taro config 一致
28
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
+ * ```
29
263
  */
30
- declare function devTools(_options?: DevToolsOptions): Plugin[];
264
+ declare function devTools(options?: DevToolsOptions): Plugin[];
31
265
 
32
- export { type DevToolsOptions, componentIdPlugin, devTools as default, editorBridgePlugin, routesExposePlugin };
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,13 +20,247 @@ 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
+
86
+ /**
87
+ * Taro Environment Variables Injection Helper
88
+ *
89
+ * Automatically injects environment variables into Taro's defineConstants
90
+ * so that they will be replaced at build time.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * // In Taro config/index.ts
95
+ * import { injectTaroEnv } from '@amaster.ai/vite-plugins/taro-env-inject'
96
+ *
97
+ * export default defineConfig({
98
+ * defineConstants: {
99
+ * ...injectTaroEnv()
100
+ * }
101
+ * })
102
+ * ```
103
+ */
104
+ interface TaroEnvInjectOptions {
105
+ /**
106
+ * Additional environment variable names to inject
107
+ * @default []
108
+ */
109
+ additional?: string[];
110
+ /**
111
+ * Whether to inject all TARO_APP_* prefixed variables
112
+ * @default true
113
+ */
114
+ autoInjectTaroApp?: boolean;
115
+ /**
116
+ * Whether to inject all VITE_* prefixed variables
117
+ * @default true
118
+ */
119
+ autoInjectVite?: boolean;
120
+ }
121
+ /**
122
+ * Inject environment variables into Taro's defineConstants
123
+ *
124
+ * This function reads environment variables and formats them for Taro's defineConstants.
125
+ * Taro will replace these at build time, converting `process.env.XXX` to actual values.
126
+ */
127
+ declare function injectTaroEnv(options?: TaroEnvInjectOptions): Record<string, string>;
128
+ /**
129
+ * Inject specific environment variables for amaster.ai mini-program builds
130
+ *
131
+ * This is a convenience function that injects the standard environment variables
132
+ * used by @amaster.ai/http-client for API base URL configuration.
133
+ */
134
+ declare function injectAmasterEnv(): Record<string, string>;
135
+
136
+ /**
137
+ * PostCSS 插件:H5 模式下将 rpx 预转换为 px
138
+ *
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(正确)
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * // In Taro config/index.ts
153
+ * import { rpx2pxPlugin } from '@amaster.ai/vite-plugins'
154
+ *
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
+ * }
166
+ * }
167
+ * }
168
+ * ```
169
+ */
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;
202
+
23
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;
24
232
  }
25
233
  /**
26
234
  * 开发工具插件集合,简化调用,vite.config.ts 直接 devTools() 即可
27
- * @param _options 预留参数,用于未来扩展配置(例如可选择性启用某些插件)
235
+ *
236
+ * @param options 配置项
237
+ * @param options.isTaro 是否为 Taro 项目(默认根据 TARO_ENV 自动判断)
238
+ * @param options.styleAdapter Taro H5 样式适配配置,designWidth 应与 Taro config 一致
28
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
+ * ```
29
263
  */
30
- declare function devTools(_options?: DevToolsOptions): Plugin[];
264
+ declare function devTools(options?: DevToolsOptions): Plugin[];
31
265
 
32
- export { type DevToolsOptions, componentIdPlugin, devTools as default, editorBridgePlugin, routesExposePlugin };
266
+ export { type DevToolsOptions, type Rpx2pxPluginOptions, type TaroStyleAdapterOptions, componentIdPlugin, devTools as default, editorBridgePlugin, injectAmasterEnv, injectTaroEnv, jsxSourceTaggerPlugin, routesExposePlugin, rpx2pxPlugin, tailwindConfigSyncPlugin, taroStyleAdapterPlugin };