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.
Files changed (133) 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/{vite/utils.d.ts → node/utils/modules.d.ts} +2 -2
  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.d.ts → runtime/h5/taro-runtime.d.ts} +3 -1
  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/{shim/wx.d.ts → runtime/wx/taro-runtime.d.ts} +1 -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 -2
  67. package/dist/vite.js +1 -1
  68. package/package.json +15 -11
  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/shim/h5.js +0 -5
  105. package/dist/virtual/api.d.ts +0 -3
  106. package/dist/virtual/components.d.ts +0 -1
  107. package/dist/virtual/components.js +0 -1
  108. package/dist/vite/constants.d.ts +0 -4
  109. package/dist/vite/constants.js +0 -8
  110. package/dist/vite/plugins.d.ts +0 -8
  111. package/dist/vite/plugins.js +0 -186
  112. package/dist/vite/tailwindcss.d.ts +0 -3
  113. package/dist/vite/tailwindcss.js +0 -35
  114. package/dist/vite/targets/h5.d.ts +0 -31
  115. package/dist/vite/targets/h5.js +0 -201
  116. package/dist/vite/targets/wx.d.ts +0 -74
  117. package/dist/vite/targets/wx.js +0 -362
  118. package/dist/vite/types.d.ts +0 -38
  119. package/dist/vite/virtual-modules.d.ts +0 -3
  120. package/dist/vite/virtual-modules.js +0 -13
  121. package/dist/vite/vite-plugin-taro.d.ts +0 -7
  122. package/dist/vite/vite-plugin-taro.js +0 -80
  123. package/src/shim/h5.ts +0 -6
  124. package/src/virtual/components.ts +0 -1
  125. package/src/vite/constants.ts +0 -12
  126. package/src/vite/plugins.ts +0 -218
  127. package/src/vite/tailwindcss.ts +0 -41
  128. package/src/vite/targets/h5.ts +0 -229
  129. package/src/vite/targets/wx.ts +0 -436
  130. package/src/vite/types.ts +0 -48
  131. package/src/vite/virtual-modules.ts +0 -14
  132. package/src/vite/vite-plugin-taro.ts +0 -106
  133. /package/dist/{vite/types.js → options.js} +0 -0
