vite-plugin-taro 0.2.2 → 0.3.0
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 +83 -106
- package/README.zh.md +83 -104
- package/dist/node/build-context.d.ts +25 -0
- package/dist/node/build-context.js +48 -0
- package/dist/node/css/css-pipeline.d.ts +21 -0
- package/dist/node/css/css-pipeline.js +129 -0
- package/dist/node/plugins/conditional-directives.d.ts +3 -0
- package/dist/node/plugins/conditional-directives.js +62 -0
- package/dist/node/plugins/taro-runtime.d.ts +5 -0
- package/dist/node/plugins/taro-runtime.js +18 -0
- package/dist/node/targets/h5/plugin.d.ts +6 -0
- package/dist/node/targets/h5/plugin.js +117 -0
- package/dist/node/targets/h5/virtual-modules.d.ts +5 -0
- package/dist/node/targets/h5/virtual-modules.js +60 -0
- package/dist/node/targets/wx/companion-assets.d.ts +18 -0
- package/dist/node/targets/wx/companion-assets.js +121 -0
- package/dist/node/targets/wx/dev-server/development-session.d.ts +31 -0
- package/dist/node/targets/wx/dev-server/development-session.js +202 -0
- package/dist/node/targets/wx/dev-server/full-build-scheduler.d.ts +14 -0
- package/dist/node/targets/wx/dev-server/full-build-scheduler.js +60 -0
- package/dist/node/targets/wx/dev-server/js-utils.d.ts +5 -0
- package/dist/node/targets/wx/dev-server/js-utils.js +28 -0
- package/dist/node/targets/wx/dev-server/output.d.ts +19 -0
- package/dist/node/targets/wx/dev-server/output.js +44 -0
- package/dist/node/targets/wx/dev-server/rolldown-runtime-source.d.ts +8 -0
- package/dist/node/targets/wx/dev-server/rolldown-runtime-source.js +95 -0
- package/dist/node/targets/wx/dev-server/update-server-state.d.ts +65 -0
- package/dist/node/targets/wx/dev-server/update-server-state.js +116 -0
- package/dist/node/targets/wx/dev-server/update-transport.d.ts +28 -0
- package/dist/node/targets/wx/dev-server/update-transport.js +212 -0
- package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.d.ts +44 -0
- package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.js +145 -0
- package/dist/node/targets/wx/development-files.d.ts +8 -0
- package/dist/node/targets/wx/development-files.js +8 -0
- package/dist/node/targets/wx/plugin.d.ts +6 -0
- package/dist/node/targets/wx/plugin.js +143 -0
- package/dist/node/targets/wx/react-refresh.d.ts +4 -0
- package/dist/node/targets/wx/react-refresh.js +38 -0
- package/dist/node/targets/wx/virtual-modules.d.ts +16 -0
- package/dist/node/targets/wx/virtual-modules.js +120 -0
- package/dist/node/utils/async.d.ts +8 -0
- package/dist/node/utils/async.js +16 -0
- package/dist/node/utils/filesystem.d.ts +13 -0
- package/dist/node/utils/filesystem.js +43 -0
- package/dist/{vite/utils.d.ts → node/utils/modules.d.ts} +2 -2
- package/dist/{vite/utils.js → node/utils/modules.js} +10 -3
- package/dist/node/utils/packages.d.ts +2 -0
- package/dist/node/utils/packages.js +8 -0
- package/dist/node/vite-plugin.d.ts +4 -0
- package/dist/node/vite-plugin.js +33 -0
- package/dist/options.d.ts +21 -0
- package/dist/{shim/h5.d.ts → runtime/h5/taro-runtime.d.ts} +3 -1
- package/dist/runtime/h5/taro-runtime.js +9 -0
- package/dist/runtime/taro/api.d.ts +5 -0
- package/dist/{virtual → runtime/taro}/api.js +3 -0
- package/dist/runtime/taro/components.d.ts +2 -0
- package/dist/runtime/taro/components.js +2 -0
- package/dist/runtime/wx/page-update.d.ts +6 -0
- package/dist/runtime/wx/page-update.js +157 -0
- package/dist/{shim/wx.d.ts → runtime/wx/taro-runtime.d.ts} +1 -0
- package/dist/{shim/wx.js → runtime/wx/taro-runtime.js} +1 -0
- package/dist/runtime/wx/update-client-state.d.ts +68 -0
- package/dist/runtime/wx/update-client-state.js +84 -0
- package/dist/runtime/wx/update-client.d.ts +1 -0
- package/dist/runtime/wx/update-client.js +170 -0
- package/dist/vite.d.ts +2 -2
- package/dist/vite.js +1 -1
- package/package.json +15 -11
- package/src/node/build-context.ts +63 -0
- package/src/node/css/css-pipeline.ts +147 -0
- package/src/node/plugins/conditional-directives.ts +76 -0
- package/src/node/plugins/taro-runtime.ts +22 -0
- package/src/node/targets/h5/plugin.ts +135 -0
- package/src/node/targets/h5/virtual-modules.ts +71 -0
- package/src/node/targets/wx/companion-assets.ts +160 -0
- package/src/node/targets/wx/dev-server/development-session.ts +253 -0
- package/src/node/targets/wx/dev-server/full-build-scheduler.ts +62 -0
- package/src/node/targets/wx/dev-server/js-utils.ts +33 -0
- package/src/node/targets/wx/dev-server/output.ts +56 -0
- package/src/node/targets/wx/dev-server/rolldown-runtime-source.ts +95 -0
- package/src/node/targets/wx/dev-server/update-server-state.ts +183 -0
- package/src/node/targets/wx/dev-server/update-transport.ts +261 -0
- package/src/node/targets/wx/dev-server/vite-bundled-dev-adapter.ts +203 -0
- package/src/node/targets/wx/development-files.ts +11 -0
- package/src/node/targets/wx/plugin.ts +163 -0
- package/src/node/targets/wx/react-refresh.ts +51 -0
- package/src/node/targets/wx/virtual-modules.ts +152 -0
- package/src/node/utils/async.ts +19 -0
- package/src/node/utils/filesystem.ts +45 -0
- package/src/{vite/utils.ts → node/utils/modules.ts} +11 -3
- package/src/node/utils/packages.ts +10 -0
- package/src/node/vite-plugin.ts +41 -0
- package/src/options.ts +25 -0
- package/src/runtime/h5/taro-css.d.ts +3 -0
- package/src/runtime/h5/taro-runtime.ts +10 -0
- package/src/{virtual → runtime/taro}/api.ts +3 -0
- package/src/runtime/taro/components.ts +2 -0
- package/src/runtime/wx/page-update.ts +230 -0
- package/src/runtime/wx/react-refresh.d.ts +20 -0
- package/src/{shim/wx.ts → runtime/wx/taro-runtime.ts} +1 -0
- package/src/runtime/wx/update-client-state.ts +138 -0
- package/src/runtime/wx/update-client.ts +222 -0
- package/src/vite.ts +2 -2
- package/dist/shim/h5.js +0 -5
- package/dist/virtual/api.d.ts +0 -3
- package/dist/virtual/components.d.ts +0 -1
- package/dist/virtual/components.js +0 -1
- package/dist/vite/constants.d.ts +0 -4
- package/dist/vite/constants.js +0 -8
- package/dist/vite/plugins.d.ts +0 -8
- package/dist/vite/plugins.js +0 -186
- package/dist/vite/tailwindcss.d.ts +0 -3
- package/dist/vite/tailwindcss.js +0 -35
- package/dist/vite/targets/h5.d.ts +0 -31
- package/dist/vite/targets/h5.js +0 -201
- package/dist/vite/targets/wx.d.ts +0 -74
- package/dist/vite/targets/wx.js +0 -362
- package/dist/vite/types.d.ts +0 -38
- package/dist/vite/virtual-modules.d.ts +0 -3
- package/dist/vite/virtual-modules.js +0 -13
- package/dist/vite/vite-plugin-taro.d.ts +0 -7
- package/dist/vite/vite-plugin-taro.js +0 -80
- package/src/shim/h5.ts +0 -6
- package/src/virtual/components.ts +0 -1
- package/src/vite/constants.ts +0 -12
- package/src/vite/plugins.ts +0 -218
- package/src/vite/tailwindcss.ts +0 -41
- package/src/vite/targets/h5.ts +0 -229
- package/src/vite/targets/wx.ts +0 -436
- package/src/vite/types.ts +0 -48
- package/src/vite/virtual-modules.ts +0 -14
- package/src/vite/vite-plugin-taro.ts +0 -106
- /package/dist/{vite/types.js → options.js} +0 -0
package/dist/vite/plugins.js
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
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 createVitePluginTaroConditionalDirectivePlugin(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
|
-
* Plugin-only: source filter for 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
|
-
* Plugin-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 the configured target token.
|
|
132
|
-
*
|
|
133
|
-
* Plugin-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 target token, optionally negated.
|
|
145
|
-
*
|
|
146
|
-
* Plugin-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 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
|
-
* Plugin-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
|
-
* Plugin-only: stack activity helper for generalized conditional directives.
|
|
183
|
-
*/
|
|
184
|
-
function isDirectiveStackActive(frames) {
|
|
185
|
-
return frames.every((frame) => frame.active);
|
|
186
|
-
}
|
package/dist/vite/tailwindcss.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import tailwindcss from '@tailwindcss/vite';
|
|
2
|
-
import { WeappTailwindcss } from 'weapp-tailwindcss/vite';
|
|
3
|
-
export function createTailwindcssPlugins(context) {
|
|
4
|
-
if (context.target === 'h5')
|
|
5
|
-
return [tailwindcss()];
|
|
6
|
-
const plugins = WeappTailwindcss({
|
|
7
|
-
appType: 'taro',
|
|
8
|
-
generator: {
|
|
9
|
-
target: 'weapp'
|
|
10
|
-
},
|
|
11
|
-
tailwindcss: {
|
|
12
|
-
version: 4,
|
|
13
|
-
packageName: 'tailwindcss'
|
|
14
|
-
},
|
|
15
|
-
cssCalc: false,
|
|
16
|
-
// skyline does not support -webkit prefix.
|
|
17
|
-
autoprefixer: false,
|
|
18
|
-
postcssOptions: {
|
|
19
|
-
// Tailwind v4 prod mode emits legacy :before/:after selectors; skyline requires ::before/::after.
|
|
20
|
-
plugins: [createWechatPseudoElementPlugin()]
|
|
21
|
-
},
|
|
22
|
-
rem2rpx: true,
|
|
23
|
-
px2rpx: true
|
|
24
|
-
});
|
|
25
|
-
return plugins ?? [];
|
|
26
|
-
}
|
|
27
|
-
function createWechatPseudoElementPlugin() {
|
|
28
|
-
const legacyPseudoElementPattern = /(?<!:):(before|after)\b/g;
|
|
29
|
-
return {
|
|
30
|
-
postcssPlugin: 'vite-plugin-taro-wechat-pseudo-elements',
|
|
31
|
-
Rule(rule) {
|
|
32
|
-
rule.selector = rule.selector.replace(legacyPseudoElementPattern, '::$1');
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { HtmlTagDescriptor, PluginOption, UserConfig } from 'vite';
|
|
2
|
-
import type { VitePluginTaroBuildContext } from '../types.ts';
|
|
3
|
-
/**
|
|
4
|
-
* Checks whether an id belongs to an H5 virtual module.
|
|
5
|
-
*/
|
|
6
|
-
export declare function isH5VirtualModuleId(id: string): boolean;
|
|
7
|
-
/**
|
|
8
|
-
* Loads generated source for H5 virtual modules.
|
|
9
|
-
*/
|
|
10
|
-
export declare function loadH5VirtualModule(cleanId: string, context: VitePluginTaroBuildContext): string | undefined;
|
|
11
|
-
/**
|
|
12
|
-
* Configures the Vite pieces needed for Taro H5 resolve/runtime behavior.
|
|
13
|
-
*/
|
|
14
|
-
export declare function createH5ViteConfig(): UserConfig;
|
|
15
|
-
/**
|
|
16
|
-
* Creates H5-only support plugins used before the target emitter runs.
|
|
17
|
-
*
|
|
18
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-h5/src/program.ts#L219-L249
|
|
19
|
-
*/
|
|
20
|
-
export declare function createH5SupportPlugins(): PluginOption[];
|
|
21
|
-
/**
|
|
22
|
-
* Injects vite-plugin-taro's generated Web entry into Vite's HTML shell.
|
|
23
|
-
*/
|
|
24
|
-
export declare function createWebIndexHtmlTags(context: VitePluginTaroBuildContext): HtmlTagDescriptor[] | undefined;
|
|
25
|
-
/**
|
|
26
|
-
* Builds the generated Web entry around Taro's official Web router/runtime APIs.
|
|
27
|
-
* Base Taro CSS is imported before the app; component CSS order is handled by rewriteStencilStyleInsertion.
|
|
28
|
-
*
|
|
29
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L120-L150
|
|
30
|
-
*/
|
|
31
|
-
export declare function createWebEntry(context: VitePluginTaroBuildContext): string;
|
package/dist/vite/targets/h5.js
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import babel from '@rolldown/plugin-babel';
|
|
2
|
-
import react from '@vitejs/plugin-react';
|
|
3
|
-
import { h5ShimImportPath, isProd, nodeRequire } from '../constants.js';
|
|
4
|
-
import { createPageComponentImport, toImportPath } from '../utils.js';
|
|
5
|
-
import { virtualTaroApiId } from '../virtual-modules.js';
|
|
6
|
-
const virtualH5Id = 'virtual:vite-plugin-taro/h5';
|
|
7
|
-
const patchStencilCssOrder = true;
|
|
8
|
-
/**
|
|
9
|
-
* Checks whether an id belongs to an H5 virtual module.
|
|
10
|
-
*/
|
|
11
|
-
export function isH5VirtualModuleId(id) {
|
|
12
|
-
return id === virtualH5Id;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Loads generated source for H5 virtual modules.
|
|
16
|
-
*/
|
|
17
|
-
export function loadH5VirtualModule(cleanId, context) {
|
|
18
|
-
if (cleanId !== virtualH5Id)
|
|
19
|
-
return;
|
|
20
|
-
return createWebEntry(context);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Configures the Vite pieces needed for Taro H5 resolve/runtime behavior.
|
|
24
|
-
*/
|
|
25
|
-
export function createH5ViteConfig() {
|
|
26
|
-
return {
|
|
27
|
-
define: createH5TaroDefines(),
|
|
28
|
-
resolve: {
|
|
29
|
-
mainFields: ['main:h5', 'browser', 'module', 'jsnext:main', 'jsnext'],
|
|
30
|
-
alias: [
|
|
31
|
-
// Resolve Stencil's transitive runtime import after Taro components are optimized separately.
|
|
32
|
-
...(patchStencilCssOrder
|
|
33
|
-
? [
|
|
34
|
-
{
|
|
35
|
-
find: /^@stencil\/core\/internal\/client$/,
|
|
36
|
-
replacement: nodeRequire.resolve('@stencil/core/internal/client', {
|
|
37
|
-
paths: [nodeRequire.resolve('@tarojs/components/package.json')]
|
|
38
|
-
})
|
|
39
|
-
}
|
|
40
|
-
]
|
|
41
|
-
: []),
|
|
42
|
-
// H5 React code must use Taro's React component wrappers, not the raw custom-element entry.
|
|
43
|
-
{ find: /^@tarojs\/components$/, replacement: nodeRequire.resolve('@tarojs/components/lib/react') },
|
|
44
|
-
// Taro's H5 router/components deep-import this custom-element loader; make it resolvable under pnpm.
|
|
45
|
-
{
|
|
46
|
-
find: /^@tarojs\/components\/dist\/components$/,
|
|
47
|
-
replacement: nodeRequire.resolve('@tarojs/components/dist/components')
|
|
48
|
-
},
|
|
49
|
-
// H5 APIs are exported from the platform API barrel; the generic @tarojs/taro root is native-oriented.
|
|
50
|
-
{
|
|
51
|
-
find: /^@tarojs\/taro$/,
|
|
52
|
-
replacement: nodeRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
|
|
53
|
-
}
|
|
54
|
-
]
|
|
55
|
-
},
|
|
56
|
-
optimizeDeps: {
|
|
57
|
-
// Keep the Stencil runtime in Vite's transform pipeline so rewriteStencilStyleInsertion can patch it.
|
|
58
|
-
exclude: [patchStencilCssOrder ? '@stencil/core/internal/client' : ''].filter(Boolean)
|
|
59
|
-
},
|
|
60
|
-
build: {
|
|
61
|
-
target: 'es2018',
|
|
62
|
-
minify: isProd
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Creates H5-only support plugins used before the target emitter runs.
|
|
68
|
-
*
|
|
69
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-h5/src/program.ts#L219-L249
|
|
70
|
-
*/
|
|
71
|
-
export function createH5SupportPlugins() {
|
|
72
|
-
const plugins = [...react()];
|
|
73
|
-
if (patchStencilCssOrder) {
|
|
74
|
-
plugins.push(babel({
|
|
75
|
-
include: /[\\/]@stencil[\\/]core[\\/]internal[\\/]client[\\/]index\.js(?:\?.*)?$/,
|
|
76
|
-
exclude: [],
|
|
77
|
-
plugins: [rewriteStencilStyleInsertion]
|
|
78
|
-
}));
|
|
79
|
-
}
|
|
80
|
-
// Mirrors Taro H5: rewrite default Taro.xxx calls from virtual:taro/api to named H5 API imports.
|
|
81
|
-
plugins.push(babel({
|
|
82
|
-
plugins: [
|
|
83
|
-
[
|
|
84
|
-
nodeRequire.resolve('babel-plugin-transform-taroapi'),
|
|
85
|
-
{
|
|
86
|
-
packageName: virtualTaroApiId,
|
|
87
|
-
definition: nodeRequire(nodeRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json'))
|
|
88
|
-
}
|
|
89
|
-
]
|
|
90
|
-
]
|
|
91
|
-
}));
|
|
92
|
-
return plugins;
|
|
93
|
-
}
|
|
94
|
-
function rewriteStencilStyleInsertion() {
|
|
95
|
-
return {
|
|
96
|
-
name: 'rewrite-stencil-style-insertion',
|
|
97
|
-
visitor: {
|
|
98
|
-
CallExpression(path) {
|
|
99
|
-
if (!isStencilStyleInsertBeforeCall(path))
|
|
100
|
-
return;
|
|
101
|
-
path.get('arguments.1').replaceWithSourceString(`scopeId.startsWith('sc-taro-') ? styleContainerNode.querySelector('style,link[rel="stylesheet"]') : styleContainerNode.querySelector('link')`);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
function isStencilStyleInsertBeforeCall(path) {
|
|
107
|
-
return (path.get('callee').matchesPattern('styleContainerNode.insertBefore') &&
|
|
108
|
-
path.get('arguments.0').toString() === 'styleElm' &&
|
|
109
|
-
path.get('arguments.1').toString() === "styleContainerNode.querySelector('link')");
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Creates compile-time constants expected by Taro's Web runtime packages.
|
|
113
|
-
*
|
|
114
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/H5WebpackPlugin.ts#L51-L69
|
|
115
|
-
*/
|
|
116
|
-
function createH5TaroDefines() {
|
|
117
|
-
return {
|
|
118
|
-
'process.env.FRAMEWORK': JSON.stringify('react'),
|
|
119
|
-
'process.env.SUPPORT_TARO_POLYFILL': JSON.stringify('disabled'),
|
|
120
|
-
'process.env.TARO_ENV': JSON.stringify('h5'),
|
|
121
|
-
'process.env.TARO_PLATFORM': JSON.stringify('web'),
|
|
122
|
-
'process.env.SUPPORT_DINGTALK_NAVIGATE': JSON.stringify('disabled'),
|
|
123
|
-
DEPRECATED_ADAPTER_COMPONENT: 'false'
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Injects vite-plugin-taro's generated Web entry into Vite's HTML shell.
|
|
128
|
-
*/
|
|
129
|
-
export function createWebIndexHtmlTags(context) {
|
|
130
|
-
if (context.target !== 'h5')
|
|
131
|
-
return;
|
|
132
|
-
const tags = [];
|
|
133
|
-
tags.push({
|
|
134
|
-
tag: 'script',
|
|
135
|
-
attrs: { type: 'module' },
|
|
136
|
-
children: `import '${virtualH5Id}'`,
|
|
137
|
-
injectTo: 'body'
|
|
138
|
-
});
|
|
139
|
-
return tags;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Builds the generated Web entry around Taro's official Web router/runtime APIs.
|
|
143
|
-
* Base Taro CSS is imported before the app; component CSS order is handled by rewriteStencilStyleInsertion.
|
|
144
|
-
*
|
|
145
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L120-L150
|
|
146
|
-
*/
|
|
147
|
-
export function createWebEntry(context) {
|
|
148
|
-
const webAppConfigCode = JSON.stringify(createWebAppConfig(context.appConfig));
|
|
149
|
-
const webRoutesConfigCode = createWebRoutesConfig(context.pages);
|
|
150
|
-
return `import ${JSON.stringify(toImportPath(nodeRequire.resolve('@tarojs/components/global.css')))}
|
|
151
|
-
import ${JSON.stringify(toImportPath(nodeRequire.resolve('@tarojs/components/dist/taro-components/taro-components.css')))}
|
|
152
|
-
import {
|
|
153
|
-
createHashHistory,
|
|
154
|
-
createReactApp,
|
|
155
|
-
createRouter,
|
|
156
|
-
handleAppMount,
|
|
157
|
-
window
|
|
158
|
-
} from ${JSON.stringify(h5ShimImportPath)}
|
|
159
|
-
import React from 'react'
|
|
160
|
-
import ReactDOM from 'react-dom/client'
|
|
161
|
-
import AppComponent from ${JSON.stringify(context.appComponentImport)}
|
|
162
|
-
|
|
163
|
-
const config = window.__taroAppConfig = ${webAppConfigCode}
|
|
164
|
-
config.routes = ${webRoutesConfigCode}
|
|
165
|
-
const app = createReactApp(AppComponent, React, ReactDOM, config)
|
|
166
|
-
const history = createHashHistory({ window })
|
|
167
|
-
handleAppMount(config, history)
|
|
168
|
-
createRouter(history, app, config, React)
|
|
169
|
-
`;
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Creates the H5 app config consumed by Taro's Web router.
|
|
173
|
-
* Taro's H5 runtime expects `config.router` to exist, even when it is an empty object.
|
|
174
|
-
*
|
|
175
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts#L49-L53
|
|
176
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts#L133-L138
|
|
177
|
-
*/
|
|
178
|
-
function createWebAppConfig(sharedAppConfig) {
|
|
179
|
-
return {
|
|
180
|
-
router: {},
|
|
181
|
-
...sharedAppConfig
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Creates Web route records in the same shape as Taro's H5 loader.
|
|
186
|
-
*
|
|
187
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
|
|
188
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L108-L114
|
|
189
|
-
*/
|
|
190
|
-
function createWebRoutesConfig(webPages) {
|
|
191
|
-
const webRoutes = webPages.map((page) => [
|
|
192
|
-
'Object.assign({',
|
|
193
|
-
` path: ${JSON.stringify(page.path)},`,
|
|
194
|
-
' load: async function(context, params) {',
|
|
195
|
-
` const page = await import(${JSON.stringify(createPageComponentImport(page.path))})`,
|
|
196
|
-
' return [page, context, params]',
|
|
197
|
-
' }',
|
|
198
|
-
`}, ${JSON.stringify(page.config)})`
|
|
199
|
-
].join('\n'));
|
|
200
|
-
return `[\n${webRoutes.join(',\n')}\n]`;
|
|
201
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import type { UserConfig } from 'vite';
|
|
2
|
-
import type { VitePluginTaroBuildContext, VitePluginTaroPageOption } from '../types.ts';
|
|
3
|
-
/**
|
|
4
|
-
* Checks whether an id belongs to a wx virtual module.
|
|
5
|
-
*/
|
|
6
|
-
export declare function isWxVirtualModuleId(id: string): boolean;
|
|
7
|
-
export declare function loadWxVirtualModule(cleanId: string, context: VitePluginTaroBuildContext): string | undefined;
|
|
8
|
-
/**
|
|
9
|
-
* Configures wx target entry, output, and chunk layout.
|
|
10
|
-
*/
|
|
11
|
-
export declare function createWxViteConfig(context: VitePluginTaroBuildContext): UserConfig;
|
|
12
|
-
type WechatAssetSource = string | Uint8Array;
|
|
13
|
-
type WechatBundleModule = {
|
|
14
|
-
renderedExports?: string[];
|
|
15
|
-
};
|
|
16
|
-
type WechatBundleItem = {
|
|
17
|
-
type: 'asset' | 'chunk';
|
|
18
|
-
source?: WechatAssetSource;
|
|
19
|
-
modules?: Record<string, WechatBundleModule>;
|
|
20
|
-
};
|
|
21
|
-
type WechatBundle = Record<string, WechatBundleItem>;
|
|
22
|
-
type WechatChunkEmitter = {
|
|
23
|
-
emitFile(chunk: {
|
|
24
|
-
type: 'chunk';
|
|
25
|
-
id: string;
|
|
26
|
-
fileName: string;
|
|
27
|
-
implicitlyLoadedAfterOneOf: string[];
|
|
28
|
-
}): string;
|
|
29
|
-
};
|
|
30
|
-
type WechatAssetEmitter = {
|
|
31
|
-
emitFile(asset: {
|
|
32
|
-
type: 'asset';
|
|
33
|
-
fileName: string;
|
|
34
|
-
source: WechatAssetSource;
|
|
35
|
-
}): string;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* Emits page and component chunks like Taro Webpack's MiniPlugin generated entries.
|
|
39
|
-
*
|
|
40
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L228-L243
|
|
41
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L743-L777
|
|
42
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroSingleEntryPlugin.ts#L18-L38
|
|
43
|
-
*/
|
|
44
|
-
export declare function emitWechatImplicitChunksForVirtualApp(emitter: WechatChunkEmitter, context: VitePluginTaroBuildContext, cleanId: string): void;
|
|
45
|
-
/**
|
|
46
|
-
* Builds the generated WeChat app entry that registers Taro's React App config.
|
|
47
|
-
* vite-plugin-taro omits Taro's generated pxTransform initialization because styles are handled by Tailwind.
|
|
48
|
-
*
|
|
49
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/app.ts#L54-L63
|
|
50
|
-
*/
|
|
51
|
-
export declare function createWxAppEntry(context: VitePluginTaroBuildContext): string;
|
|
52
|
-
/**
|
|
53
|
-
* Builds a generated WeChat page entry that registers Taro's Page config.
|
|
54
|
-
*
|
|
55
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/page.ts#L52-L78
|
|
56
|
-
*/
|
|
57
|
-
export declare function createWxPageEntry(pageOption: VitePluginTaroPageOption): string;
|
|
58
|
-
/**
|
|
59
|
-
* Builds the generated JS companion for comp.wxml/comp.json. Without it WeChat
|
|
60
|
-
* can load recursive markup, but it will not have Taro's properties or `eh` event
|
|
61
|
-
* dispatch method.
|
|
62
|
-
*
|
|
63
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/template/comp.ts#L1-L4
|
|
64
|
-
*/
|
|
65
|
-
export declare function createWxCompEntry(): string;
|
|
66
|
-
/**
|
|
67
|
-
* Creates Taro-style Mini Program template/config/style companion files.
|
|
68
|
-
*
|
|
69
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-weapp/src/program.ts#L33-L55
|
|
70
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1198-L1311
|
|
71
|
-
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1346-L1390
|
|
72
|
-
*/
|
|
73
|
-
export declare function emitWechatAssets(emitter: WechatAssetEmitter, bundle: WechatBundle, context: VitePluginTaroBuildContext): void;
|
|
74
|
-
export {};
|