vite-plugin-taro 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,366 @@
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 pluginSourcePath = 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/') ||
111
+ normalizedId.startsWith(`${pluginSourcePath}/`) ||
112
+ normalizedId.includes('virtual:taro'));
113
+ }
114
+ /**
115
+ * Names Rolldown's helper chunk like Taro webpack's runtime chunk.
116
+ *
117
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L96-L103
118
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L115-L117
119
+ */
120
+ function createWechatChunkFileName(chunkInfo) {
121
+ return `${chunkInfo.name === 'rolldown-runtime' ? 'runtime' : chunkInfo.name}.js`;
122
+ }
123
+ /**
124
+ * Checks whether a module is a third-party dependency chunk candidate.
125
+ *
126
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/webpack/MiniCombination.ts#L132-L139
127
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L36
128
+ */
129
+ function isNodeModule(id) {
130
+ return normalizeModuleId(id).includes('/node_modules/');
131
+ }
132
+ /**
133
+ * Emits page and component chunks like Taro Webpack's MiniPlugin generated entries.
134
+ *
135
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L228-L243
136
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L743-L777
137
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroSingleEntryPlugin.ts#L18-L38
138
+ */
139
+ export function emitWechatImplicitChunksForVirtualApp(emitter, context, cleanId) {
140
+ if (context.target !== 'wx' || cleanId !== virtualWxAppId)
141
+ return;
142
+ for (const page of context.pages) {
143
+ emitter.emitFile({
144
+ type: 'chunk',
145
+ id: `${virtualWxPagePrefix}${page.path}`,
146
+ fileName: `${page.path}.js`,
147
+ implicitlyLoadedAfterOneOf: [cleanId]
148
+ });
149
+ }
150
+ emitter.emitFile({
151
+ type: 'chunk',
152
+ id: virtualWxCompId,
153
+ fileName: 'comp.js',
154
+ implicitlyLoadedAfterOneOf: [cleanId]
155
+ });
156
+ }
157
+ /**
158
+ * Builds the generated WeChat app entry that registers Taro's React App config.
159
+ * vite-plugin-taro omits Taro's generated pxTransform initialization; apps should handle style transforms in their own Vite pipeline.
160
+ *
161
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/app.ts#L54-L63
162
+ */
163
+ export function createWxAppEntry(context) {
164
+ const wechatAppConfigCode = JSON.stringify(context.appConfig);
165
+ return `import { createReactApp, ReactDOM } from 'vite-plugin-taro/shim/wx'
166
+ import React from 'react'
167
+ import AppComponent from '${context.appComponentImport}'
168
+
169
+ const appConfig = ${wechatAppConfigCode}
170
+ App(createReactApp(AppComponent, React, ReactDOM, appConfig))
171
+ `;
172
+ }
173
+ /**
174
+ * Builds a generated WeChat page entry that registers Taro's Page config.
175
+ *
176
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/page.ts#L52-L78
177
+ */
178
+ export function createWxPageEntry(pageOption) {
179
+ const wechatPageConfigCode = JSON.stringify(pageOption.config);
180
+ const pageComponentImport = createPageComponentImport(pageOption.path);
181
+ return `import { createPageConfig } from 'vite-plugin-taro/shim/wx'
182
+ import PageComponent from '${pageComponentImport}'
183
+
184
+ const pageConfig = ${wechatPageConfigCode}
185
+ const taroPageConfig = createPageConfig(PageComponent, '${pageOption.path}', { root: { cn: [] } }, pageConfig)
186
+ if (PageComponent && PageComponent.behaviors) {
187
+ taroPageConfig.behaviors = (taroPageConfig.behaviors || []).concat(PageComponent.behaviors)
188
+ }
189
+ Page(taroPageConfig)
190
+ `;
191
+ }
192
+ /**
193
+ * Builds the generated JS companion for comp.wxml/comp.json. Without it WeChat
194
+ * can load recursive markup, but it will not have Taro's properties or `eh` event
195
+ * dispatch method.
196
+ *
197
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/template/comp.ts#L1-L4
198
+ */
199
+ export function createWxCompEntry() {
200
+ return `import { createRecursiveComponentConfig } from 'vite-plugin-taro/shim/wx'
201
+
202
+ Component(createRecursiveComponentConfig())
203
+ `;
204
+ }
205
+ /**
206
+ * Creates Taro-style Mini Program template/config/style companion files.
207
+ *
208
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-weapp/src/program.ts#L33-L55
209
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1198-L1311
210
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1346-L1390
211
+ */
212
+ export function emitWechatAssets(emitter, bundle, context) {
213
+ if (context.target !== 'wx')
214
+ return;
215
+ for (const asset of createWechatAssets(bundle, context)) {
216
+ emitter.emitFile({ type: 'asset', fileName: asset.fileName, source: asset.source });
217
+ }
218
+ }
219
+ function createWechatAssets(bundle, context) {
220
+ const builder = createWechatTemplateBuilder();
221
+ return [
222
+ { fileName: 'app.json', source: stringifyJsonAsset(context.appConfig) },
223
+ { fileName: 'app.wxss', source: collectWechatBundleWxss(bundle) },
224
+ { fileName: 'base.wxml', source: builder.buildTemplate(collectWechatTemplateComponentConfig(bundle)) },
225
+ { fileName: 'utils.wxs', source: builder.buildXScript() },
226
+ { fileName: 'comp.wxml', source: builder.buildBaseComponentTemplate('.wxml') },
227
+ { fileName: 'comp.json', source: stringifyJsonAsset(createWechatCompJson()) },
228
+ { fileName: 'project.config.json', source: stringifyJsonAsset(context.projectConfigJson) },
229
+ { fileName: 'sitemap.json', source: stringifyJsonAsset(context.sitemapJson) },
230
+ ...context.pages.flatMap((page) => [
231
+ {
232
+ fileName: `${page.path}.wxml`,
233
+ source: builder.buildPageTemplate(relativeWechatRootAssetFromPage(page.path, 'base.wxml'), {
234
+ content: page.config,
235
+ path: page.path
236
+ })
237
+ },
238
+ {
239
+ fileName: `${page.path}.json`,
240
+ source: stringifyJsonAsset({
241
+ ...page.config,
242
+ usingComponents: {
243
+ comp: relativeWechatRootAssetFromPage(page.path, 'comp')
244
+ }
245
+ })
246
+ },
247
+ { fileName: `${page.path}.wxss`, source: '' }
248
+ ])
249
+ ];
250
+ }
251
+ function createWechatTemplateBuilder() {
252
+ const wechatPlatform = new WechatPlatform({ helper: { recursiveMerge }, modifyWebpackChain() { }, registerPlatform() { } }, {}, {});
253
+ wechatPlatform.modifyTemplate({});
254
+ return wechatPlatform.template;
255
+ }
256
+ /**
257
+ * Computes a WeChat import path from a page file to a generated root asset.
258
+ *
259
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1270-L1298
260
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L74-L88
261
+ */
262
+ function relativeWechatRootAssetFromPage(wechatPagePath, wechatRootAsset) {
263
+ const wechatPageDir = path.posix.dirname(wechatPagePath);
264
+ const relativePath = path.posix.relative(wechatPageDir, wechatRootAsset);
265
+ return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
266
+ }
267
+ /**
268
+ * Builds Taro's template component include config from official defaults plus
269
+ * the component exports that Rolldown kept in the @tarojs/components bundle.
270
+ *
271
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/component.ts#L3-L8
272
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroComponentsExportsPlugin.ts#L83-L124
273
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroLoadChunksPlugin.ts#L165-L180
274
+ */
275
+ function collectWechatTemplateComponentConfig(bundle) {
276
+ const wechatComponentConfig = {
277
+ includes: new Set([
278
+ 'view',
279
+ 'catch-view',
280
+ 'static-view',
281
+ 'pure-view',
282
+ 'click-view',
283
+ 'scroll-view',
284
+ 'image',
285
+ 'static-image',
286
+ 'text',
287
+ 'static-text'
288
+ ]),
289
+ exclude: new Set(),
290
+ thirdPartyComponents: new Map(),
291
+ includeAll: false
292
+ };
293
+ const wechatComponentsModule = findBundleModule(bundle, taroWechatComponentsReactPath);
294
+ for (const item of wechatComponentsModule?.renderedExports ?? []) {
295
+ wechatComponentConfig.includes.add(toDashed(item));
296
+ }
297
+ return wechatComponentConfig;
298
+ }
299
+ function toDashed(s) {
300
+ return s.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
301
+ }
302
+ /**
303
+ * Finds a module record inside the generated bundle by normalized module ID.
304
+ *
305
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroComponentsExportsPlugin.ts#L83-L124
306
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroLoadChunksPlugin.ts#L165-L180
307
+ */
308
+ function findBundleModule(bundle, resolvedId) {
309
+ const normalizedResolvedId = normalizeModuleId(resolvedId);
310
+ for (const item of Object.values(bundle)) {
311
+ if (item.type !== 'chunk') {
312
+ continue;
313
+ }
314
+ const found = Object.entries(item.modules ?? {}).find(([id]) => normalizeModuleId(id) === normalizedResolvedId);
315
+ if (found) {
316
+ return found[1];
317
+ }
318
+ }
319
+ }
320
+ /**
321
+ * Creates the JSON config for Taro's shared recursive component.
322
+ *
323
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1228-L1252
324
+ */
325
+ function createWechatCompJson() {
326
+ return {
327
+ component: true,
328
+ styleIsolation: 'apply-shared',
329
+ // Taro's recursive template can nest <comp />, so the component references
330
+ // itself just like the official runner output.
331
+ usingComponents: { comp: './comp' }
332
+ };
333
+ }
334
+ /**
335
+ * Converts a WeChat-emitted asset's source into text.
336
+ */
337
+ function getWechatAssetSource(item) {
338
+ if (typeof item.source === 'string')
339
+ return item.source;
340
+ return item.source ? new TextDecoder().decode(item.source) : '';
341
+ }
342
+ /**
343
+ * Flattens Vite-emitted CSS into app.wxss and removes the intermediate CSS asset.
344
+ * This is vite-plugin-taro's Vite equivalent of Taro Webpack's app/common style consolidation.
345
+ *
346
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1310-L1311
347
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1471-L1528
348
+ */
349
+ function collectWechatBundleWxss(bundle) {
350
+ const wechatWxssChunks = [];
351
+ for (const [fileName, item] of Object.entries(bundle)) {
352
+ if (item.type !== 'asset' || !fileName.endsWith('.css'))
353
+ continue;
354
+ const source = getWechatAssetSource(item);
355
+ if (source)
356
+ wechatWxssChunks.push(source);
357
+ delete bundle[fileName];
358
+ }
359
+ return wechatWxssChunks.join('\n');
360
+ }
361
+ /**
362
+ * Serializes generated Mini Program JSON assets; vite-plugin-taro pretty-prints non-prod output.
363
+ */
364
+ function stringifyJsonAsset(value) {
365
+ return isProd ? JSON.stringify(value) : JSON.stringify(value, null, 2);
366
+ }
@@ -0,0 +1,81 @@
1
+ import { createTaroConditionalDirectivePlugin } from './plugins.js';
2
+ import { createH5SupportPlugins, createH5ViteConfig, createWebIndexHtmlTags, isH5VirtualModuleId, loadH5VirtualModule } from './targets/h5.js';
3
+ import { createWxViteConfig, emitWechatAssets, emitWechatImplicitChunksForVirtualApp, isWxVirtualModuleId, loadWxVirtualModule } from './targets/wx.js';
4
+ import { stripVirtualPrefix, toImportPath } from './utils.js';
5
+ import { isPublicVirtualModuleId, loadPublicVirtualModule } from './virtual.js';
6
+ /**
7
+ * Creates the Vite/Rolldown plugin that emits either WeChat Mini Program files
8
+ * or a Taro Web app using the official Taro runtime packages.
9
+ */
10
+ export default function taro(options) {
11
+ const context = createTaroBuildContext(options);
12
+ return [
13
+ createTaroConditionalDirectivePlugin(context),
14
+ ...createTargetSupportPlugins(context),
15
+ createTaroPlugin(context)
16
+ ];
17
+ }
18
+ function createTargetSupportPlugins(context) {
19
+ switch (context.target) {
20
+ case 'h5':
21
+ return createH5SupportPlugins();
22
+ default:
23
+ return [];
24
+ }
25
+ }
26
+ /**
27
+ * Creates the vite-plugin-taro plugin that emits H5 or Wx outputs.
28
+ */
29
+ function createTaroPlugin(context) {
30
+ return {
31
+ name: 'vite-plugin-taro',
32
+ enforce: 'post',
33
+ /** Configures Vite/Rolldown for the active target. */
34
+ config: {
35
+ order: 'pre',
36
+ handler: () => {
37
+ return context.target === 'wx' ? createWxViteConfig(context) : createH5ViteConfig();
38
+ }
39
+ },
40
+ /** Marks generated app/page/component entries as virtual modules. */
41
+ resolveId(id) {
42
+ if (isPublicVirtualModuleId(id) || isWxVirtualModuleId(id) || isH5VirtualModuleId(id))
43
+ return `\0${id}`;
44
+ },
45
+ /** Supplies source code for each virtual entry module. */
46
+ load(id) {
47
+ const cleanId = stripVirtualPrefix(id);
48
+ emitWechatImplicitChunksForVirtualApp(this, context, cleanId);
49
+ return (loadPublicVirtualModule(cleanId, context) ??
50
+ loadWxVirtualModule(cleanId, context) ??
51
+ loadH5VirtualModule(cleanId, context));
52
+ },
53
+ /** Injects the generated Web entry into the app shell before Vite scans HTML imports. */
54
+ transformIndexHtml: {
55
+ order: 'pre',
56
+ handler() {
57
+ return createWebIndexHtmlTags(context);
58
+ }
59
+ },
60
+ /** Emits the WeChat JSON/WXML/WXS/WXSS files that are not JS bundle chunks. */
61
+ generateBundle(_, bundle) {
62
+ emitWechatAssets(this, bundle, context);
63
+ }
64
+ };
65
+ }
66
+ /**
67
+ * Normalizes user options into the shared data used by both target builders.
68
+ */
69
+ function createTaroBuildContext(options) {
70
+ return {
71
+ target: options.target,
72
+ appComponentImport: toImportPath(options.app),
73
+ pages: options.pages,
74
+ appConfig: {
75
+ ...options.appJson,
76
+ pages: options.pages.map((page) => page.path)
77
+ },
78
+ projectConfigJson: options.projectConfigJson,
79
+ sitemapJson: options.sitemapJson
80
+ };
81
+ }
@@ -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 declare function createPageComponentImport(pagePath: string): string;
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 declare function toImportPath(filePath: string): string;
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 declare function stripVirtualPrefix(id: string): string;
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 declare function normalizeModuleId(id: string): string;
29
+ export function normalizeModuleId(id) {
30
+ return id.replace(/\\/g, '/').replace(/\?.*$/, '');
31
+ }
@@ -0,0 +1,26 @@
1
+ import { nodeRequire } from './constants.js';
2
+ import { normalizeModuleId } from './utils.js';
3
+ const virtualTaroId = 'virtual:taro';
4
+ const virtualTaroComponentsId = 'virtual:taro/components';
5
+ export function isPublicVirtualModuleId(id) {
6
+ return id === virtualTaroId || id === virtualTaroComponentsId;
7
+ }
8
+ export function loadPublicVirtualModule(id, context) {
9
+ if (id === virtualTaroId)
10
+ return createVirtualTaroModule(context);
11
+ if (id === virtualTaroComponentsId)
12
+ return "export * from '@tarojs/components'\n";
13
+ }
14
+ function createVirtualTaroModule(context) {
15
+ if (context.target === 'h5') {
16
+ return "export * from '@tarojs/taro'\nexport { default } from '@tarojs/taro'\n";
17
+ }
18
+ const taroPath = createResolvedImport('@tarojs/taro');
19
+ return `import Taro from ${taroPath}
20
+
21
+ export default Taro
22
+ `;
23
+ }
24
+ function createResolvedImport(id) {
25
+ return JSON.stringify(normalizeModuleId(nodeRequire.resolve(id)));
26
+ }