@@ -1,362 +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
- ENABLE_ADJACENT_HTML: 'false',
92
- ENABLE_CLONE_NODE: 'false',
93
- ENABLE_CONTAINS: 'false',
94
- ENABLE_INNER_HTML: 'false',
95
- ENABLE_MUTATION_OBSERVER: 'false',
96
- ENABLE_SIZE_APIS: 'false',
97
- ENABLE_TEMPLATE_CONTENT: 'false'
98
- };
99
- }
100
- /**
101
- * Checks whether a module should live in the Taro/framework base chunk.
102
- * vite-plugin-taro support modules are kept with Taro so pages do not duplicate runtime facades.
103
- *
104
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L141-L144
105
- */
106
- function isWxTaroChunkModule(id) {
107
- const normalizedId = normalizeModuleId(id);
108
- return normalizedId.includes('/node_modules/@tarojs/') || normalizedId.startsWith(`${vitePluginTaroSourcePath}/`);
109
- }
110
- /**
111
- * Names Rolldown's helper chunk like Taro webpack's runtime chunk.
112
- *
113
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L96-L103
114
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L115-L117
115
- */
116
- function createWechatChunkFileName(chunkInfo) {
117
- return `${chunkInfo.name === 'rolldown-runtime' ? 'runtime' : chunkInfo.name}.js`;
118
- }
119
- /**
120
- * Checks whether a module is a third-party dependency chunk candidate.
121
- *
122
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L132-L139
123
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L36
124
- */
125
- function isNodeModule(id) {
126
- return normalizeModuleId(id).includes('/node_modules/');
127
- }
128
- /**
129
- * Emits page and component chunks like Taro Webpack's MiniPlugin generated entries.
130
- *
131
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L228-L243
132
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L743-L777
133
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroSingleEntryPlugin.ts#L18-L38
134
- */
135
- export function emitWechatImplicitChunksForVirtualApp(emitter, context, cleanId) {
136
- if (context.target !== 'wx' || cleanId !== virtualWxAppId)
137
- return;
138
- for (const page of context.pages) {
139
- emitter.emitFile({
140
- type: 'chunk',
141
- id: `${virtualWxPagePrefix}${page.path}`,
142
- fileName: `${page.path}.js`,
143
- implicitlyLoadedAfterOneOf: [cleanId]
144
- });
145
- }
146
- emitter.emitFile({
147
- type: 'chunk',
148
- id: virtualWxCompId,
149
- fileName: 'comp.js',
150
- implicitlyLoadedAfterOneOf: [cleanId]
151
- });
152
- }
153
- /**
154
- * Builds the generated WeChat app entry that registers Taro's React App config.
155
- * vite-plugin-taro omits Taro's generated pxTransform initialization because styles are handled by Tailwind.
156
- *
157
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/app.ts#L54-L63
158
- */
159
- export function createWxAppEntry(context) {
160
- const wechatAppConfigCode = JSON.stringify(context.appConfig);
161
- return `import { createReactApp, ReactDOM } from ${JSON.stringify(wxShimImportPath)}
162
- import React from 'react'
163
- import AppComponent from ${JSON.stringify(context.appComponentImport)}
164
-
165
- const appConfig = ${wechatAppConfigCode}
166
- App(createReactApp(AppComponent, React, ReactDOM, appConfig))
167
- `;
168
- }
169
- /**
170
- * Builds a generated WeChat page entry that registers Taro's Page config.
171
- *
172
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/page.ts#L52-L78
173
- */
174
- export function createWxPageEntry(pageOption) {
175
- const wechatPageConfigCode = JSON.stringify(pageOption.config);
176
- const pageComponentImport = createPageComponentImport(pageOption.path);
177
- return `import { createPageConfig } from ${JSON.stringify(wxShimImportPath)}
178
- import PageComponent from ${JSON.stringify(pageComponentImport)}
179
-
180
- const pageConfig = ${wechatPageConfigCode}
181
- const taroPageConfig = createPageConfig(PageComponent, '${pageOption.path}', { root: { cn: [] } }, pageConfig)
182
- if (PageComponent && PageComponent.behaviors) {
183
- taroPageConfig.behaviors = (taroPageConfig.behaviors || []).concat(PageComponent.behaviors)
184
- }
185
- Page(taroPageConfig)
186
- `;
187
- }
188
- /**
189
- * Builds the generated JS companion for comp.wxml/comp.json. Without it WeChat
190
- * can load recursive markup, but it will not have Taro's properties or `eh` event
191
- * dispatch method.
192
- *
193
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/template/comp.ts#L1-L4
194
- */
195
- export function createWxCompEntry() {
196
- return `import { createRecursiveComponentConfig } from ${JSON.stringify(wxShimImportPath)}
197
-
198
- Component(createRecursiveComponentConfig())
199
- `;
200
- }
201
- /**
202
- * Creates Taro-style Mini Program template/config/style companion files.
203
- *
204
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-weapp/src/program.ts#L33-L55
205
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1198-L1311
206
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1346-L1390
207
- */
208
- export function emitWechatAssets(emitter, bundle, context) {
209
- if (context.target !== 'wx')
210
- return;
211
- for (const asset of createWechatAssets(bundle, context)) {
212
- emitter.emitFile({ type: 'asset', fileName: asset.fileName, source: asset.source });
213
- }
214
- }
215
- function createWechatAssets(bundle, context) {
216
- const builder = createWechatTemplateBuilder();
217
- return [
218
- { fileName: 'app.json', source: stringifyJsonAsset(context.appConfig) },
219
- { fileName: 'app.wxss', source: collectWechatBundleWxss(bundle) },
220
- { fileName: 'base.wxml', source: builder.buildTemplate(collectWechatTemplateComponentConfig(bundle)) },
221
- { fileName: 'utils.wxs', source: builder.buildXScript() },
222
- { fileName: 'comp.wxml', source: builder.buildBaseComponentTemplate('.wxml') },
223
- { fileName: 'comp.json', source: stringifyJsonAsset(createWechatCompJson()) },
224
- { fileName: 'project.config.json', source: stringifyJsonAsset(context.projectConfigJson) },
225
- { fileName: 'sitemap.json', source: stringifyJsonAsset(context.sitemapJson) },
226
- ...context.pages.flatMap((page) => [
227
- {
228
- fileName: `${page.path}.wxml`,
229
- source: builder.buildPageTemplate(relativeWechatRootAssetFromPage(page.path, 'base.wxml'), {
230
- content: page.config,
231
- path: page.path
232
- })
233
- },
234
- {
235
- fileName: `${page.path}.json`,
236
- source: stringifyJsonAsset({
237
- ...page.config,
238
- usingComponents: {
239
- comp: relativeWechatRootAssetFromPage(page.path, 'comp')
240
- }
241
- })
242
- },
243
- { fileName: `${page.path}.wxss`, source: '' }
244
- ])
245
- ];
246
- }
247
- function createWechatTemplateBuilder() {
248
- const wechatPlatform = new WechatPlatform({ helper: { recursiveMerge }, modifyWebpackChain() { }, registerPlatform() { } }, {}, {});
249
- wechatPlatform.modifyTemplate({});
250
- return wechatPlatform.template;
251
- }
252
- /**
253
- * Computes a WeChat import path from a page file to a generated root asset.
254
- *
255
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1270-L1298
256
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L74-L88
257
- */
258
- function relativeWechatRootAssetFromPage(wechatPagePath, wechatRootAsset) {
259
- const wechatPageDir = path.posix.dirname(wechatPagePath);
260
- const relativePath = path.posix.relative(wechatPageDir, wechatRootAsset);
261
- return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
262
- }
263
- /**
264
- * Builds Taro's template component include config from official defaults plus
265
- * the component exports that Rolldown kept in the @tarojs/components bundle.
266
- *
267
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/component.ts#L3-L8
268
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroComponentsExportsPlugin.ts#L83-L124
269
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroLoadChunksPlugin.ts#L165-L180
270
- */
271
- function collectWechatTemplateComponentConfig(bundle) {
272
- const wechatComponentConfig = {
273
- includes: new Set([
274
- 'view',
275
- 'catch-view',
276
- 'static-view',
277
- 'pure-view',
278
- 'click-view',
279
- 'scroll-view',
280
- 'image',
281
- 'static-image',
282
- 'text',
283
- 'static-text'
284
- ]),
285
- exclude: new Set(),
286
- thirdPartyComponents: new Map(),
287
- includeAll: false
288
- };
289
- const wechatComponentsModule = findBundleModule(bundle, taroWechatComponentsReactPath);
290
- for (const item of wechatComponentsModule?.renderedExports ?? []) {
291
- wechatComponentConfig.includes.add(toDashed(item));
292
- }
293
- return wechatComponentConfig;
294
- }
295
- function toDashed(s) {
296
- return s.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
297
- }
298
- /**
299
- * Finds a module record inside the generated bundle by normalized module ID.
300
- *
301
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroComponentsExportsPlugin.ts#L83-L124
302
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroLoadChunksPlugin.ts#L165-L180
303
- */
304
- function findBundleModule(bundle, resolvedId) {
305
- const normalizedResolvedId = normalizeModuleId(resolvedId);
306
- for (const item of Object.values(bundle)) {
307
- if (item.type !== 'chunk') {
308
- continue;
309
- }
310
- const found = Object.entries(item.modules ?? {}).find(([id]) => normalizeModuleId(id) === normalizedResolvedId);
311
- if (found) {
312
- return found[1];
313
- }
314
- }
315
- }
316
- /**
317
- * Creates the JSON config for Taro's shared recursive component.
318
- *
319
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1228-L1252
320
- */
321
- function createWechatCompJson() {
322
- return {
323
- component: true,
324
- styleIsolation: 'apply-shared',
325
- // Taro's recursive template can nest <comp />, so the component references
326
- // itself just like the official runner output.
327
- usingComponents: { comp: './comp' }
328
- };
329
- }
330
- /**
331
- * Converts a WeChat-emitted asset's source into text.
332
- */
333
- function getWechatAssetSource(item) {
334
- if (typeof item.source === 'string')
335
- return item.source;
336
- return item.source ? new TextDecoder().decode(item.source) : '';
337
- }
338
- /**
339
- * Flattens Vite-emitted CSS into app.wxss and removes the intermediate CSS asset.
340
- * This is vite-plugin-taro's Vite equivalent of Taro Webpack's app/common style consolidation.
341
- *
342
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1310-L1311
343
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1471-L1528
344
- */
345
- function collectWechatBundleWxss(bundle) {
346
- const wechatWxssChunks = [];
347
- for (const [fileName, item] of Object.entries(bundle)) {
348
- if (item.type !== 'asset' || !fileName.endsWith('.css'))
349
- continue;
350
- const source = getWechatAssetSource(item);
351
- if (source)
352
- wechatWxssChunks.push(source);
353
- delete bundle[fileName];
354
- }
355
- return wechatWxssChunks.join('\n');
356
- }
357
- /**
358
- * Serializes generated Mini Program JSON assets; vite-plugin-taro pretty-prints non-prod output.
359
- */
360
- function stringifyJsonAsset(value) {
361
- return isProd ? JSON.stringify(value) : JSON.stringify(value, null, 2);
362
- }
@@ -1,38 +0,0 @@
1
- /** Plain JSON object emitted into Mini Program/Web config payloads. */
2
- export type JsonObject = Record<string, unknown>;
3
- /** Build target handled by this plugin. */
4
- export type VitePluginTaroTarget = 'wx' | 'h5';
5
- /** Describes one React-backed page shared by WeChat Mini Program and Web builds. */
6
- export type VitePluginTaroPageOption = {
7
- /**
8
- * Page route and output path, without file extension.
9
- * Example: "pages/index/index" emits pages/index/index.{js,json,wxml,wxss}
10
- * for WeChat and becomes the Web router path.
11
- */
12
- path: string;
13
- /** Page JSON config merged into WeChat JSON and Web route config. */
14
- config: JsonObject;
15
- };
16
- /** Required build inputs for the custom Vite/Rolldown Taro renderer plugin. */
17
- export interface VitePluginTaroOptions {
18
- /** Active target for this Vite invocation. */
19
- target: VitePluginTaroTarget;
20
- /** Source file that default-exports the root React app component. */
21
- app: string;
22
- /** Ordered page list; also becomes app.json.pages and Web route order. */
23
- pages: VitePluginTaroPageOption[];
24
- /** Base app.json content. Its pages field is overwritten from options.pages. */
25
- appJson: JsonObject;
26
- /** project.config.json content emitted at the Mini Program root. */
27
- projectConfigJson: JsonObject;
28
- /** sitemap.json content emitted at the Mini Program root. */
29
- sitemapJson: JsonObject;
30
- }
31
- export type VitePluginTaroBuildContext = {
32
- target: VitePluginTaroTarget;
33
- appComponentImport: string;
34
- pages: VitePluginTaroPageOption[];
35
- appConfig: JsonObject;
36
- projectConfigJson: JsonObject;
37
- sitemapJson: JsonObject;
38
- };
@@ -1,3 +0,0 @@
1
- export declare const virtualTaroApiId = "virtual:taro/api";
2
- export declare const virtualTaroComponentsId = "virtual:taro/components";
3
- export declare function resolvePublicVirtualModuleId(id: string): string | undefined;
@@ -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,7 +0,0 @@
1
- import type { PluginOption } from 'vite';
2
- import type { VitePluginTaroOptions } from './types.ts';
3
- /**
4
- * Creates the Vite/Rolldown plugin that emits either WeChat Mini Program files
5
- * or a Taro Web app using the official Taro runtime packages.
6
- */
7
- export default function vitePluginTaro(options: VitePluginTaroOptions): PluginOption[];
@@ -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'))