vite-plugin-taro 0.2.1 → 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.
Files changed (122) hide show
  1. package/README.md +83 -106
  2. package/README.zh.md +83 -104
  3. package/dist/node/build-context.d.ts +25 -0
  4. package/dist/node/build-context.js +48 -0
  5. package/dist/node/css/css-pipeline.d.ts +21 -0
  6. package/dist/node/css/css-pipeline.js +129 -0
  7. package/dist/node/plugins/conditional-directives.d.ts +3 -0
  8. package/dist/node/plugins/conditional-directives.js +62 -0
  9. package/dist/node/plugins/taro-runtime.d.ts +5 -0
  10. package/dist/node/plugins/taro-runtime.js +18 -0
  11. package/dist/node/targets/h5/plugin.d.ts +6 -0
  12. package/dist/node/targets/h5/plugin.js +117 -0
  13. package/dist/node/targets/h5/virtual-modules.d.ts +5 -0
  14. package/dist/node/targets/h5/virtual-modules.js +60 -0
  15. package/dist/node/targets/wx/companion-assets.d.ts +18 -0
  16. package/dist/node/targets/wx/companion-assets.js +121 -0
  17. package/dist/node/targets/wx/dev-server/development-session.d.ts +31 -0
  18. package/dist/node/targets/wx/dev-server/development-session.js +202 -0
  19. package/dist/node/targets/wx/dev-server/full-build-scheduler.d.ts +14 -0
  20. package/dist/node/targets/wx/dev-server/full-build-scheduler.js +60 -0
  21. package/dist/node/targets/wx/dev-server/js-utils.d.ts +5 -0
  22. package/dist/node/targets/wx/dev-server/js-utils.js +28 -0
  23. package/dist/node/targets/wx/dev-server/output.d.ts +19 -0
  24. package/dist/node/targets/wx/dev-server/output.js +44 -0
  25. package/dist/node/targets/wx/dev-server/rolldown-runtime-source.d.ts +8 -0
  26. package/dist/node/targets/wx/dev-server/rolldown-runtime-source.js +95 -0
  27. package/dist/node/targets/wx/dev-server/update-server-state.d.ts +65 -0
  28. package/dist/node/targets/wx/dev-server/update-server-state.js +116 -0
  29. package/dist/node/targets/wx/dev-server/update-transport.d.ts +28 -0
  30. package/dist/node/targets/wx/dev-server/update-transport.js +212 -0
  31. package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.d.ts +44 -0
  32. package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.js +145 -0
  33. package/dist/node/targets/wx/development-files.d.ts +8 -0
  34. package/dist/node/targets/wx/development-files.js +8 -0
  35. package/dist/node/targets/wx/plugin.d.ts +6 -0
  36. package/dist/node/targets/wx/plugin.js +143 -0
  37. package/dist/node/targets/wx/react-refresh.d.ts +4 -0
  38. package/dist/node/targets/wx/react-refresh.js +38 -0
  39. package/dist/node/targets/wx/virtual-modules.d.ts +16 -0
  40. package/dist/node/targets/wx/virtual-modules.js +120 -0
  41. package/dist/node/utils/async.d.ts +8 -0
  42. package/dist/node/utils/async.js +16 -0
  43. package/dist/node/utils/filesystem.d.ts +13 -0
  44. package/dist/node/utils/filesystem.js +43 -0
  45. package/dist/node/utils/modules.d.ts +25 -0
  46. package/dist/{vite/utils.js → node/utils/modules.js} +10 -3
  47. package/dist/node/utils/packages.d.ts +2 -0
  48. package/dist/node/utils/packages.js +8 -0
  49. package/dist/node/vite-plugin.d.ts +4 -0
  50. package/dist/node/vite-plugin.js +33 -0
  51. package/dist/options.d.ts +21 -0
  52. package/dist/{shim/h5.js → runtime/h5/taro-runtime.d.ts} +3 -2
  53. package/dist/runtime/h5/taro-runtime.js +9 -0
  54. package/dist/runtime/taro/api.d.ts +5 -0
  55. package/dist/{virtual → runtime/taro}/api.js +3 -0
  56. package/dist/runtime/taro/components.d.ts +2 -0
  57. package/dist/runtime/taro/components.js +2 -0
  58. package/dist/runtime/wx/page-update.d.ts +6 -0
  59. package/dist/runtime/wx/page-update.js +157 -0
  60. package/dist/runtime/wx/taro-runtime.d.ts +5 -0
  61. package/dist/{shim/wx.js → runtime/wx/taro-runtime.js} +1 -0
  62. package/dist/runtime/wx/update-client-state.d.ts +68 -0
  63. package/dist/runtime/wx/update-client-state.js +84 -0
  64. package/dist/runtime/wx/update-client.d.ts +1 -0
  65. package/dist/runtime/wx/update-client.js +170 -0
  66. package/dist/vite.d.ts +2 -0
  67. package/dist/vite.js +1 -1
  68. package/package.json +17 -12
  69. package/src/node/build-context.ts +63 -0
  70. package/src/node/css/css-pipeline.ts +147 -0
  71. package/src/node/plugins/conditional-directives.ts +76 -0
  72. package/src/node/plugins/taro-runtime.ts +22 -0
  73. package/src/node/targets/h5/plugin.ts +135 -0
  74. package/src/node/targets/h5/virtual-modules.ts +71 -0
  75. package/src/node/targets/wx/companion-assets.ts +160 -0
  76. package/src/node/targets/wx/dev-server/development-session.ts +253 -0
  77. package/src/node/targets/wx/dev-server/full-build-scheduler.ts +62 -0
  78. package/src/node/targets/wx/dev-server/js-utils.ts +33 -0
  79. package/src/node/targets/wx/dev-server/output.ts +56 -0
  80. package/src/node/targets/wx/dev-server/rolldown-runtime-source.ts +95 -0
  81. package/src/node/targets/wx/dev-server/update-server-state.ts +183 -0
  82. package/src/node/targets/wx/dev-server/update-transport.ts +261 -0
  83. package/src/node/targets/wx/dev-server/vite-bundled-dev-adapter.ts +203 -0
  84. package/src/node/targets/wx/development-files.ts +11 -0
  85. package/src/node/targets/wx/plugin.ts +163 -0
  86. package/src/node/targets/wx/react-refresh.ts +51 -0
  87. package/src/node/targets/wx/virtual-modules.ts +152 -0
  88. package/src/node/utils/async.ts +19 -0
  89. package/src/node/utils/filesystem.ts +45 -0
  90. package/src/{vite/utils.ts → node/utils/modules.ts} +11 -3
  91. package/src/node/utils/packages.ts +10 -0
  92. package/src/node/vite-plugin.ts +41 -0
  93. package/src/options.ts +25 -0
  94. package/src/runtime/h5/taro-css.d.ts +3 -0
  95. package/src/runtime/h5/taro-runtime.ts +10 -0
  96. package/src/{virtual → runtime/taro}/api.ts +3 -0
  97. package/src/runtime/taro/components.ts +2 -0
  98. package/src/runtime/wx/page-update.ts +230 -0
  99. package/src/runtime/wx/react-refresh.d.ts +20 -0
  100. package/src/{shim/wx.ts → runtime/wx/taro-runtime.ts} +1 -0
  101. package/src/runtime/wx/update-client-state.ts +138 -0
  102. package/src/runtime/wx/update-client.ts +222 -0
  103. package/src/vite.ts +2 -2
  104. package/dist/virtual/components.js +0 -1
  105. package/dist/vite/constants.js +0 -8
  106. package/dist/vite/plugins.js +0 -186
  107. package/dist/vite/tailwindcss.js +0 -35
  108. package/dist/vite/targets/h5.js +0 -203
  109. package/dist/vite/targets/wx.js +0 -364
  110. package/dist/vite/virtual-modules.js +0 -13
  111. package/dist/vite/vite-plugin-taro.js +0 -80
  112. package/src/shim/h5.ts +0 -6
  113. package/src/virtual/components.ts +0 -1
  114. package/src/vite/constants.ts +0 -12
  115. package/src/vite/plugins.ts +0 -218
  116. package/src/vite/tailwindcss.ts +0 -41
  117. package/src/vite/targets/h5.ts +0 -231
  118. package/src/vite/targets/wx.ts +0 -438
  119. package/src/vite/types.ts +0 -48
  120. package/src/vite/virtual-modules.ts +0 -14
  121. package/src/vite/vite-plugin-taro.ts +0 -106
  122. /package/dist/{vite/types.js → options.js} +0 -0
