vite-plugin-taro 0.0.3 → 0.0.5

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.
@@ -1,18 +1,19 @@
1
- import { createTaroConditionalDirectivePlugin } from './plugins.js';
1
+ import { createVitePluginTaroConditionalDirectivePlugin } from './plugins.js';
2
+ import { createTailwindcssPlugins } from './tailwindcss.js';
2
3
  import { createH5SupportPlugins, createH5ViteConfig, createWebIndexHtmlTags, isH5VirtualModuleId, loadH5VirtualModule } from './targets/h5.js';
3
4
  import { createWxViteConfig, emitWechatAssets, emitWechatImplicitChunksForVirtualApp, isWxVirtualModuleId, loadWxVirtualModule } from './targets/wx.js';
4
5
  import { stripVirtualPrefix, toImportPath } from './utils.js';
5
- import { isPublicVirtualModuleId, loadPublicVirtualModule } from './virtual.js';
6
6
  /**
7
7
  * Creates the Vite/Rolldown plugin that emits either WeChat Mini Program files
8
8
  * or a Taro Web app using the official Taro runtime packages.
9
9
  */
10
- export default function taro(options) {
11
- const context = createTaroBuildContext(options);
10
+ export default function vitePluginTaro(options) {
11
+ const context = createVitePluginTaroBuildContext(options);
12
12
  return [
13
- createTaroConditionalDirectivePlugin(context),
13
+ createVitePluginTaroConditionalDirectivePlugin(context),
14
14
  ...createTargetSupportPlugins(context),
15
- createTaroPlugin(context)
15
+ ...createTailwindcssPlugins(context),
16
+ createVitePluginTaroPlugin(context)
16
17
  ];
17
18
  }
18
19
  function createTargetSupportPlugins(context) {
@@ -26,7 +27,7 @@ function createTargetSupportPlugins(context) {
26
27
  /**
27
28
  * Creates the vite-plugin-taro plugin that emits H5 or Wx outputs.
28
29
  */
29
- function createTaroPlugin(context) {
30
+ function createVitePluginTaroPlugin(context) {
30
31
  return {
31
32
  name: 'vite-plugin-taro',
32
33
  enforce: 'post',
@@ -39,16 +40,14 @@ function createTaroPlugin(context) {
39
40
  },
40
41
  /** Marks generated app/page/component entries as virtual modules. */
41
42
  resolveId(id) {
42
- if (isPublicVirtualModuleId(id) || isWxVirtualModuleId(id) || isH5VirtualModuleId(id))
43
+ if (isWxVirtualModuleId(id) || isH5VirtualModuleId(id))
43
44
  return `\0${id}`;
44
45
  },
45
46
  /** Supplies source code for each virtual entry module. */
46
47
  load(id) {
47
48
  const cleanId = stripVirtualPrefix(id);
48
49
  emitWechatImplicitChunksForVirtualApp(this, context, cleanId);
49
- return (loadPublicVirtualModule(cleanId, context) ??
50
- loadWxVirtualModule(cleanId, context) ??
51
- loadH5VirtualModule(cleanId, context));
50
+ return loadWxVirtualModule(cleanId, context) ?? loadH5VirtualModule(cleanId, context);
52
51
  },
53
52
  /** Injects the generated Web entry into the app shell before Vite scans HTML imports. */
54
53
  transformIndexHtml: {
@@ -66,7 +65,7 @@ function createTaroPlugin(context) {
66
65
  /**
67
66
  * Normalizes user options into the shared data used by both target builders.
68
67
  */
69
- function createTaroBuildContext(options) {
68
+ function createVitePluginTaroBuildContext(options) {
70
69
  return {
71
70
  target: options.target,
72
71
  appComponentImport: toImportPath(options.app),
package/dist/vite.js CHANGED
@@ -1 +1 @@
1
- export { default } from './vite/taro.js';
1
+ export { default } from './vite/vite-plugin-taro.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-taro",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Vite 8 plugin for building one React/Taro codebase for WeChat Mini Program and H5 targets.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -16,29 +16,40 @@
16
16
  "module": "./dist/vite.js",
17
17
  "exports": {
18
18
  ".": {
19
+ "types": "./src/vite.ts",
19
20
  "import": "./dist/vite.js",
20
21
  "default": "./dist/vite.js"
21
22
  },
22
- "./vite": {
23
- "import": "./dist/vite.js",
24
- "default": "./dist/vite.js"
23
+ "./components": {
24
+ "types": "./src/public/components.ts",
25
+ "import": "./dist/public/components.js",
26
+ "default": "./dist/public/components.js"
25
27
  },
26
- "./client": {
27
- "types": "./client.d.ts"
28
+ "./taro": {
29
+ "types": "./src/public/taro.d.ts",
30
+ "import": "./dist/public/taro.js",
31
+ "default": "./dist/public/taro.js"
28
32
  },
29
33
  "./shim/h5": {
34
+ "types": "./src/shim/h5.ts",
30
35
  "import": "./dist/shim/h5.js",
31
36
  "default": "./dist/shim/h5.js"
32
37
  },
33
38
  "./shim/wx": {
39
+ "types": "./src/shim/wx.ts",
34
40
  "import": "./dist/shim/wx.js",
35
41
  "default": "./dist/shim/wx.js"
36
42
  },
43
+ "./vite": {
44
+ "types": "./src/vite.ts",
45
+ "import": "./dist/vite.js",
46
+ "default": "./dist/vite.js"
47
+ },
37
48
  "./package.json": "./package.json"
38
49
  },
39
50
  "files": [
40
51
  "dist",
41
- "client.d.ts",
52
+ "src",
42
53
  "LICENSE",
43
54
  "README.md"
44
55
  ],
@@ -59,7 +70,9 @@
59
70
  "node": "^20.19.0 || >=22.12.0"
60
71
  },
61
72
  "dependencies": {
73
+ "@babel/core": "^7.29.7",
62
74
  "@rolldown/plugin-babel": "^0.2.3",
75
+ "@tailwindcss/vite": "^4.3.1",
63
76
  "@tarojs/components": "^4.2.0",
64
77
  "@tarojs/helper": "^4.2.0",
65
78
  "@tarojs/plugin-platform-h5": "^4.2.0",
@@ -69,8 +82,10 @@
69
82
  "@tarojs/taro": "^4.2.0",
70
83
  "@vitejs/plugin-react": "^6.0.2",
71
84
  "babel-plugin-transform-taroapi": "^4.2.0",
72
- "@tarojs/react": "npm:vite-plugin-taro-react@4.2.0-react19.2",
73
- "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@4.2.0-react19.2"
85
+ "tailwindcss": "^4.3.1",
86
+ "weapp-tailwindcss": "^5.0.13",
87
+ "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@4.2.0-react19.4",
88
+ "@tarojs/react": "npm:vite-plugin-taro-react@4.2.0-react19.4"
74
89
  },
75
90
  "peerDependencies": {
76
91
  "react": "^19.0.0",
@@ -78,14 +93,13 @@
78
93
  "vite": "^8.0.0"
79
94
  },
80
95
  "devDependencies": {
96
+ "@types/babel__core": "^7.20.5",
81
97
  "@types/node": "^26.0.0",
82
98
  "@typescript/native-preview": "latest",
83
99
  "vite": "^8.0.16"
84
100
  },
85
101
  "scripts": {
86
- "clean": "rm -rf dist",
87
- "build": "pnpm -w prepare:taro && pnpm clean && tsgo --project tsconfig.json",
88
- "typecheck": "pnpm -w prepare:taro && tsgo --project tsconfig.json --noEmit",
89
- "pack:dry": "pnpm pack --dry-run"
102
+ "build": "rm -rf dist && tsgo --project tsconfig.json",
103
+ "typecheck": "tsgo --project tsconfig.json --noEmit"
90
104
  }
91
105
  }
@@ -0,0 +1 @@
1
+ export * from '@tarojs/components'
@@ -0,0 +1,3 @@
1
+ import Taro = require('@tarojs/taro')
2
+ export = Taro
3
+ export default Taro
@@ -0,0 +1,10 @@
1
+ import { hooks } from '@tarojs/runtime'
2
+ import Taro from '@tarojs/taro'
3
+
4
+ if (hooks.isExist('initNativeApi')) {
5
+ hooks.call('initNativeApi', Taro)
6
+ }
7
+
8
+ // @ts-expect-error @tarojs/taro declares export= types, but vite-plugin-taro target aliases expose runtime named exports.
9
+ export * from '@tarojs/taro'
10
+ export default Taro
package/src/shim/h5.ts ADDED
@@ -0,0 +1,6 @@
1
+ import '@tarojs/plugin-platform-h5/dist/runtime'
2
+
3
+ // @ts-expect-error Taro exposes createReactApp from this runtime-only deep entry without types.
4
+ export { createReactApp } from '@tarojs/plugin-framework-react/dist/runtime'
5
+ export { createBrowserHistory, createHashHistory, createRouter, handleAppMount } from '@tarojs/router'
6
+ export { window } from '@tarojs/runtime'
package/src/shim/wx.ts ADDED
@@ -0,0 +1,6 @@
1
+ import '@tarojs/plugin-platform-weapp/dist/runtime.js'
2
+
3
+ // @ts-expect-error Taro exposes createReactApp from this runtime-only deep entry without types.
4
+ export { createReactApp } from '@tarojs/plugin-framework-react/dist/runtime'
5
+ export { default as ReactDOM } from '@tarojs/react'
6
+ export { createPageConfig, createRecursiveComponentConfig } from '@tarojs/runtime'
@@ -0,0 +1,5 @@
1
+ import { createRequire } from 'node:module'
2
+
3
+ export const isProd = process.env.NODE_ENV === 'production'
4
+
5
+ export const nodeRequire = createRequire(import.meta.url)
@@ -0,0 +1,218 @@
1
+ import type { Plugin } from 'vite'
2
+ import type { VitePluginTaroBuildContext, VitePluginTaroTarget } from './types.ts'
3
+ import { normalizeModuleId } from './utils.ts'
4
+
5
+ /**
6
+ * Applies Taro-style conditional compilation comments before Vite parses source files.
7
+ *
8
+ * Mirrors Taro's CSS #ifdef/#ifndef handling, generalized before Vite parses code.
9
+ */
10
+ export function createVitePluginTaroConditionalDirectivePlugin(context: VitePluginTaroBuildContext): Plugin {
11
+ const target = context.target
12
+ return {
13
+ name: 'vite-plugin-taro-conditional-directives',
14
+ enforce: 'pre',
15
+ transform(code, id) {
16
+ if (!isConditionalDirectiveSource(id) || !code.includes('#if')) return
17
+ return { code: transformConditionalDirectives(code, target), map: null }
18
+ }
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Filters files where Taro's conditional comments are meaningful.
24
+ *
25
+ * Plugin-only: source filter for generalized conditional-directive transform.
26
+ */
27
+ function isConditionalDirectiveSource(id: string): boolean {
28
+ const normalizedId = normalizeModuleId(id)
29
+ if (normalizedId.includes('/node_modules/')) return false
30
+ return /\.(?:[cm]?[jt]sx?|css|s[ac]ss|less|styl)(?:\?|$)/.test(normalizedId)
31
+ }
32
+
33
+ type ConditionalDirectiveName = 'ifdef' | 'ifndef' | 'if' | 'elif' | 'else' | 'endif'
34
+
35
+ type ConditionalDirective = {
36
+ name: ConditionalDirectiveName
37
+ expression: string
38
+ }
39
+
40
+ type ConditionalDirectiveFrame = {
41
+ parentActive: boolean
42
+ active: boolean
43
+ matched: boolean
44
+ }
45
+
46
+ /**
47
+ * Removes inactive blocks guarded by Taro conditional comments.
48
+ *
49
+ * Mirrors Taro's CSS #ifdef/#ifndef handling.
50
+ */
51
+ function transformConditionalDirectives(code: string, target: VitePluginTaroTarget): string {
52
+ const lines = code.match(/[^\n]*(?:\n|$)/g) ?? []
53
+ const frames: ConditionalDirectiveFrame[] = []
54
+ let transformed = ''
55
+
56
+ for (const line of lines) {
57
+ if (!line) continue
58
+ const directive = parseConditionalDirective(line)
59
+ const lineEnding = getLineEnding(line)
60
+ if (directive) {
61
+ updateConditionalDirectiveFrames(frames, directive, target)
62
+ transformed += lineEnding
63
+ continue
64
+ }
65
+ transformed += isDirectiveStackActive(frames) ? line : lineEnding
66
+ }
67
+
68
+ return transformed
69
+ }
70
+
71
+ /**
72
+ * Parses one Taro conditional compilation directive from a comment-only line.
73
+ *
74
+ * Mirrors Taro's CSS comment-token handling.
75
+ */
76
+ function parseConditionalDirective(line: string): ConditionalDirective | undefined {
77
+ const match = line.match(/^\s*(?:(?:\/\/)|(?:\/\*))\s*#(ifdef|ifndef|if|elif|else|endif)\b([^*\r\n]*)/)
78
+ if (!match) return
79
+ const name = toConditionalDirectiveName(match[1])
80
+ if (!name) return
81
+ return {
82
+ name,
83
+ expression: match[2]?.replace(/\*\/$/, '').trim() ?? ''
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Converts a regex capture into a supported directive name.
89
+ *
90
+ * Mirrors Taro's CSS #ifdef/#ifndef/#endif token handling.
91
+ */
92
+ function toConditionalDirectiveName(value: string): ConditionalDirectiveName | undefined {
93
+ if (
94
+ value === 'ifdef' ||
95
+ value === 'ifndef' ||
96
+ value === 'if' ||
97
+ value === 'elif' ||
98
+ value === 'else' ||
99
+ value === 'endif'
100
+ ) {
101
+ return value
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Updates the active conditional stack using Taro-style #ifdef/#ifndef/#else/#endif semantics.
107
+ *
108
+ * Plugin-only: stack-based #if/#elif/#else support has no Taro webpack counterpart.
109
+ */
110
+ function updateConditionalDirectiveFrames(
111
+ frames: ConditionalDirectiveFrame[],
112
+ directive: ConditionalDirective,
113
+ target: VitePluginTaroTarget
114
+ ): void {
115
+ if (directive.name === 'ifdef' || directive.name === 'ifndef' || directive.name === 'if') {
116
+ const conditionMatched = evaluateConditionalDirective(directive, target)
117
+ const parentActive = isDirectiveStackActive(frames)
118
+ frames.push({ parentActive, active: parentActive && conditionMatched, matched: conditionMatched })
119
+ return
120
+ }
121
+
122
+ const currentFrame = frames.at(-1)
123
+ if (!currentFrame) return
124
+
125
+ if (directive.name === 'elif') {
126
+ if (currentFrame.matched) {
127
+ currentFrame.active = false
128
+ return
129
+ }
130
+ const conditionMatched = evaluateConditionalDirective(directive, target)
131
+ currentFrame.active = currentFrame.parentActive && conditionMatched
132
+ currentFrame.matched = conditionMatched
133
+ return
134
+ }
135
+
136
+ if (directive.name === 'else') {
137
+ currentFrame.active = currentFrame.parentActive && !currentFrame.matched
138
+ currentFrame.matched = true
139
+ return
140
+ }
141
+
142
+ if (directive.name === 'endif') frames.pop()
143
+ }
144
+
145
+ /**
146
+ * Evaluates the small expression subset used by Taro conditional comments.
147
+ *
148
+ * Mirrors Taro's simple CSS platform membership checks.
149
+ */
150
+ function evaluateConditionalDirective(directive: ConditionalDirective, target: VitePluginTaroTarget): boolean {
151
+ if (directive.name === 'ifndef') return !matchesDirectiveTarget(directive.expression, target)
152
+ if (directive.name === 'ifdef') return matchesDirectiveTarget(directive.expression, target)
153
+ return evaluateConditionalExpression(directive.expression, target)
154
+ }
155
+
156
+ /**
157
+ * Supports simple #if expressions with !, &&, and || over the configured target token.
158
+ *
159
+ * Plugin-only: #if expressions with && and || have no Taro webpack counterpart.
160
+ */
161
+ function evaluateConditionalExpression(expression: string, target: VitePluginTaroTarget): boolean {
162
+ const orTerms = expression.split('||')
163
+ return orTerms.some((term) =>
164
+ term
165
+ .split('&&')
166
+ .map((factor) => factor.trim())
167
+ .filter(Boolean)
168
+ .every((factor) => evaluateConditionalFactor(factor, target))
169
+ )
170
+ }
171
+
172
+ /**
173
+ * Evaluates one target token, optionally negated.
174
+ *
175
+ * Plugin-only: negated #if factors have no Taro webpack counterpart.
176
+ */
177
+ function evaluateConditionalFactor(factor: string, target: VitePluginTaroTarget): boolean {
178
+ let token = factor.replace(/[()]/g, '').trim()
179
+ let negated = false
180
+ while (token.startsWith('!')) {
181
+ negated = !negated
182
+ token = token.slice(1).trim()
183
+ }
184
+ const matched = matchesDirectiveTarget(token, target)
185
+ return negated ? !matched : matched
186
+ }
187
+
188
+ /**
189
+ * Checks whether a directive target list includes the current target.
190
+ *
191
+ * Mirrors Taro's simple CSS platform membership checks.
192
+ */
193
+ function matchesDirectiveTarget(expression: string, target: VitePluginTaroTarget): boolean {
194
+ const tokens = expression
195
+ .split(/[\s,|&()!]+/)
196
+ .map((token) => token.trim().toLowerCase())
197
+ .filter(Boolean)
198
+ return tokens.includes(target)
199
+ }
200
+
201
+ /**
202
+ * Preserves source line counts when conditional blocks are stripped.
203
+ *
204
+ * Plugin-only: preserves Vite source-map line counts while stripping conditional blocks.
205
+ */
206
+ function getLineEnding(line: string): string {
207
+ const match = line.match(/\r?\n$/)
208
+ return match?.[0] ?? ''
209
+ }
210
+
211
+ /**
212
+ * Returns whether all active nested conditional frames include the current line.
213
+ *
214
+ * Plugin-only: stack activity helper for generalized conditional directives.
215
+ */
216
+ function isDirectiveStackActive(frames: ConditionalDirectiveFrame[]): boolean {
217
+ return frames.every((frame) => frame.active)
218
+ }
@@ -0,0 +1,41 @@
1
+ import tailwindcss from '@tailwindcss/vite'
2
+ import type { PluginOption } from 'vite'
3
+ import { WeappTailwindcss } from 'weapp-tailwindcss/vite'
4
+ import type { VitePluginTaroBuildContext } from './types.ts'
5
+
6
+ export function createTailwindcssPlugins(context: VitePluginTaroBuildContext): PluginOption[] {
7
+ if (context.target === 'h5') return [tailwindcss()]
8
+
9
+ const plugins = WeappTailwindcss({
10
+ appType: 'taro',
11
+ generator: {
12
+ target: 'weapp'
13
+ },
14
+ tailwindcss: {
15
+ version: 4,
16
+ packageName: 'tailwindcss'
17
+ },
18
+ cssCalc: false,
19
+ // skyline does not support -webkit prefix.
20
+ autoprefixer: false,
21
+ postcssOptions: {
22
+ // Tailwind v4 prod mode emits legacy :before/:after selectors; skyline requires ::before/::after.
23
+ plugins: [createWechatPseudoElementPlugin()]
24
+ },
25
+ rem2rpx: true,
26
+ px2rpx: true
27
+ })
28
+
29
+ return plugins ?? []
30
+ }
31
+
32
+ function createWechatPseudoElementPlugin() {
33
+ const legacyPseudoElementPattern = /(?<!:):(before|after)\b/g
34
+
35
+ return {
36
+ postcssPlugin: 'vite-plugin-taro-wechat-pseudo-elements',
37
+ Rule(rule: { selector: string }) {
38
+ rule.selector = rule.selector.replace(legacyPseudoElementPattern, '::$1')
39
+ }
40
+ }
41
+ }