vite-plugin-taro 0.0.3 → 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.
@@ -0,0 +1,48 @@
1
+ /** Plain JSON object emitted into Mini Program/Web config payloads. */
2
+ export type JsonObject = Record<string, unknown>
3
+
4
+ /** Build target handled by this plugin. */
5
+ export type VitePluginTaroTarget = 'wx' | 'h5'
6
+
7
+ /** Describes one React-backed page shared by WeChat Mini Program and Web builds. */
8
+ export type VitePluginTaroPageOption = {
9
+ /**
10
+ * Page route and output path, without file extension.
11
+ * Example: "pages/index/index" emits pages/index/index.{js,json,wxml,wxss}
12
+ * for WeChat and becomes the Web router path.
13
+ */
14
+ path: string
15
+
16
+ /** Page JSON config merged into WeChat JSON and Web route config. */
17
+ config: JsonObject
18
+ }
19
+
20
+ /** Required build inputs for the custom Vite/Rolldown Taro renderer plugin. */
21
+ export interface VitePluginTaroOptions {
22
+ /** Active target for this Vite invocation. */
23
+ target: VitePluginTaroTarget
24
+
25
+ /** Source file that default-exports the root React app component. */
26
+ app: string
27
+
28
+ /** Ordered page list; also becomes app.json.pages and Web route order. */
29
+ pages: VitePluginTaroPageOption[]
30
+
31
+ /** Base app.json content. Its pages field is overwritten from options.pages. */
32
+ appJson: JsonObject
33
+
34
+ /** project.config.json content emitted at the Mini Program root. */
35
+ projectConfigJson: JsonObject
36
+
37
+ /** sitemap.json content emitted at the Mini Program root. */
38
+ sitemapJson: JsonObject
39
+ }
40
+
41
+ export type VitePluginTaroBuildContext = {
42
+ target: VitePluginTaroTarget
43
+ appComponentImport: string
44
+ pages: VitePluginTaroPageOption[]
45
+ appConfig: JsonObject
46
+ projectConfigJson: JsonObject
47
+ sitemapJson: JsonObject
48
+ }
@@ -0,0 +1,35 @@
1
+ import path from 'node:path'
2
+
3
+ /**
4
+ * Derives a page component import from a Taro-style page path.
5
+ *
6
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L660-L668
7
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/app.ts#L74-L90
8
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
9
+ */
10
+ export function createPageComponentImport(pagePath: string): string {
11
+ return toImportPath(`src/${pagePath}.tsx`)
12
+ }
13
+
14
+ /**
15
+ * Converts a local file path into an absolute ESM import path for Vite.
16
+ */
17
+ export function toImportPath(filePath: string): string {
18
+ return path.resolve(filePath)
19
+ }
20
+
21
+ /**
22
+ * Removes Rollup/Vite's internal virtual-module prefix before ID comparisons.
23
+ */
24
+ export function stripVirtualPrefix(id: string): string {
25
+ return id.startsWith('\0') ? id.slice(1) : id
26
+ }
27
+
28
+ /**
29
+ * Uses Taro-style slash normalization, plus Vite query-string stripping for module IDs.
30
+ *
31
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L34
32
+ */
33
+ export function normalizeModuleId(id: string): string {
34
+ return id.replace(/\\/g, '/').replace(/\?.*$/, '')
35
+ }
@@ -0,0 +1,105 @@
1
+ import type { Plugin, PluginOption, UserConfig } from 'vite'
2
+ import { createVitePluginTaroConditionalDirectivePlugin } from './plugins.ts'
3
+ import { createTailwindcssPlugins } from './tailwindcss.ts'
4
+ import {
5
+ createH5SupportPlugins,
6
+ createH5ViteConfig,
7
+ createWebIndexHtmlTags,
8
+ isH5VirtualModuleId,
9
+ loadH5VirtualModule
10
+ } from './targets/h5.ts'
11
+ import {
12
+ createWxViteConfig,
13
+ emitWechatAssets,
14
+ emitWechatImplicitChunksForVirtualApp,
15
+ isWxVirtualModuleId,
16
+ loadWxVirtualModule
17
+ } from './targets/wx.ts'
18
+ import type { VitePluginTaroBuildContext, VitePluginTaroOptions } from './types.ts'
19
+ import { stripVirtualPrefix, toImportPath } from './utils.ts'
20
+
21
+ /**
22
+ * Creates the Vite/Rolldown plugin that emits either WeChat Mini Program files
23
+ * or a Taro Web app using the official Taro runtime packages.
24
+ */
25
+ export default function vitePluginTaro(options: VitePluginTaroOptions): PluginOption[] {
26
+ const context = createVitePluginTaroBuildContext(options)
27
+
28
+ return [
29
+ createVitePluginTaroConditionalDirectivePlugin(context),
30
+ ...createTargetSupportPlugins(context),
31
+ ...createTailwindcssPlugins(context),
32
+ createVitePluginTaroPlugin(context)
33
+ ]
34
+ }
35
+
36
+ function createTargetSupportPlugins(context: VitePluginTaroBuildContext): PluginOption[] {
37
+ switch (context.target) {
38
+ case 'h5':
39
+ return createH5SupportPlugins()
40
+ default:
41
+ return []
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Creates the vite-plugin-taro plugin that emits H5 or Wx outputs.
47
+ */
48
+ function createVitePluginTaroPlugin(context: VitePluginTaroBuildContext): Plugin {
49
+ return {
50
+ name: 'vite-plugin-taro',
51
+ enforce: 'post',
52
+
53
+ /** Configures Vite/Rolldown for the active target. */
54
+ config: {
55
+ order: 'pre',
56
+ handler: (): UserConfig => {
57
+ return context.target === 'wx' ? createWxViteConfig(context) : createH5ViteConfig()
58
+ }
59
+ },
60
+
61
+ /** Marks generated app/page/component entries as virtual modules. */
62
+ resolveId(id) {
63
+ if (isWxVirtualModuleId(id) || isH5VirtualModuleId(id)) return `\0${id}`
64
+ },
65
+
66
+ /** Supplies source code for each virtual entry module. */
67
+ load(id) {
68
+ const cleanId = stripVirtualPrefix(id)
69
+
70
+ emitWechatImplicitChunksForVirtualApp(this, context, cleanId)
71
+
72
+ return loadWxVirtualModule(cleanId, context) ?? loadH5VirtualModule(cleanId, context)
73
+ },
74
+
75
+ /** Injects the generated Web entry into the app shell before Vite scans HTML imports. */
76
+ transformIndexHtml: {
77
+ order: 'pre',
78
+ handler() {
79
+ return createWebIndexHtmlTags(context)
80
+ }
81
+ },
82
+
83
+ /** Emits the WeChat JSON/WXML/WXS/WXSS files that are not JS bundle chunks. */
84
+ generateBundle(_, bundle) {
85
+ emitWechatAssets(this, bundle, context)
86
+ }
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Normalizes user options into the shared data used by both target builders.
92
+ */
93
+ function createVitePluginTaroBuildContext(options: VitePluginTaroOptions): VitePluginTaroBuildContext {
94
+ return {
95
+ target: options.target,
96
+ appComponentImport: toImportPath(options.app),
97
+ pages: options.pages,
98
+ appConfig: {
99
+ ...options.appJson,
100
+ pages: options.pages.map((page) => page.path)
101
+ },
102
+ projectConfigJson: options.projectConfigJson,
103
+ sitemapJson: options.sitemapJson
104
+ }
105
+ }
package/src/vite.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type { VitePluginTaroOptions, VitePluginTaroPageOption, VitePluginTaroTarget } from './vite/types.ts'
2
+ export { default } from './vite/vite-plugin-taro.ts'
package/client.d.ts DELETED
@@ -1,10 +0,0 @@
1
- declare module 'virtual:taro' {
2
- import Taro = require('@tarojs/taro')
3
-
4
- const taro: typeof Taro
5
- export default taro
6
- }
7
-
8
- declare module 'virtual:taro/components' {
9
- export * from '@tarojs/components'
10
- }
@@ -1,26 +0,0 @@
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
- }