vite-plugin-taro 0.0.2 → 0.0.4
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 +205 -54
- package/dist/public/components.js +1 -1
- package/dist/public/taro.js +8 -10
- package/dist/shim/h5.js +5 -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/tailwindcss.js +35 -0
- package/dist/vite/targets/h5.js +202 -0
- package/dist/vite/targets/wx.js +364 -0
- package/dist/vite/types.js +1 -0
- package/dist/vite/{utils.d.ts → utils.js} +13 -4
- package/dist/vite/vite-plugin-taro.js +80 -0
- package/dist/vite.js +1 -851
- package/package.json +23 -24
- package/src/public/components.ts +1 -0
- package/src/public/taro.ts +10 -0
- package/src/shim/h5.ts +6 -0
- package/src/shim/wx.ts +6 -0
- package/src/vite/constants.ts +5 -0
- package/src/vite/plugins.ts +218 -0
- package/src/vite/tailwindcss.ts +41 -0
- package/src/vite/targets/h5.ts +230 -0
- package/src/vite/targets/wx.ts +438 -0
- package/{dist/vite/types.d.ts → src/vite/types.ts} +31 -21
- package/src/vite/utils.ts +35 -0
- package/src/vite/vite-plugin-taro.ts +105 -0
- package/src/vite.ts +2 -0
- package/dist/public/components.d.ts +0 -1
- 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.d.ts +0 -2
- package/dist/vite.js.map +0 -1
- /package/{dist → src}/public/taro.d.ts +0 -0
|
@@ -0,0 +1,202 @@
|
|
|
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
|
+
const patchStencilCssOrder = true;
|
|
7
|
+
/**
|
|
8
|
+
* Checks whether an id belongs to an H5 virtual module.
|
|
9
|
+
*/
|
|
10
|
+
export function isH5VirtualModuleId(id) {
|
|
11
|
+
return id === virtualH5Id;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Loads generated source for H5 virtual modules.
|
|
15
|
+
*/
|
|
16
|
+
export function loadH5VirtualModule(cleanId, context) {
|
|
17
|
+
if (cleanId !== virtualH5Id)
|
|
18
|
+
return;
|
|
19
|
+
return createWebEntry(context);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Configures the Vite pieces needed for Taro H5 resolve/runtime behavior.
|
|
23
|
+
*/
|
|
24
|
+
export function createH5ViteConfig() {
|
|
25
|
+
return {
|
|
26
|
+
define: createH5TaroDefines(),
|
|
27
|
+
resolve: {
|
|
28
|
+
mainFields: ['main:h5', 'browser', 'module', 'jsnext:main', 'jsnext'],
|
|
29
|
+
alias: [
|
|
30
|
+
// Resolve Stencil's transitive runtime import after Taro components are optimized separately.
|
|
31
|
+
...(patchStencilCssOrder
|
|
32
|
+
? [
|
|
33
|
+
{
|
|
34
|
+
find: /^@stencil\/core\/internal\/client$/,
|
|
35
|
+
replacement: nodeRequire.resolve('@stencil/core/internal/client', {
|
|
36
|
+
paths: [nodeRequire.resolve('@tarojs/components/package.json')]
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
: []),
|
|
41
|
+
// H5 React code must use Taro's React component wrappers, not the raw custom-element entry.
|
|
42
|
+
{ find: /^@tarojs\/components$/, replacement: nodeRequire.resolve('@tarojs/components/lib/react') },
|
|
43
|
+
// Taro's H5 router/components deep-import this custom-element loader; make it resolvable under pnpm.
|
|
44
|
+
{
|
|
45
|
+
find: /^@tarojs\/components\/dist\/components$/,
|
|
46
|
+
replacement: nodeRequire.resolve('@tarojs/components/dist/components')
|
|
47
|
+
},
|
|
48
|
+
// H5 APIs are exported from the platform API barrel; the generic @tarojs/taro root is native-oriented.
|
|
49
|
+
{
|
|
50
|
+
find: /^@tarojs\/taro$/,
|
|
51
|
+
replacement: nodeRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
optimizeDeps: {
|
|
56
|
+
// Keep the Stencil runtime in Vite's transform pipeline so rewriteStencilStyleInsertion can patch it.
|
|
57
|
+
exclude: [patchStencilCssOrder ? '@stencil/core/internal/client' : ''].filter(Boolean)
|
|
58
|
+
},
|
|
59
|
+
build: {
|
|
60
|
+
target: 'es2018',
|
|
61
|
+
minify: isProd
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates H5-only support plugins used before the target emitter runs.
|
|
67
|
+
*
|
|
68
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-h5/src/program.ts#L219-L249
|
|
69
|
+
*/
|
|
70
|
+
export function createH5SupportPlugins() {
|
|
71
|
+
const plugins = [...react()];
|
|
72
|
+
if (patchStencilCssOrder) {
|
|
73
|
+
plugins.push(babel({
|
|
74
|
+
include: /[\\/]@stencil[\\/]core[\\/]internal[\\/]client[\\/]index\.js(?:\?.*)?$/,
|
|
75
|
+
exclude: [],
|
|
76
|
+
plugins: [rewriteStencilStyleInsertion]
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
79
|
+
// Mirrors Taro H5: rewrite default Taro.xxx calls from vite-plugin-taro/taro to named H5 API imports.
|
|
80
|
+
plugins.push(babel({
|
|
81
|
+
plugins: [
|
|
82
|
+
[
|
|
83
|
+
nodeRequire.resolve('babel-plugin-transform-taroapi'),
|
|
84
|
+
{
|
|
85
|
+
packageName: 'vite-plugin-taro/taro',
|
|
86
|
+
definition: nodeRequire(nodeRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json'))
|
|
87
|
+
}
|
|
88
|
+
]
|
|
89
|
+
]
|
|
90
|
+
}));
|
|
91
|
+
return plugins;
|
|
92
|
+
}
|
|
93
|
+
function rewriteStencilStyleInsertion() {
|
|
94
|
+
return {
|
|
95
|
+
name: 'rewrite-stencil-style-insertion',
|
|
96
|
+
visitor: {
|
|
97
|
+
CallExpression(path) {
|
|
98
|
+
if (!isStencilStyleInsertBeforeCall(path))
|
|
99
|
+
return;
|
|
100
|
+
path.get('arguments.1').replaceWithSourceString(`scopeId.startsWith('sc-taro-') ? styleContainerNode.querySelector('style,link[rel="stylesheet"]') : styleContainerNode.querySelector('link')`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function isStencilStyleInsertBeforeCall(path) {
|
|
106
|
+
return (path.get('callee').matchesPattern('styleContainerNode.insertBefore') &&
|
|
107
|
+
path.get('arguments.0').toString() === 'styleElm' &&
|
|
108
|
+
path.get('arguments.1').toString() === "styleContainerNode.querySelector('link')");
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Creates compile-time constants expected by Taro's Web runtime packages.
|
|
112
|
+
*
|
|
113
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/H5WebpackPlugin.ts#L51-L69
|
|
114
|
+
*/
|
|
115
|
+
function createH5TaroDefines() {
|
|
116
|
+
return {
|
|
117
|
+
'process.env.FRAMEWORK': JSON.stringify('react'),
|
|
118
|
+
'process.env.SUPPORT_TARO_POLYFILL': JSON.stringify('disabled'),
|
|
119
|
+
'process.env.TARO_ENV': JSON.stringify('h5'),
|
|
120
|
+
'process.env.TARO_PLATFORM': JSON.stringify('web'),
|
|
121
|
+
IS_H5: 'true',
|
|
122
|
+
IS_WEAPP: 'false',
|
|
123
|
+
'process.env.SUPPORT_DINGTALK_NAVIGATE': JSON.stringify('disabled'),
|
|
124
|
+
DEPRECATED_ADAPTER_COMPONENT: 'false'
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Injects vite-plugin-taro's generated Web entry into Vite's HTML shell.
|
|
129
|
+
*/
|
|
130
|
+
export function createWebIndexHtmlTags(context) {
|
|
131
|
+
if (context.target !== 'h5')
|
|
132
|
+
return;
|
|
133
|
+
const tags = [];
|
|
134
|
+
tags.push({
|
|
135
|
+
tag: 'script',
|
|
136
|
+
attrs: { type: 'module' },
|
|
137
|
+
children: `import '${virtualH5Id}'`,
|
|
138
|
+
injectTo: 'body'
|
|
139
|
+
});
|
|
140
|
+
return tags;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Builds the generated Web entry around Taro's official Web router/runtime APIs.
|
|
144
|
+
* Base Taro CSS is imported before the app; component CSS order is handled by rewriteStencilStyleInsertion.
|
|
145
|
+
*
|
|
146
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L120-L150
|
|
147
|
+
*/
|
|
148
|
+
export function createWebEntry(context) {
|
|
149
|
+
const webAppConfigCode = JSON.stringify(createWebAppConfig(context.appConfig));
|
|
150
|
+
const webRoutesConfigCode = createWebRoutesConfig(context.pages);
|
|
151
|
+
return `import ${JSON.stringify(nodeRequire.resolve('@tarojs/components/global.css'))}
|
|
152
|
+
import ${JSON.stringify(nodeRequire.resolve('@tarojs/components/dist/taro-components/taro-components.css'))}
|
|
153
|
+
import {
|
|
154
|
+
createHashHistory,
|
|
155
|
+
createReactApp,
|
|
156
|
+
createRouter,
|
|
157
|
+
handleAppMount,
|
|
158
|
+
window
|
|
159
|
+
} from 'vite-plugin-taro/shim/h5'
|
|
160
|
+
import React from 'react'
|
|
161
|
+
import ReactDOM from 'react-dom/client'
|
|
162
|
+
import AppComponent from '${context.appComponentImport}'
|
|
163
|
+
|
|
164
|
+
const config = window.__taroAppConfig = ${webAppConfigCode}
|
|
165
|
+
config.routes = ${webRoutesConfigCode}
|
|
166
|
+
const app = createReactApp(AppComponent, React, ReactDOM, config)
|
|
167
|
+
const history = createHashHistory({ window })
|
|
168
|
+
handleAppMount(config, history)
|
|
169
|
+
createRouter(history, app, config, React)
|
|
170
|
+
`;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Creates the H5 app config consumed by Taro's Web router.
|
|
174
|
+
* Taro's H5 runtime expects `config.router` to exist, even when it is an empty object.
|
|
175
|
+
*
|
|
176
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts#L49-L53
|
|
177
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts#L133-L138
|
|
178
|
+
*/
|
|
179
|
+
function createWebAppConfig(sharedAppConfig) {
|
|
180
|
+
return {
|
|
181
|
+
router: {},
|
|
182
|
+
...sharedAppConfig
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Creates Web route records in the same shape as Taro's H5 loader.
|
|
187
|
+
*
|
|
188
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
|
|
189
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L108-L114
|
|
190
|
+
*/
|
|
191
|
+
function createWebRoutesConfig(webPages) {
|
|
192
|
+
const webRoutes = webPages.map((page) => [
|
|
193
|
+
'Object.assign({',
|
|
194
|
+
` path: ${JSON.stringify(page.path)},`,
|
|
195
|
+
' load: async function(context, params) {',
|
|
196
|
+
` const page = await import(${JSON.stringify(createPageComponentImport(page.path))})`,
|
|
197
|
+
' return [page, context, params]',
|
|
198
|
+
' }',
|
|
199
|
+
`}, ${JSON.stringify(page.config)})`
|
|
200
|
+
].join('\n'));
|
|
201
|
+
return `[\n${webRoutes.join(',\n')}\n]`;
|
|
202
|
+
}
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { recursiveMerge } from '@tarojs/helper';
|
|
3
|
+
import { Weapp as WechatPlatform } from '@tarojs/plugin-platform-weapp';
|
|
4
|
+
import { isProd, nodeRequire } from '../constants.js';
|
|
5
|
+
import { createPageComponentImport, normalizeModuleId } from '../utils.js';
|
|
6
|
+
const virtualWxAppId = 'virtual:vite-plugin-taro/wx/app';
|
|
7
|
+
const virtualWxCompId = 'virtual:vite-plugin-taro/wx/comp';
|
|
8
|
+
const virtualWxPagePrefix = 'virtual:vite-plugin-taro/wx/page/';
|
|
9
|
+
/**
|
|
10
|
+
* Checks whether an id belongs to a wx virtual module.
|
|
11
|
+
*/
|
|
12
|
+
export function isWxVirtualModuleId(id) {
|
|
13
|
+
return id === virtualWxAppId || id === virtualWxCompId || id.startsWith(virtualWxPagePrefix);
|
|
14
|
+
}
|
|
15
|
+
export function loadWxVirtualModule(cleanId, context) {
|
|
16
|
+
if (cleanId === virtualWxAppId) {
|
|
17
|
+
return createWxAppEntry(context);
|
|
18
|
+
}
|
|
19
|
+
if (cleanId === virtualWxCompId) {
|
|
20
|
+
return createWxCompEntry();
|
|
21
|
+
}
|
|
22
|
+
if (!cleanId.startsWith(virtualWxPagePrefix)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const pagePath = cleanId.slice(virtualWxPagePrefix.length);
|
|
26
|
+
const page = context.pages.find((candidate) => candidate.path === pagePath);
|
|
27
|
+
if (page) {
|
|
28
|
+
return createWxPageEntry(page);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const taroWechatComponentsReactPath = nodeRequire.resolve('@tarojs/plugin-platform-weapp/dist/components-react');
|
|
32
|
+
const vitePluginTaroSourcePath = normalizeModuleId(path.dirname(nodeRequire.resolve('vite-plugin-taro/vite')));
|
|
33
|
+
const taroVersion = String(nodeRequire('@tarojs/runtime/package.json').version);
|
|
34
|
+
/**
|
|
35
|
+
* Configures wx target entry, output, and chunk layout.
|
|
36
|
+
*/
|
|
37
|
+
export function createWxViteConfig(context) {
|
|
38
|
+
return {
|
|
39
|
+
define: createWechatTaroDefines(),
|
|
40
|
+
// https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L22-L84
|
|
41
|
+
resolve: {
|
|
42
|
+
// https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniBaseConfig.ts#L44-L73
|
|
43
|
+
alias: [{ find: /^@tarojs\/components$/, replacement: taroWechatComponentsReactPath }]
|
|
44
|
+
},
|
|
45
|
+
build: {
|
|
46
|
+
target: 'es2018',
|
|
47
|
+
assetsInlineLimit: 1024,
|
|
48
|
+
cssCodeSplit: false,
|
|
49
|
+
minify: isProd,
|
|
50
|
+
rolldownOptions: {
|
|
51
|
+
// Start from app; page/component chunks below mirror Taro Webpack's generated entries.
|
|
52
|
+
input: { app: virtualWxAppId },
|
|
53
|
+
experimental: {
|
|
54
|
+
// Rolldown's dev debug comments include virtual IDs like "\0virtual:...".
|
|
55
|
+
// WeChat DevTools can blank-screen on those NUL markers, so disable them at the source.
|
|
56
|
+
attachDebugInfo: 'none'
|
|
57
|
+
},
|
|
58
|
+
output: {
|
|
59
|
+
format: 'cjs',
|
|
60
|
+
entryFileNames: '[name].js',
|
|
61
|
+
assetFileNames: 'assets/[name][extname]',
|
|
62
|
+
chunkFileNames: createWechatChunkFileName,
|
|
63
|
+
strictExecutionOrder: true,
|
|
64
|
+
codeSplitting: {
|
|
65
|
+
includeDependenciesRecursively: false,
|
|
66
|
+
minSize: 0,
|
|
67
|
+
// https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L96-L144
|
|
68
|
+
groups: [
|
|
69
|
+
{ name: 'taro', test: isWxTaroChunkModule, priority: 100 },
|
|
70
|
+
{ name: 'vendors', test: isNodeModule, priority: 10 },
|
|
71
|
+
{ name: 'common', minShareCount: 2, minModuleSize: 1, priority: 1 }
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates compile-time constants expected by Taro's WeChat runtime packages.
|
|
81
|
+
*
|
|
82
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniWebpackPlugin.ts#L67-L94
|
|
83
|
+
*/
|
|
84
|
+
function createWechatTaroDefines() {
|
|
85
|
+
return {
|
|
86
|
+
'process.env.FRAMEWORK': JSON.stringify('react'),
|
|
87
|
+
'process.env.SUPPORT_TARO_POLYFILL': JSON.stringify('disabled'),
|
|
88
|
+
'process.env.TARO_ENV': JSON.stringify('weapp'),
|
|
89
|
+
'process.env.TARO_PLATFORM': JSON.stringify('mini'),
|
|
90
|
+
'process.env.TARO_VERSION': JSON.stringify(taroVersion),
|
|
91
|
+
IS_H5: 'false',
|
|
92
|
+
IS_WEAPP: 'true',
|
|
93
|
+
ENABLE_ADJACENT_HTML: 'false',
|
|
94
|
+
ENABLE_CLONE_NODE: 'false',
|
|
95
|
+
ENABLE_CONTAINS: 'false',
|
|
96
|
+
ENABLE_INNER_HTML: 'false',
|
|
97
|
+
ENABLE_MUTATION_OBSERVER: 'false',
|
|
98
|
+
ENABLE_SIZE_APIS: 'false',
|
|
99
|
+
ENABLE_TEMPLATE_CONTENT: 'false'
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Checks whether a module should live in the Taro/framework base chunk.
|
|
104
|
+
* vite-plugin-taro support modules are kept with Taro so pages do not duplicate runtime facades.
|
|
105
|
+
*
|
|
106
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L141-L144
|
|
107
|
+
*/
|
|
108
|
+
function isWxTaroChunkModule(id) {
|
|
109
|
+
const normalizedId = normalizeModuleId(id);
|
|
110
|
+
return normalizedId.includes('/node_modules/@tarojs/') || normalizedId.startsWith(`${vitePluginTaroSourcePath}/`);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Names Rolldown's helper chunk like Taro webpack's runtime chunk.
|
|
114
|
+
*
|
|
115
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L96-L103
|
|
116
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L115-L117
|
|
117
|
+
*/
|
|
118
|
+
function createWechatChunkFileName(chunkInfo) {
|
|
119
|
+
return `${chunkInfo.name === 'rolldown-runtime' ? 'runtime' : chunkInfo.name}.js`;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Checks whether a module is a third-party dependency chunk candidate.
|
|
123
|
+
*
|
|
124
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L132-L139
|
|
125
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L36
|
|
126
|
+
*/
|
|
127
|
+
function isNodeModule(id) {
|
|
128
|
+
return normalizeModuleId(id).includes('/node_modules/');
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Emits page and component chunks like Taro Webpack's MiniPlugin generated entries.
|
|
132
|
+
*
|
|
133
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L228-L243
|
|
134
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L743-L777
|
|
135
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroSingleEntryPlugin.ts#L18-L38
|
|
136
|
+
*/
|
|
137
|
+
export function emitWechatImplicitChunksForVirtualApp(emitter, context, cleanId) {
|
|
138
|
+
if (context.target !== 'wx' || cleanId !== virtualWxAppId)
|
|
139
|
+
return;
|
|
140
|
+
for (const page of context.pages) {
|
|
141
|
+
emitter.emitFile({
|
|
142
|
+
type: 'chunk',
|
|
143
|
+
id: `${virtualWxPagePrefix}${page.path}`,
|
|
144
|
+
fileName: `${page.path}.js`,
|
|
145
|
+
implicitlyLoadedAfterOneOf: [cleanId]
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
emitter.emitFile({
|
|
149
|
+
type: 'chunk',
|
|
150
|
+
id: virtualWxCompId,
|
|
151
|
+
fileName: 'comp.js',
|
|
152
|
+
implicitlyLoadedAfterOneOf: [cleanId]
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Builds the generated WeChat app entry that registers Taro's React App config.
|
|
157
|
+
* vite-plugin-taro omits Taro's generated pxTransform initialization because styles are handled by Tailwind.
|
|
158
|
+
*
|
|
159
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/app.ts#L54-L63
|
|
160
|
+
*/
|
|
161
|
+
export function createWxAppEntry(context) {
|
|
162
|
+
const wechatAppConfigCode = JSON.stringify(context.appConfig);
|
|
163
|
+
return `import { createReactApp, ReactDOM } from 'vite-plugin-taro/shim/wx'
|
|
164
|
+
import React from 'react'
|
|
165
|
+
import AppComponent from '${context.appComponentImport}'
|
|
166
|
+
|
|
167
|
+
const appConfig = ${wechatAppConfigCode}
|
|
168
|
+
App(createReactApp(AppComponent, React, ReactDOM, appConfig))
|
|
169
|
+
`;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Builds a generated WeChat page entry that registers Taro's Page config.
|
|
173
|
+
*
|
|
174
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/page.ts#L52-L78
|
|
175
|
+
*/
|
|
176
|
+
export function createWxPageEntry(pageOption) {
|
|
177
|
+
const wechatPageConfigCode = JSON.stringify(pageOption.config);
|
|
178
|
+
const pageComponentImport = createPageComponentImport(pageOption.path);
|
|
179
|
+
return `import { createPageConfig } from 'vite-plugin-taro/shim/wx'
|
|
180
|
+
import PageComponent from '${pageComponentImport}'
|
|
181
|
+
|
|
182
|
+
const pageConfig = ${wechatPageConfigCode}
|
|
183
|
+
const taroPageConfig = createPageConfig(PageComponent, '${pageOption.path}', { root: { cn: [] } }, pageConfig)
|
|
184
|
+
if (PageComponent && PageComponent.behaviors) {
|
|
185
|
+
taroPageConfig.behaviors = (taroPageConfig.behaviors || []).concat(PageComponent.behaviors)
|
|
186
|
+
}
|
|
187
|
+
Page(taroPageConfig)
|
|
188
|
+
`;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Builds the generated JS companion for comp.wxml/comp.json. Without it WeChat
|
|
192
|
+
* can load recursive markup, but it will not have Taro's properties or `eh` event
|
|
193
|
+
* dispatch method.
|
|
194
|
+
*
|
|
195
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/template/comp.ts#L1-L4
|
|
196
|
+
*/
|
|
197
|
+
export function createWxCompEntry() {
|
|
198
|
+
return `import { createRecursiveComponentConfig } from 'vite-plugin-taro/shim/wx'
|
|
199
|
+
|
|
200
|
+
Component(createRecursiveComponentConfig())
|
|
201
|
+
`;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Creates Taro-style Mini Program template/config/style companion files.
|
|
205
|
+
*
|
|
206
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-weapp/src/program.ts#L33-L55
|
|
207
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1198-L1311
|
|
208
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1346-L1390
|
|
209
|
+
*/
|
|
210
|
+
export function emitWechatAssets(emitter, bundle, context) {
|
|
211
|
+
if (context.target !== 'wx')
|
|
212
|
+
return;
|
|
213
|
+
for (const asset of createWechatAssets(bundle, context)) {
|
|
214
|
+
emitter.emitFile({ type: 'asset', fileName: asset.fileName, source: asset.source });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function createWechatAssets(bundle, context) {
|
|
218
|
+
const builder = createWechatTemplateBuilder();
|
|
219
|
+
return [
|
|
220
|
+
{ fileName: 'app.json', source: stringifyJsonAsset(context.appConfig) },
|
|
221
|
+
{ fileName: 'app.wxss', source: collectWechatBundleWxss(bundle) },
|
|
222
|
+
{ fileName: 'base.wxml', source: builder.buildTemplate(collectWechatTemplateComponentConfig(bundle)) },
|
|
223
|
+
{ fileName: 'utils.wxs', source: builder.buildXScript() },
|
|
224
|
+
{ fileName: 'comp.wxml', source: builder.buildBaseComponentTemplate('.wxml') },
|
|
225
|
+
{ fileName: 'comp.json', source: stringifyJsonAsset(createWechatCompJson()) },
|
|
226
|
+
{ fileName: 'project.config.json', source: stringifyJsonAsset(context.projectConfigJson) },
|
|
227
|
+
{ fileName: 'sitemap.json', source: stringifyJsonAsset(context.sitemapJson) },
|
|
228
|
+
...context.pages.flatMap((page) => [
|
|
229
|
+
{
|
|
230
|
+
fileName: `${page.path}.wxml`,
|
|
231
|
+
source: builder.buildPageTemplate(relativeWechatRootAssetFromPage(page.path, 'base.wxml'), {
|
|
232
|
+
content: page.config,
|
|
233
|
+
path: page.path
|
|
234
|
+
})
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
fileName: `${page.path}.json`,
|
|
238
|
+
source: stringifyJsonAsset({
|
|
239
|
+
...page.config,
|
|
240
|
+
usingComponents: {
|
|
241
|
+
comp: relativeWechatRootAssetFromPage(page.path, 'comp')
|
|
242
|
+
}
|
|
243
|
+
})
|
|
244
|
+
},
|
|
245
|
+
{ fileName: `${page.path}.wxss`, source: '' }
|
|
246
|
+
])
|
|
247
|
+
];
|
|
248
|
+
}
|
|
249
|
+
function createWechatTemplateBuilder() {
|
|
250
|
+
const wechatPlatform = new WechatPlatform({ helper: { recursiveMerge }, modifyWebpackChain() { }, registerPlatform() { } }, {}, {});
|
|
251
|
+
wechatPlatform.modifyTemplate({});
|
|
252
|
+
return wechatPlatform.template;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Computes a WeChat import path from a page file to a generated root asset.
|
|
256
|
+
*
|
|
257
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1270-L1298
|
|
258
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L74-L88
|
|
259
|
+
*/
|
|
260
|
+
function relativeWechatRootAssetFromPage(wechatPagePath, wechatRootAsset) {
|
|
261
|
+
const wechatPageDir = path.posix.dirname(wechatPagePath);
|
|
262
|
+
const relativePath = path.posix.relative(wechatPageDir, wechatRootAsset);
|
|
263
|
+
return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Builds Taro's template component include config from official defaults plus
|
|
267
|
+
* the component exports that Rolldown kept in the @tarojs/components bundle.
|
|
268
|
+
*
|
|
269
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/component.ts#L3-L8
|
|
270
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroComponentsExportsPlugin.ts#L83-L124
|
|
271
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroLoadChunksPlugin.ts#L165-L180
|
|
272
|
+
*/
|
|
273
|
+
function collectWechatTemplateComponentConfig(bundle) {
|
|
274
|
+
const wechatComponentConfig = {
|
|
275
|
+
includes: new Set([
|
|
276
|
+
'view',
|
|
277
|
+
'catch-view',
|
|
278
|
+
'static-view',
|
|
279
|
+
'pure-view',
|
|
280
|
+
'click-view',
|
|
281
|
+
'scroll-view',
|
|
282
|
+
'image',
|
|
283
|
+
'static-image',
|
|
284
|
+
'text',
|
|
285
|
+
'static-text'
|
|
286
|
+
]),
|
|
287
|
+
exclude: new Set(),
|
|
288
|
+
thirdPartyComponents: new Map(),
|
|
289
|
+
includeAll: false
|
|
290
|
+
};
|
|
291
|
+
const wechatComponentsModule = findBundleModule(bundle, taroWechatComponentsReactPath);
|
|
292
|
+
for (const item of wechatComponentsModule?.renderedExports ?? []) {
|
|
293
|
+
wechatComponentConfig.includes.add(toDashed(item));
|
|
294
|
+
}
|
|
295
|
+
return wechatComponentConfig;
|
|
296
|
+
}
|
|
297
|
+
function toDashed(s) {
|
|
298
|
+
return s.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Finds a module record inside the generated bundle by normalized module ID.
|
|
302
|
+
*
|
|
303
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroComponentsExportsPlugin.ts#L83-L124
|
|
304
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroLoadChunksPlugin.ts#L165-L180
|
|
305
|
+
*/
|
|
306
|
+
function findBundleModule(bundle, resolvedId) {
|
|
307
|
+
const normalizedResolvedId = normalizeModuleId(resolvedId);
|
|
308
|
+
for (const item of Object.values(bundle)) {
|
|
309
|
+
if (item.type !== 'chunk') {
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
const found = Object.entries(item.modules ?? {}).find(([id]) => normalizeModuleId(id) === normalizedResolvedId);
|
|
313
|
+
if (found) {
|
|
314
|
+
return found[1];
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Creates the JSON config for Taro's shared recursive component.
|
|
320
|
+
*
|
|
321
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1228-L1252
|
|
322
|
+
*/
|
|
323
|
+
function createWechatCompJson() {
|
|
324
|
+
return {
|
|
325
|
+
component: true,
|
|
326
|
+
styleIsolation: 'apply-shared',
|
|
327
|
+
// Taro's recursive template can nest <comp />, so the component references
|
|
328
|
+
// itself just like the official runner output.
|
|
329
|
+
usingComponents: { comp: './comp' }
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Converts a WeChat-emitted asset's source into text.
|
|
334
|
+
*/
|
|
335
|
+
function getWechatAssetSource(item) {
|
|
336
|
+
if (typeof item.source === 'string')
|
|
337
|
+
return item.source;
|
|
338
|
+
return item.source ? new TextDecoder().decode(item.source) : '';
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Flattens Vite-emitted CSS into app.wxss and removes the intermediate CSS asset.
|
|
342
|
+
* This is vite-plugin-taro's Vite equivalent of Taro Webpack's app/common style consolidation.
|
|
343
|
+
*
|
|
344
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1310-L1311
|
|
345
|
+
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1471-L1528
|
|
346
|
+
*/
|
|
347
|
+
function collectWechatBundleWxss(bundle) {
|
|
348
|
+
const wechatWxssChunks = [];
|
|
349
|
+
for (const [fileName, item] of Object.entries(bundle)) {
|
|
350
|
+
if (item.type !== 'asset' || !fileName.endsWith('.css'))
|
|
351
|
+
continue;
|
|
352
|
+
const source = getWechatAssetSource(item);
|
|
353
|
+
if (source)
|
|
354
|
+
wechatWxssChunks.push(source);
|
|
355
|
+
delete bundle[fileName];
|
|
356
|
+
}
|
|
357
|
+
return wechatWxssChunks.join('\n');
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Serializes generated Mini Program JSON assets; vite-plugin-taro pretty-prints non-prod output.
|
|
361
|
+
*/
|
|
362
|
+
function stringifyJsonAsset(value) {
|
|
363
|
+
return isProd ? JSON.stringify(value) : JSON.stringify(value, null, 2);
|
|
364
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
1
2
|
/**
|
|
2
3
|
* Derives a page component import from a Taro-style page path.
|
|
3
4
|
*
|
|
@@ -5,18 +6,26 @@
|
|
|
5
6
|
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/app.ts#L74-L90
|
|
6
7
|
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
|
|
7
8
|
*/
|
|
8
|
-
export
|
|
9
|
+
export function createPageComponentImport(pagePath) {
|
|
10
|
+
return toImportPath(`src/${pagePath}.tsx`);
|
|
11
|
+
}
|
|
9
12
|
/**
|
|
10
13
|
* Converts a local file path into an absolute ESM import path for Vite.
|
|
11
14
|
*/
|
|
12
|
-
export
|
|
15
|
+
export function toImportPath(filePath) {
|
|
16
|
+
return path.resolve(filePath);
|
|
17
|
+
}
|
|
13
18
|
/**
|
|
14
19
|
* Removes Rollup/Vite's internal virtual-module prefix before ID comparisons.
|
|
15
20
|
*/
|
|
16
|
-
export
|
|
21
|
+
export function stripVirtualPrefix(id) {
|
|
22
|
+
return id.startsWith('\0') ? id.slice(1) : id;
|
|
23
|
+
}
|
|
17
24
|
/**
|
|
18
25
|
* Uses Taro-style slash normalization, plus Vite query-string stripping for module IDs.
|
|
19
26
|
*
|
|
20
27
|
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L34
|
|
21
28
|
*/
|
|
22
|
-
export
|
|
29
|
+
export function normalizeModuleId(id) {
|
|
30
|
+
return id.replace(/\\/g, '/').replace(/\?.*$/, '');
|
|
31
|
+
}
|