vite-plugin-taro 0.0.2 → 0.0.3
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/LICENSE +1 -1
- package/README.md +119 -20
- package/client.d.ts +10 -0
- package/dist/shim/h5.js +6 -6
- package/dist/shim/wx.js +5 -5
- package/dist/vite/constants.js +3 -0
- package/dist/vite/plugins.js +186 -0
- package/dist/vite/targets/h5.js +161 -0
- package/dist/vite/targets/wx.js +366 -0
- package/dist/vite/taro.js +81 -0
- package/dist/vite/types.js +1 -0
- package/dist/vite/{utils.d.ts → utils.js} +13 -4
- package/dist/vite/virtual.js +26 -0
- package/dist/vite.js +1 -851
- package/package.json +10 -25
- package/dist/public/components.d.ts +0 -1
- package/dist/public/components.js +0 -1
- package/dist/public/taro.d.ts +0 -2
- package/dist/public/taro.js +0 -10
- package/dist/public/taro.js.map +0 -1
- package/dist/shim/h5.d.ts +0 -3
- package/dist/shim/wx.d.ts +0 -3
- package/dist/vite/constants.d.ts +0 -2
- package/dist/vite/plugins.d.ts +0 -8
- package/dist/vite/tailwindcss.d.ts +0 -3
- package/dist/vite/targets/h5.d.ts +0 -31
- package/dist/vite/targets/wx.d.ts +0 -74
- package/dist/vite/taro.d.ts +0 -7
- package/dist/vite/types.d.ts +0 -38
- package/dist/vite.d.ts +0 -2
- package/dist/vite.js.map +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
# vite-plugin-taro
|
|
2
2
|
|
|
3
|
-
Vite 8 + React 19 plugin for building one React/Taro codebase for both
|
|
3
|
+
Vite 8 + React 19 plugin for building one React/Taro codebase for both WeChat Mini Program (`wx`) and Web (`h5`) targets.
|
|
4
4
|
|
|
5
|
-
-
|
|
6
|
-
-
|
|
5
|
+
- npm: <https://www.npmjs.com/package/vite-plugin-taro>
|
|
6
|
+
- Sample H5 demo: <https://sep2.github.io/vite-plugin-taro/>
|
|
7
|
+
- Repository: <https://github.com/sep2/vite-plugin-taro>
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- `wx` target: emits WeChat Mini Program JS/JSON/WXML/WXSS assets.
|
|
12
|
+
- `h5` target: emits a Web app using the Taro H5 runtime and router.
|
|
13
|
+
- React 19 support via patched Taro runtime packages published as npm aliases.
|
|
14
|
+
- No app-side `patchedDependencies` required.
|
|
15
|
+
- Taro-style conditional compilation comments for TS/JS/JSX/TSX and style files.
|
|
16
|
+
- Target-specific Vite/Rolldown and WeChat output setup.
|
|
9
17
|
|
|
10
18
|
## Install
|
|
11
19
|
|
|
@@ -17,21 +25,24 @@ pnpm add react react-dom
|
|
|
17
25
|
## Vite usage
|
|
18
26
|
|
|
19
27
|
```ts
|
|
20
|
-
import taro
|
|
28
|
+
import taro from 'vite-plugin-taro/vite'
|
|
21
29
|
import { defineConfig, loadEnv } from 'vite'
|
|
22
30
|
|
|
23
31
|
export default defineConfig(({ mode }) => {
|
|
24
32
|
const env = loadEnv(mode, process.cwd(), 'VITE_PLUGIN_TARO_')
|
|
25
|
-
const target = env.VITE_PLUGIN_TARO_TARGET as
|
|
33
|
+
const target = env.VITE_PLUGIN_TARO_TARGET as 'wx' | 'h5'
|
|
26
34
|
|
|
27
35
|
return {
|
|
36
|
+
base: target === 'h5' ? './' : undefined,
|
|
28
37
|
plugins: [
|
|
29
38
|
taro({
|
|
30
39
|
target,
|
|
31
40
|
app: 'src/app.ts',
|
|
32
41
|
pages: [{ path: 'pages/index/index', config: {} }],
|
|
33
42
|
appJson: {},
|
|
34
|
-
projectConfigJson: {
|
|
43
|
+
projectConfigJson: {
|
|
44
|
+
appid: env.VITE_PLUGIN_TARO_WECHAT_APP_ID || 'touristappid'
|
|
45
|
+
},
|
|
35
46
|
sitemapJson: { rules: [{ action: 'allow', page: '*' }] }
|
|
36
47
|
})
|
|
37
48
|
]
|
|
@@ -39,16 +50,94 @@ export default defineConfig(({ mode }) => {
|
|
|
39
50
|
})
|
|
40
51
|
```
|
|
41
52
|
|
|
42
|
-
|
|
53
|
+
Example scripts:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build:h5": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=h5 vite build",
|
|
59
|
+
"build:wx": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=wx vite build",
|
|
60
|
+
"dev:h5": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=h5 vite",
|
|
61
|
+
"dev:wx": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=wx vite build --watch"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Application code should usually import only from the plugin virtual modules. `virtual:taro` is default-export only; call APIs as `Taro.xxx`.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import Taro from 'virtual:taro'
|
|
70
|
+
import { Text, View } from 'virtual:taro/components'
|
|
71
|
+
|
|
72
|
+
Taro.useLaunch(() => {})
|
|
73
|
+
Taro.getWindowInfo()
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
For Taro namespace types:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import type Taro from 'virtual:taro'
|
|
80
|
+
|
|
81
|
+
type Color = Taro.Color
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Add the virtual module declarations to the app `tsconfig.json`:
|
|
85
|
+
|
|
86
|
+
```json
|
|
87
|
+
{
|
|
88
|
+
"compilerOptions": {
|
|
89
|
+
"types": ["vite/client", "vite-plugin-taro/client"]
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Styling
|
|
95
|
+
|
|
96
|
+
`vite-plugin-taro` does not bundle a Tailwind or `weapp-tailwindcss` pipeline. Add styling plugins directly in the app's Vite config when needed. See [`loan-genius`](../loan-genius) for an example using Tailwind v4 and `weapp-tailwindcss`.
|
|
97
|
+
|
|
98
|
+
## Options
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
type TaroTarget = 'wx' | 'h5'
|
|
102
|
+
|
|
103
|
+
type TaroPageOption = {
|
|
104
|
+
path: string
|
|
105
|
+
config: Record<string, unknown>
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
interface TaroPluginOptions {
|
|
109
|
+
target: TaroTarget
|
|
110
|
+
app: string
|
|
111
|
+
pages: TaroPageOption[]
|
|
112
|
+
appJson: Record<string, unknown>
|
|
113
|
+
projectConfigJson: Record<string, unknown>
|
|
114
|
+
sitemapJson: Record<string, unknown>
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
| Option | Description |
|
|
119
|
+
| --- | --- |
|
|
120
|
+
| `target` | Active build target: `wx` or `h5`. |
|
|
121
|
+
| `app` | Source file that default-exports the root React app component. |
|
|
122
|
+
| `pages` | Ordered page list. Also becomes `app.json.pages` and H5 route order. |
|
|
123
|
+
| `appJson` | Base `app.json` content. `pages` is overwritten from `pages`. |
|
|
124
|
+
| `projectConfigJson` | `project.config.json` emitted for WeChat builds. |
|
|
125
|
+
| `sitemapJson` | `sitemap.json` emitted for WeChat builds. |
|
|
126
|
+
|
|
127
|
+
## App virtual modules
|
|
128
|
+
|
|
129
|
+
| Import | Purpose |
|
|
130
|
+
| --- | --- |
|
|
131
|
+
| `virtual:taro` | Default-only Taro API facade. Use this instead of importing `@tarojs/taro` directly. |
|
|
132
|
+
| `virtual:taro/components` | Re-export of `@tarojs/components`. Use this in app code. |
|
|
43
133
|
|
|
44
134
|
## Package exports
|
|
45
135
|
|
|
46
136
|
| Import | Purpose |
|
|
47
137
|
| --- | --- |
|
|
48
|
-
| `vite-plugin-taro` | Default Vite plugin
|
|
49
|
-
| `vite-plugin-taro/vite` | Default Vite plugin
|
|
50
|
-
| `vite-plugin-taro/
|
|
51
|
-
| `vite-plugin-taro/taro` | Taro API facade. Use this instead of importing `@tarojs/taro` directly. |
|
|
138
|
+
| `vite-plugin-taro` | Default Vite plugin entry. |
|
|
139
|
+
| `vite-plugin-taro/vite` | Default Vite plugin entry. |
|
|
140
|
+
| `vite-plugin-taro/client` | Type declarations for `virtual:taro` and `virtual:taro/components`. |
|
|
52
141
|
| `vite-plugin-taro/shim/h5` | H5 runtime shim used by generated entries. |
|
|
53
142
|
| `vite-plugin-taro/shim/wx` | WeChat runtime shim used by generated entries. |
|
|
54
143
|
|
|
@@ -91,27 +180,33 @@ vite-plugin-taro configures Rolldown for WeChat-compatible CommonJS chunks and e
|
|
|
91
180
|
|
|
92
181
|
vite-plugin-taro injects a virtual module into `index.html`, creates Taro H5 route records from `pages`, and mounts the app with Taro's hash-history router.
|
|
93
182
|
|
|
183
|
+
For GitHub Pages or any subpath deployment, set a relative/base path in Vite, for example:
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
export default defineConfig({
|
|
187
|
+
base: './'
|
|
188
|
+
})
|
|
189
|
+
```
|
|
190
|
+
|
|
94
191
|
## React 19 compatibility
|
|
95
192
|
|
|
96
|
-
Taro 4.2's official React runtime targets React 18. vite-plugin-taro depends on two small React 19-compatible runtime packages generated from the official Taro npm tarballs plus
|
|
193
|
+
Taro 4.2's official React runtime targets React 18. vite-plugin-taro depends on two small React 19-compatible runtime packages generated from the official Taro npm tarballs plus local patch files:
|
|
97
194
|
|
|
98
195
|
- `vite-plugin-taro-react`
|
|
99
196
|
- `vite-plugin-taro-plugin-framework-react`
|
|
100
197
|
|
|
101
|
-
|
|
198
|
+
When packed/published by pnpm, workspace aliases become npm aliases to these patched packages:
|
|
102
199
|
|
|
103
200
|
```json
|
|
104
201
|
{
|
|
105
|
-
"@tarojs/react": "
|
|
106
|
-
"@tarojs/plugin-framework-react": "
|
|
202
|
+
"@tarojs/react": "npm:vite-plugin-taro-react@4.2.0-react19.2",
|
|
203
|
+
"@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@4.2.0-react19.2"
|
|
107
204
|
}
|
|
108
205
|
```
|
|
109
206
|
|
|
110
|
-
|
|
207
|
+
That means app users get React 19-compatible Taro runtime packages automatically and do not need local patches.
|
|
111
208
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
## Publishing
|
|
209
|
+
## Publishing from this repository
|
|
115
210
|
|
|
116
211
|
```sh
|
|
117
212
|
pnpm install
|
|
@@ -123,3 +218,7 @@ pnpm publish:all -- --otp 123456
|
|
|
123
218
|
```
|
|
124
219
|
|
|
125
220
|
The package publishes built ESM JavaScript and `.d.ts` files from `dist`.
|
|
221
|
+
|
|
222
|
+
## License
|
|
223
|
+
|
|
224
|
+
MIT.
|
package/client.d.ts
ADDED
package/dist/shim/h5.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export {
|
|
1
|
+
import '@tarojs/plugin-platform-h5/dist/runtime';
|
|
2
|
+
import '@tarojs/components/global.css';
|
|
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/dist/shim/wx.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export {
|
|
1
|
+
import '@tarojs/plugin-platform-weapp/dist/runtime.js';
|
|
2
|
+
// @ts-expect-error Taro exposes createReactApp from this runtime-only deep entry without types.
|
|
3
|
+
export { createReactApp } from '@tarojs/plugin-framework-react/dist/runtime';
|
|
4
|
+
export { default as ReactDOM } from '@tarojs/react';
|
|
5
|
+
export { createPageConfig, createRecursiveComponentConfig } from '@tarojs/runtime';
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { normalizeModuleId } from './utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Applies Taro-style conditional compilation comments before Vite parses source files.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors Taro's CSS #ifdef/#ifndef handling, generalized before Vite parses code.
|
|
6
|
+
*/
|
|
7
|
+
export function createTaroConditionalDirectivePlugin(context) {
|
|
8
|
+
const target = context.target;
|
|
9
|
+
return {
|
|
10
|
+
name: 'vite-plugin-taro-conditional-directives',
|
|
11
|
+
enforce: 'pre',
|
|
12
|
+
transform(code, id) {
|
|
13
|
+
if (!isConditionalDirectiveSource(id) || !code.includes('#if'))
|
|
14
|
+
return;
|
|
15
|
+
return { code: transformConditionalDirectives(code, target), map: null };
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Filters files where Taro's conditional comments are meaningful.
|
|
21
|
+
*
|
|
22
|
+
* vite-plugin-taro-only: source filter for vite-plugin-taro's generalized conditional-directive transform.
|
|
23
|
+
*/
|
|
24
|
+
function isConditionalDirectiveSource(id) {
|
|
25
|
+
const normalizedId = normalizeModuleId(id);
|
|
26
|
+
if (normalizedId.includes('/node_modules/'))
|
|
27
|
+
return false;
|
|
28
|
+
return /\.(?:[cm]?[jt]sx?|css|s[ac]ss|less|styl)(?:\?|$)/.test(normalizedId);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Removes inactive blocks guarded by Taro conditional comments.
|
|
32
|
+
*
|
|
33
|
+
* Mirrors Taro's CSS #ifdef/#ifndef handling.
|
|
34
|
+
*/
|
|
35
|
+
function transformConditionalDirectives(code, target) {
|
|
36
|
+
const lines = code.match(/[^\n]*(?:\n|$)/g) ?? [];
|
|
37
|
+
const frames = [];
|
|
38
|
+
let transformed = '';
|
|
39
|
+
for (const line of lines) {
|
|
40
|
+
if (!line)
|
|
41
|
+
continue;
|
|
42
|
+
const directive = parseConditionalDirective(line);
|
|
43
|
+
const lineEnding = getLineEnding(line);
|
|
44
|
+
if (directive) {
|
|
45
|
+
updateConditionalDirectiveFrames(frames, directive, target);
|
|
46
|
+
transformed += lineEnding;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
transformed += isDirectiveStackActive(frames) ? line : lineEnding;
|
|
50
|
+
}
|
|
51
|
+
return transformed;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Parses one Taro conditional compilation directive from a comment-only line.
|
|
55
|
+
*
|
|
56
|
+
* Mirrors Taro's CSS comment-token handling.
|
|
57
|
+
*/
|
|
58
|
+
function parseConditionalDirective(line) {
|
|
59
|
+
const match = line.match(/^\s*(?:(?:\/\/)|(?:\/\*))\s*#(ifdef|ifndef|if|elif|else|endif)\b([^*\r\n]*)/);
|
|
60
|
+
if (!match)
|
|
61
|
+
return;
|
|
62
|
+
const name = toConditionalDirectiveName(match[1]);
|
|
63
|
+
if (!name)
|
|
64
|
+
return;
|
|
65
|
+
return {
|
|
66
|
+
name,
|
|
67
|
+
expression: match[2]?.replace(/\*\/$/, '').trim() ?? ''
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Converts a regex capture into a supported directive name.
|
|
72
|
+
*
|
|
73
|
+
* Mirrors Taro's CSS #ifdef/#ifndef/#endif token handling.
|
|
74
|
+
*/
|
|
75
|
+
function toConditionalDirectiveName(value) {
|
|
76
|
+
if (value === 'ifdef' ||
|
|
77
|
+
value === 'ifndef' ||
|
|
78
|
+
value === 'if' ||
|
|
79
|
+
value === 'elif' ||
|
|
80
|
+
value === 'else' ||
|
|
81
|
+
value === 'endif') {
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Updates the active conditional stack using Taro-style #ifdef/#ifndef/#else/#endif semantics.
|
|
87
|
+
*
|
|
88
|
+
* vite-plugin-taro-only: stack-based #if/#elif/#else support has no Taro webpack counterpart.
|
|
89
|
+
*/
|
|
90
|
+
function updateConditionalDirectiveFrames(frames, directive, target) {
|
|
91
|
+
if (directive.name === 'ifdef' || directive.name === 'ifndef' || directive.name === 'if') {
|
|
92
|
+
const conditionMatched = evaluateConditionalDirective(directive, target);
|
|
93
|
+
const parentActive = isDirectiveStackActive(frames);
|
|
94
|
+
frames.push({ parentActive, active: parentActive && conditionMatched, matched: conditionMatched });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const currentFrame = frames.at(-1);
|
|
98
|
+
if (!currentFrame)
|
|
99
|
+
return;
|
|
100
|
+
if (directive.name === 'elif') {
|
|
101
|
+
if (currentFrame.matched) {
|
|
102
|
+
currentFrame.active = false;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const conditionMatched = evaluateConditionalDirective(directive, target);
|
|
106
|
+
currentFrame.active = currentFrame.parentActive && conditionMatched;
|
|
107
|
+
currentFrame.matched = conditionMatched;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (directive.name === 'else') {
|
|
111
|
+
currentFrame.active = currentFrame.parentActive && !currentFrame.matched;
|
|
112
|
+
currentFrame.matched = true;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (directive.name === 'endif')
|
|
116
|
+
frames.pop();
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Evaluates the small expression subset used by Taro conditional comments.
|
|
120
|
+
*
|
|
121
|
+
* Mirrors Taro's simple CSS platform membership checks.
|
|
122
|
+
*/
|
|
123
|
+
function evaluateConditionalDirective(directive, target) {
|
|
124
|
+
if (directive.name === 'ifndef')
|
|
125
|
+
return !matchesDirectiveTarget(directive.expression, target);
|
|
126
|
+
if (directive.name === 'ifdef')
|
|
127
|
+
return matchesDirectiveTarget(directive.expression, target);
|
|
128
|
+
return evaluateConditionalExpression(directive.expression, target);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Supports simple #if expressions with !, &&, and || over vite-plugin-taro target tokens.
|
|
132
|
+
*
|
|
133
|
+
* vite-plugin-taro-only: #if expressions with && and || have no Taro webpack counterpart.
|
|
134
|
+
*/
|
|
135
|
+
function evaluateConditionalExpression(expression, target) {
|
|
136
|
+
const orTerms = expression.split('||');
|
|
137
|
+
return orTerms.some((term) => term
|
|
138
|
+
.split('&&')
|
|
139
|
+
.map((factor) => factor.trim())
|
|
140
|
+
.filter(Boolean)
|
|
141
|
+
.every((factor) => evaluateConditionalFactor(factor, target)));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Evaluates one vite-plugin-taro target token, optionally negated.
|
|
145
|
+
*
|
|
146
|
+
* vite-plugin-taro-only: negated #if factors have no Taro webpack counterpart.
|
|
147
|
+
*/
|
|
148
|
+
function evaluateConditionalFactor(factor, target) {
|
|
149
|
+
let token = factor.replace(/[()]/g, '').trim();
|
|
150
|
+
let negated = false;
|
|
151
|
+
while (token.startsWith('!')) {
|
|
152
|
+
negated = !negated;
|
|
153
|
+
token = token.slice(1).trim();
|
|
154
|
+
}
|
|
155
|
+
const matched = matchesDirectiveTarget(token, target);
|
|
156
|
+
return negated ? !matched : matched;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Checks whether a directive target list includes the current vite-plugin-taro target.
|
|
160
|
+
*
|
|
161
|
+
* Mirrors Taro's simple CSS platform membership checks.
|
|
162
|
+
*/
|
|
163
|
+
function matchesDirectiveTarget(expression, target) {
|
|
164
|
+
const tokens = expression
|
|
165
|
+
.split(/[\s,|&()!]+/)
|
|
166
|
+
.map((token) => token.trim().toLowerCase())
|
|
167
|
+
.filter(Boolean);
|
|
168
|
+
return tokens.includes(target);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Preserves source line counts when conditional blocks are stripped.
|
|
172
|
+
*
|
|
173
|
+
* vite-plugin-taro-only: preserves Vite source-map line counts while stripping conditional blocks.
|
|
174
|
+
*/
|
|
175
|
+
function getLineEnding(line) {
|
|
176
|
+
const match = line.match(/\r?\n$/);
|
|
177
|
+
return match?.[0] ?? '';
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Returns whether all active nested conditional frames include the current line.
|
|
181
|
+
*
|
|
182
|
+
* vite-plugin-taro-only: stack activity helper for generalized conditional directives.
|
|
183
|
+
*/
|
|
184
|
+
function isDirectiveStackActive(frames) {
|
|
185
|
+
return frames.every((frame) => frame.active);
|
|
186
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import babel from '@rolldown/plugin-babel';
|
|
2
|
+
import react from '@vitejs/plugin-react';
|
|
3
|
+
import { isProd, nodeRequire } from '../constants.js';
|
|
4
|
+
import { createPageComponentImport } from '../utils.js';
|
|
5
|
+
const virtualH5Id = 'virtual:vite-plugin-taro/h5';
|
|
6
|
+
/**
|
|
7
|
+
* Checks whether an id belongs to an H5 virtual module.
|
|
8
|
+
*/
|
|
9
|
+
export function isH5VirtualModuleId(id) {
|
|
10
|
+
return id === virtualH5Id;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Loads generated source for H5 virtual modules.
|
|
14
|
+
*/
|
|
15
|
+
export function loadH5VirtualModule(cleanId, context) {
|
|
16
|
+
if (cleanId !== virtualH5Id)
|
|
17
|
+
return;
|
|
18
|
+
return createWebEntry(context);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Configures the Vite pieces needed for Taro H5 resolve/runtime behavior.
|
|
22
|
+
*/
|
|
23
|
+
export function createH5ViteConfig() {
|
|
24
|
+
return {
|
|
25
|
+
define: createH5TaroDefines(),
|
|
26
|
+
resolve: {
|
|
27
|
+
mainFields: ['main:h5', 'browser', 'module', 'jsnext:main', 'jsnext'],
|
|
28
|
+
alias: [
|
|
29
|
+
// H5 React code must use Taro's React component wrappers, not the raw custom-element entry.
|
|
30
|
+
{ find: /^@tarojs\/components$/, replacement: nodeRequire.resolve('@tarojs/components/lib/react') },
|
|
31
|
+
// Taro's H5 router/components deep-import this custom-element loader; make it resolvable under pnpm.
|
|
32
|
+
{
|
|
33
|
+
find: /^@tarojs\/components\/dist\/components$/,
|
|
34
|
+
replacement: nodeRequire.resolve('@tarojs/components/dist/components')
|
|
35
|
+
},
|
|
36
|
+
// H5 APIs are exported from the platform API barrel; the generic @tarojs/taro root is native-oriented.
|
|
37
|
+
{
|
|
38
|
+
find: /^@tarojs\/taro$/,
|
|
39
|
+
replacement: nodeRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
build: {
|
|
44
|
+
target: 'es2018',
|
|
45
|
+
minify: isProd
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Creates H5-only support plugins used before the target emitter runs.
|
|
51
|
+
*
|
|
52
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-h5/src/program.ts#L219-L249
|
|
53
|
+
*/
|
|
54
|
+
export function createH5SupportPlugins() {
|
|
55
|
+
return [
|
|
56
|
+
...react(),
|
|
57
|
+
// Mirrors Taro H5: rewrite default Taro.xxx calls from virtual:taro to named H5 API imports.
|
|
58
|
+
babel({
|
|
59
|
+
plugins: [
|
|
60
|
+
[
|
|
61
|
+
nodeRequire.resolve('babel-plugin-transform-taroapi'),
|
|
62
|
+
{
|
|
63
|
+
packageName: 'virtual:taro',
|
|
64
|
+
definition: nodeRequire(nodeRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json'))
|
|
65
|
+
}
|
|
66
|
+
]
|
|
67
|
+
]
|
|
68
|
+
})
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Creates compile-time constants expected by Taro's Web runtime packages.
|
|
73
|
+
*
|
|
74
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/H5WebpackPlugin.ts#L51-L69
|
|
75
|
+
*/
|
|
76
|
+
function createH5TaroDefines() {
|
|
77
|
+
return {
|
|
78
|
+
'process.env.FRAMEWORK': JSON.stringify('react'),
|
|
79
|
+
'process.env.SUPPORT_TARO_POLYFILL': JSON.stringify('disabled'),
|
|
80
|
+
'process.env.TARO_ENV': JSON.stringify('h5'),
|
|
81
|
+
'process.env.TARO_PLATFORM': JSON.stringify('web'),
|
|
82
|
+
IS_H5: 'true',
|
|
83
|
+
IS_WEAPP: 'false',
|
|
84
|
+
'process.env.SUPPORT_DINGTALK_NAVIGATE': JSON.stringify('disabled'),
|
|
85
|
+
DEPRECATED_ADAPTER_COMPONENT: 'false'
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Injects vite-plugin-taro's generated Web entry into Vite's HTML shell.
|
|
90
|
+
*/
|
|
91
|
+
export function createWebIndexHtmlTags(context) {
|
|
92
|
+
if (context.target !== 'h5')
|
|
93
|
+
return;
|
|
94
|
+
const tags = [];
|
|
95
|
+
tags.push({
|
|
96
|
+
tag: 'script',
|
|
97
|
+
attrs: { type: 'module' },
|
|
98
|
+
children: `import '${virtualH5Id}'`,
|
|
99
|
+
injectTo: 'body'
|
|
100
|
+
});
|
|
101
|
+
return tags;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Builds the generated Web entry around Taro's official Web router/runtime APIs.
|
|
105
|
+
* vite-plugin-taro omits Taro's generated pxTransform initialization; apps should handle style transforms in their own Vite pipeline.
|
|
106
|
+
*
|
|
107
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L120-L150
|
|
108
|
+
*/
|
|
109
|
+
export function createWebEntry(context) {
|
|
110
|
+
const webAppConfigCode = JSON.stringify(createWebAppConfig(context.appConfig));
|
|
111
|
+
const webRoutesConfigCode = createWebRoutesConfig(context.pages);
|
|
112
|
+
return `import {
|
|
113
|
+
createHashHistory,
|
|
114
|
+
createReactApp,
|
|
115
|
+
createRouter,
|
|
116
|
+
handleAppMount,
|
|
117
|
+
window
|
|
118
|
+
} from 'vite-plugin-taro/shim/h5'
|
|
119
|
+
import React from 'react'
|
|
120
|
+
import ReactDOM from 'react-dom/client'
|
|
121
|
+
import AppComponent from '${context.appComponentImport}'
|
|
122
|
+
|
|
123
|
+
const config = window.__taroAppConfig = ${webAppConfigCode}
|
|
124
|
+
config.routes = ${webRoutesConfigCode}
|
|
125
|
+
const app = createReactApp(AppComponent, React, ReactDOM, config)
|
|
126
|
+
const history = createHashHistory({ window })
|
|
127
|
+
handleAppMount(config, history)
|
|
128
|
+
createRouter(history, app, config, React)
|
|
129
|
+
`;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Creates the H5 app config consumed by Taro's Web router.
|
|
133
|
+
* Taro's H5 runtime expects `config.router` to exist, even when it is an empty object.
|
|
134
|
+
*
|
|
135
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts#L49-L53
|
|
136
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts#L133-L138
|
|
137
|
+
*/
|
|
138
|
+
function createWebAppConfig(sharedAppConfig) {
|
|
139
|
+
return {
|
|
140
|
+
router: {},
|
|
141
|
+
...sharedAppConfig
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Creates Web route records in the same shape as Taro's H5 loader.
|
|
146
|
+
*
|
|
147
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
|
|
148
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L108-L114
|
|
149
|
+
*/
|
|
150
|
+
function createWebRoutesConfig(webPages) {
|
|
151
|
+
const webRoutes = webPages.map((page) => [
|
|
152
|
+
'Object.assign({',
|
|
153
|
+
` path: ${JSON.stringify(page.path)},`,
|
|
154
|
+
' load: async function(context, params) {',
|
|
155
|
+
` const page = await import(${JSON.stringify(createPageComponentImport(page.path))})`,
|
|
156
|
+
' return [page, context, params]',
|
|
157
|
+
' }',
|
|
158
|
+
`}, ${JSON.stringify(page.config)})`
|
|
159
|
+
].join('\n'));
|
|
160
|
+
return `[\n${webRoutes.join(',\n')}\n]`;
|
|
161
|
+
}
|