@@ -1,364 +0,0 @@
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, wxShimImportPath } 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')));
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 ${JSON.stringify(wxShimImportPath)}
164
- import React from 'react'
165
- import AppComponent from ${JSON.stringify(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 ${JSON.stringify(wxShimImportPath)}
180
- import PageComponent from ${JSON.stringify(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 ${JSON.stringify(wxShimImportPath)}
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
- }
@@ -1,13 +0,0 @@
1
- import path from 'node:path';
2
- import { nodeRequire } from './constants.js';
3
- export const virtualTaroApiId = 'virtual:taro/api';
4
- export const virtualTaroComponentsId = 'virtual:taro/components';
5
- const packageRoot = path.dirname(nodeRequire.resolve('vite-plugin-taro/package.json'));
6
- const virtualTaroApiResolvedId = path.join(packageRoot, 'dist/virtual/api.js');
7
- const virtualTaroComponentsResolvedId = path.join(packageRoot, 'dist/virtual/components.js');
8
- export function resolvePublicVirtualModuleId(id) {
9
- if (id === virtualTaroApiId)
10
- return virtualTaroApiResolvedId;
11
- if (id === virtualTaroComponentsId)
12
- return virtualTaroComponentsResolvedId;
13
- }
@@ -1,80 +0,0 @@
1
- import { createVitePluginTaroConditionalDirectivePlugin } from './plugins.js';
2
- import { createTailwindcssPlugins } from './tailwindcss.js';
3
- import { createH5SupportPlugins, createH5ViteConfig, createWebIndexHtmlTags, isH5VirtualModuleId, loadH5VirtualModule } from './targets/h5.js';
4
- import { createWxViteConfig, emitWechatAssets, emitWechatImplicitChunksForVirtualApp, isWxVirtualModuleId, loadWxVirtualModule } from './targets/wx.js';
5
- import { stripVirtualPrefix, toImportPath } from './utils.js';
6
- import { resolvePublicVirtualModuleId } from './virtual-modules.js';
7
- /**
8
- * Creates the Vite/Rolldown plugin that emits either WeChat Mini Program files
9
- * or a Taro Web app using the official Taro runtime packages.
10
- */
11
- export default function vitePluginTaro(options) {
12
- const context = createVitePluginTaroBuildContext(options);
13
- return [
14
- createVitePluginTaroConditionalDirectivePlugin(context),
15
- ...createTargetSupportPlugins(context),
16
- ...createTailwindcssPlugins(context),
17
- createVitePluginTaroPlugin(context)
18
- ];
19
- }
20
- function createTargetSupportPlugins(context) {
21
- switch (context.target) {
22
- case 'h5':
23
- return createH5SupportPlugins();
24
- default:
25
- return [];
26
- }
27
- }
28
- /**
29
- * Creates the vite-plugin-taro plugin that emits H5 or Wx outputs.
30
- */
31
- function createVitePluginTaroPlugin(context) {
32
- return {
33
- name: 'vite-plugin-taro',
34
- enforce: 'post',
35
- /** Configures Vite/Rolldown for the active target. */
36
- config: {
37
- order: 'pre',
38
- handler: () => {
39
- return context.target === 'wx' ? createWxViteConfig(context) : createH5ViteConfig();
40
- }
41
- },
42
- /** Maps public virtual modules to real proxy files and marks generated entries as virtual modules. */
43
- resolveId(id) {
44
- return resolvePublicVirtualModuleId(id) ?? (isWxVirtualModuleId(id) || isH5VirtualModuleId(id) ? `\0${id}` : undefined);
45
- },
46
- /** Supplies source code for each virtual entry module. */
47
- load(id) {
48
- const cleanId = stripVirtualPrefix(id);
49
- emitWechatImplicitChunksForVirtualApp(this, context, cleanId);
50
- return loadWxVirtualModule(cleanId, context) ?? loadH5VirtualModule(cleanId, context);
51
- },
52
- /** Injects the generated Web entry into the app shell before Vite scans HTML imports. */
53
- transformIndexHtml: {
54
- order: 'pre',
55
- handler() {
56
- return createWebIndexHtmlTags(context);
57
- }
58
- },
59
- /** Emits the WeChat JSON/WXML/WXS/WXSS files that are not JS bundle chunks. */
60
- generateBundle(_, bundle) {
61
- emitWechatAssets(this, bundle, context);
62
- }
63
- };
64
- }
65
- /**
66
- * Normalizes user options into the shared data used by both target builders.
67
- */
68
- function createVitePluginTaroBuildContext(options) {
69
- return {
70
- target: options.target,
71
- appComponentImport: toImportPath(options.app),
72
- pages: options.pages,
73
- appConfig: {
74
- ...options.appJson,
75
- pages: options.pages.map((page) => page.path)
76
- },
77
- projectConfigJson: options.projectConfigJson,
78
- sitemapJson: options.sitemapJson
79
- };
80
- }
package/src/shim/h5.ts DELETED
@@ -1,6 +0,0 @@
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'
@@ -1 +0,0 @@
1
- export * from '@tarojs/components'
@@ -1,12 +0,0 @@
1
- import { createRequire } from 'node:module'
2
- import path from 'node:path'
3
- import { toImportPath } from './utils.ts'
4
-
5
- export const isProd = process.env.NODE_ENV === 'production'
6
-
7
- export const nodeRequire = createRequire(import.meta.url)
8
-
9
- const packageRoot = path.dirname(nodeRequire.resolve('vite-plugin-taro/package.json'))
10
-
11
- export const h5ShimImportPath = toImportPath(path.join(packageRoot, 'dist/shim/h5.js'))
12
- export const wxShimImportPath = toImportPath(path.join(packageRoot, 'dist/shim/wx.js'))