@amaster.ai/vite-plugins 1.0.0-beta.7 → 1.0.0-beta.73
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/README.md +153 -0
- package/dist/bridge-module.global.js +14 -0
- package/dist/index.cjs +489 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +251 -10
- package/dist/index.d.ts +251 -10
- package/dist/index.js +489 -28
- package/dist/index.js.map +1 -1
- package/package.json +25 -5
- package/dist/bridge.bridge.js +0 -24
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Vite plugin: Add data-node-component-id to React components
|
|
5
|
-
* Only enabled in development mode
|
|
6
|
-
*/
|
|
7
|
-
declare function componentIdPlugin(): Plugin;
|
|
8
|
-
|
|
9
3
|
/**
|
|
10
4
|
* Vite plugin: Inject editor bridge script
|
|
11
|
-
* Injects bridge
|
|
5
|
+
* Injects minimal bridge that loads external script from platform
|
|
6
|
+
* Also provides virtual module for HMR and Tailwind config monitoring
|
|
12
7
|
*/
|
|
13
8
|
declare function editorBridgePlugin(): Plugin;
|
|
14
9
|
|
|
@@ -20,13 +15,259 @@ declare function routesExposePlugin(options?: {
|
|
|
20
15
|
routesFilePath?: string;
|
|
21
16
|
}): Plugin;
|
|
22
17
|
|
|
18
|
+
interface TaroStyleAdapterOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Design width for responsive scaling (default: 375)
|
|
21
|
+
* Should match Taro config's designWidth
|
|
22
|
+
*/
|
|
23
|
+
designWidth?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Maximum viewport width to scale (default: 750)
|
|
26
|
+
* Beyond this width, font-size stays fixed
|
|
27
|
+
*/
|
|
28
|
+
maxWidth?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Base font size in pixels (default: 12)
|
|
31
|
+
* Should match Taro config's baseFontSize
|
|
32
|
+
*/
|
|
33
|
+
baseFontSize?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Minimum root font size in pixels (default: 12)
|
|
36
|
+
* Should match Taro config's minRootSize
|
|
37
|
+
*/
|
|
38
|
+
minRootSize?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Maximum root font size in pixels (default: 24)
|
|
41
|
+
* Should match Taro config's maxRootSize
|
|
42
|
+
*/
|
|
43
|
+
maxRootSize?: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Taro H5 样式适配插件
|
|
47
|
+
*
|
|
48
|
+
* 功能:
|
|
49
|
+
* 1. 动态计算根字号,使 H5 的 rem 与小程序的 rpx 行为对齐
|
|
50
|
+
* 2. 隐藏 Taro 页面容器的滚动条,用于编辑器 iframe 预览
|
|
51
|
+
*
|
|
52
|
+
* 原理:
|
|
53
|
+
* - 小程序 rpx: 1rpx = 屏幕宽度 / 750
|
|
54
|
+
* - H5 rem: Taro 把 rpx 转成 rem,基于 baseFontSize
|
|
55
|
+
* - 本插件动态设置 html font-size,使 rem 的实际像素值与 rpx 对齐
|
|
56
|
+
*
|
|
57
|
+
* 注意:此插件会覆盖 Taro 默认注入的根字号脚本,配置需与 Taro pxtransform 保持一致
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* // In Taro config/dev.ts
|
|
62
|
+
* import { taroStyleAdapterPlugin } from '@amaster.ai/vite-plugins'
|
|
63
|
+
*
|
|
64
|
+
* export default {
|
|
65
|
+
* compiler: {
|
|
66
|
+
* type: 'vite',
|
|
67
|
+
* vitePlugins: [
|
|
68
|
+
* taroStyleAdapterPlugin({
|
|
69
|
+
* designWidth: 375,
|
|
70
|
+
* baseFontSize: 12,
|
|
71
|
+
* minRootSize: 12,
|
|
72
|
+
* maxRootSize: 24
|
|
73
|
+
* })
|
|
74
|
+
* ]
|
|
75
|
+
* }
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare function taroStyleAdapterPlugin(options?: TaroStyleAdapterOptions): Plugin;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Taro Environment Variables Injection Helper
|
|
83
|
+
*
|
|
84
|
+
* Automatically injects environment variables into Taro's defineConstants
|
|
85
|
+
* so that they will be replaced at build time.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* // In Taro config/index.ts
|
|
90
|
+
* import { injectTaroEnv } from '@amaster.ai/vite-plugins/taro-env-inject'
|
|
91
|
+
*
|
|
92
|
+
* export default defineConfig({
|
|
93
|
+
* defineConstants: {
|
|
94
|
+
* ...injectTaroEnv()
|
|
95
|
+
* }
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
interface TaroEnvInjectOptions {
|
|
100
|
+
/**
|
|
101
|
+
* Additional environment variable names to inject
|
|
102
|
+
* @default []
|
|
103
|
+
*/
|
|
104
|
+
additional?: string[];
|
|
105
|
+
/**
|
|
106
|
+
* Whether to inject all TARO_APP_* prefixed variables
|
|
107
|
+
* @default true
|
|
108
|
+
*/
|
|
109
|
+
autoInjectTaroApp?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* Whether to inject all VITE_* prefixed variables
|
|
112
|
+
* @default true
|
|
113
|
+
*/
|
|
114
|
+
autoInjectVite?: boolean;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Inject environment variables into Taro's defineConstants
|
|
118
|
+
*
|
|
119
|
+
* This function reads environment variables and formats them for Taro's defineConstants.
|
|
120
|
+
* Taro will replace these at build time, converting `process.env.XXX` to actual values.
|
|
121
|
+
*/
|
|
122
|
+
declare function injectTaroEnv(options?: TaroEnvInjectOptions): Record<string, string>;
|
|
123
|
+
/**
|
|
124
|
+
* Inject specific environment variables for amaster.ai mini-program builds
|
|
125
|
+
*
|
|
126
|
+
* This is a convenience function that injects the standard environment variables
|
|
127
|
+
* used by @amaster.ai/http-client for API base URL configuration.
|
|
128
|
+
*/
|
|
129
|
+
declare function injectAmasterEnv(): Record<string, string>;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* PostCSS 插件:H5 模式下将 rpx 预转换为 px
|
|
133
|
+
*
|
|
134
|
+
* 问题背景:
|
|
135
|
+
* - Taro 的 pxtransform 在 H5 模式下会处理 rpx,但它把 rpx 值直接除以 rootValue
|
|
136
|
+
* - 导致 96rpx 被转成 8rem(96/12),实际显示为 96px
|
|
137
|
+
* - 而正确应该是:96rpx = 48px = 4rem = 48px
|
|
138
|
+
*
|
|
139
|
+
* 解决方案:
|
|
140
|
+
* - 在 pxtransform 之前,先把 rpx 转成 px(rpx / 2)
|
|
141
|
+
* - 然后 pxtransform 会正确地把 px 转成 rem(px / 12)
|
|
142
|
+
*
|
|
143
|
+
* 转换链路:96rpx → 48px → 4rem → 48px(正确)
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* // In Taro config/index.ts
|
|
148
|
+
* import { rpx2pxPlugin } from '@amaster.ai/vite-plugins'
|
|
149
|
+
*
|
|
150
|
+
* // 在 postcss-config-loader-plugin 中使用
|
|
151
|
+
* {
|
|
152
|
+
* name: 'postcss-config-loader-plugin',
|
|
153
|
+
* config(config) {
|
|
154
|
+
* if (typeof config.css?.postcss === 'object') {
|
|
155
|
+
* config.css?.postcss.plugins?.unshift(tailwindcss())
|
|
156
|
+
* // H5 模式下添加 rpx2px 插件
|
|
157
|
+
* if (process.env.TARO_ENV === 'h5') {
|
|
158
|
+
* config.css?.postcss.plugins?.unshift(rpx2pxPlugin())
|
|
159
|
+
* }
|
|
160
|
+
* }
|
|
161
|
+
* }
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
interface Rpx2pxPluginOptions {
|
|
166
|
+
/**
|
|
167
|
+
* rpx 到 px 的转换比例(默认 2)
|
|
168
|
+
* 即 1rpx = 1/ratio px
|
|
169
|
+
* 默认值 2 表示 2rpx = 1px(基于 375 设计稿)
|
|
170
|
+
*/
|
|
171
|
+
ratio?: number;
|
|
172
|
+
}
|
|
173
|
+
interface PostCSSDeclaration {
|
|
174
|
+
value: string;
|
|
175
|
+
}
|
|
176
|
+
interface PostCSSPlugin {
|
|
177
|
+
postcssPlugin: string;
|
|
178
|
+
Declaration: (decl: PostCSSDeclaration) => void;
|
|
179
|
+
}
|
|
180
|
+
declare function rpx2pxPlugin(options?: Rpx2pxPluginOptions): PostCSSPlugin;
|
|
181
|
+
|
|
182
|
+
interface JsxInlineStyleRpxOptions {
|
|
183
|
+
/**
|
|
184
|
+
* rpx 到 px 的转换比例(默认 2)
|
|
185
|
+
* 即 2rpx = 1px(基于 375 设计稿)
|
|
186
|
+
*/
|
|
187
|
+
ratio?: number;
|
|
188
|
+
}
|
|
189
|
+
declare function transformJsxInlineStyleRpx(code: string, ratio?: number): {
|
|
190
|
+
changed: boolean;
|
|
191
|
+
code: string;
|
|
192
|
+
};
|
|
193
|
+
declare function jsxInlineStyleRpxPlugin(options?: JsxInlineStyleRpxOptions): Plugin;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Vite plugin: JSX Source Tagger
|
|
197
|
+
* Intercepts react/jsx-dev-runtime to add source information to DOM elements
|
|
198
|
+
* Only enabled in development mode
|
|
199
|
+
*/
|
|
200
|
+
declare function jsxSourceTaggerPlugin(): Plugin;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Vite plugin: Tailwind Config Sync
|
|
204
|
+
* Simplified version - just return raw config without resolveConfig
|
|
205
|
+
*/
|
|
206
|
+
declare function tailwindConfigSyncPlugin(options?: {
|
|
207
|
+
configPath?: string;
|
|
208
|
+
}): Plugin;
|
|
209
|
+
|
|
23
210
|
interface DevToolsOptions {
|
|
211
|
+
/**
|
|
212
|
+
* 是否为 Taro 项目
|
|
213
|
+
* - true: 启用 Taro 相关插件(taroStyleAdapterPlugin、rpx2pxPlugin)
|
|
214
|
+
* - false: 禁用 Taro 相关插件
|
|
215
|
+
* - undefined: 自动检测(通过 process.env.TARO_ENV 判断,默认为 false)
|
|
216
|
+
*/
|
|
217
|
+
isTaro?: boolean;
|
|
218
|
+
/**
|
|
219
|
+
* Taro style adapter options
|
|
220
|
+
* Controls H5 responsive scaling to match mini-program rpx behavior
|
|
221
|
+
* 仅在 isTaro 为 true 时生效
|
|
222
|
+
*/
|
|
223
|
+
styleAdapter?: TaroStyleAdapterOptions;
|
|
224
|
+
/**
|
|
225
|
+
* 是否启用 JSX Source Tagger(通过 Symbol 标记元素源码位置)
|
|
226
|
+
* 默认:开发模式启用
|
|
227
|
+
*/
|
|
228
|
+
jsxSourceTagger?: boolean;
|
|
229
|
+
/**
|
|
230
|
+
* 是否启用 Tailwind Config 同步(生成 JSON 文件)
|
|
231
|
+
* 默认:开发模式启用
|
|
232
|
+
*/
|
|
233
|
+
tailwindConfigSync?: boolean;
|
|
234
|
+
/**
|
|
235
|
+
* Tailwind 配置文件路径
|
|
236
|
+
* 默认:./tailwind.config.ts
|
|
237
|
+
*/
|
|
238
|
+
tailwindConfigPath?: string;
|
|
24
239
|
}
|
|
25
240
|
/**
|
|
26
241
|
* 开发工具插件集合,简化调用,vite.config.ts 直接 devTools() 即可
|
|
27
|
-
*
|
|
242
|
+
*
|
|
243
|
+
* @param options 配置项
|
|
244
|
+
* @param options.isTaro 是否为 Taro 项目(默认根据 TARO_ENV 自动判断)
|
|
245
|
+
* @param options.styleAdapter Taro H5 样式适配配置,designWidth 应与 Taro config 一致
|
|
28
246
|
* @returns Plugin[] Vite 插件数组
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* // Taro 项目 - config/dev.ts
|
|
251
|
+
* import devTools from "@amaster.ai/vite-plugins";
|
|
252
|
+
*
|
|
253
|
+
* export default {
|
|
254
|
+
* compiler: {
|
|
255
|
+
* type: 'vite',
|
|
256
|
+
* vitePlugins: [devTools({ isTaro: true })]
|
|
257
|
+
* }
|
|
258
|
+
* }
|
|
259
|
+
* ```
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```ts
|
|
263
|
+
* // React 项目 - vite.config.ts
|
|
264
|
+
* import devTools from "@amaster.ai/vite-plugins";
|
|
265
|
+
*
|
|
266
|
+
* export default {
|
|
267
|
+
* plugins: [devTools()] // isTaro 默认为 false
|
|
268
|
+
* }
|
|
269
|
+
* ```
|
|
29
270
|
*/
|
|
30
|
-
declare function devTools(
|
|
271
|
+
declare function devTools(options?: DevToolsOptions): Plugin[];
|
|
31
272
|
|
|
32
|
-
export { type DevToolsOptions,
|
|
273
|
+
export { type DevToolsOptions, type Rpx2pxPluginOptions, type TaroStyleAdapterOptions, devTools as default, editorBridgePlugin, injectAmasterEnv, injectTaroEnv, jsxInlineStyleRpxPlugin, jsxSourceTaggerPlugin, routesExposePlugin, rpx2pxPlugin, tailwindConfigSyncPlugin, taroStyleAdapterPlugin, transformJsxInlineStyleRpx };
|