vite-plugin-taro 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 felix
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # vite-plugin-taro
2
+
3
+ Vite 8 + React 19 plugin for building one React/Taro codebase for both:
4
+
5
+ - `wx`: WeChat Mini Program output.
6
+ - `h5`: Web output powered by Taro H5 runtime and router.
7
+
8
+ It wraps React 19-compatible Taro runtime packages, emits the generated app/page entries that Taro normally creates, and configures Vite/Rolldown, Tailwind CSS, and target-specific aliases for the selected target.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ pnpm add -D vite-plugin-taro vite
14
+ pnpm add react react-dom
15
+ ```
16
+
17
+ ## Vite usage
18
+
19
+ ```ts
20
+ import taro, { type TaroTarget } from 'vite-plugin-taro/vite'
21
+ import { defineConfig, loadEnv } from 'vite'
22
+
23
+ export default defineConfig(({ mode }) => {
24
+ const env = loadEnv(mode, process.cwd(), 'VITE_PLUGIN_TARO_')
25
+ const target = env.VITE_PLUGIN_TARO_TARGET as TaroTarget
26
+
27
+ return {
28
+ plugins: [
29
+ taro({
30
+ target,
31
+ app: 'src/app.ts',
32
+ pages: [{ path: 'pages/index/index', config: {} }],
33
+ appJson: {},
34
+ projectConfigJson: { appid: 'touristappid' },
35
+ sitemapJson: { rules: [{ action: 'allow', page: '*' }] }
36
+ })
37
+ ]
38
+ }
39
+ })
40
+ ```
41
+
42
+ Application code should usually import only `vite-plugin-taro/components` and `vite-plugin-taro/taro`.
43
+
44
+ ## Package exports
45
+
46
+ | Import | Purpose |
47
+ | --- | --- |
48
+ | `vite-plugin-taro` | Default Vite plugin and public plugin types. |
49
+ | `vite-plugin-taro/vite` | Default Vite plugin and `TaroTarget`, `TaroPluginOptions`, `TaroPageOption` types. |
50
+ | `vite-plugin-taro/components` | Re-export of `@tarojs/components`. Use this in app code. |
51
+ | `vite-plugin-taro/taro` | Taro API facade. Use this instead of importing `@tarojs/taro` directly. |
52
+ | `vite-plugin-taro/shim/h5` | H5 runtime shim used by generated entries. |
53
+ | `vite-plugin-taro/shim/wx` | WeChat runtime shim used by generated entries. |
54
+
55
+ ## Conditional compilation
56
+
57
+ vite-plugin-taro strips inactive Taro-style conditional comment blocks before Vite parses source files. Supported files include TypeScript, JavaScript, JSX/TSX, CSS, Sass, Less, and Stylus.
58
+
59
+ ```ts
60
+ // #ifdef wx
61
+ console.log('WeChat only')
62
+ // #endif
63
+
64
+ // #ifdef h5
65
+ console.log('H5 only')
66
+ // #endif
67
+
68
+ // #if h5 && !wx
69
+ console.log('H5 expression')
70
+ // #elif wx
71
+ console.log('WeChat expression')
72
+ // #else
73
+ console.log('fallback')
74
+ // #endif
75
+ ```
76
+
77
+ Supported directives are `#ifdef`, `#ifndef`, `#if`, `#elif`, `#else`, and `#endif`. Expressions support simple target tokens with `!`, `&&`, and `||`.
78
+
79
+ ## Target output
80
+
81
+ ### `wx`
82
+
83
+ vite-plugin-taro configures Rolldown for WeChat-compatible CommonJS chunks and emits Mini Program assets including:
84
+
85
+ - `app.js`, `app.json`, `app.wxss`.
86
+ - Page `*.js`, `*.json`, `*.wxml`, and `*.wxss` files.
87
+ - Shared Taro recursive template assets: `base.wxml`, `comp.js`, `comp.json`, `comp.wxml`, `utils.wxs`.
88
+ - `project.config.json` and `sitemap.json`.
89
+
90
+ ### `h5`
91
+
92
+ vite-plugin-taro injects a virtual module into `index.html`, creates Taro H5 route records from `pages`, and mounts the app with Taro's hash-history router.
93
+
94
+ ## React 19 compatibility
95
+
96
+ Taro 4.2's official React runtime targets React 18. vite-plugin-taro depends on two small React 19-compatible runtime packages generated from the official Taro npm tarballs plus vite-plugin-taro's local patch files:
97
+
98
+ - `vite-plugin-taro-react`
99
+ - `vite-plugin-taro-plugin-framework-react`
100
+
101
+ In the workspace these are referenced with pnpm workspace aliases:
102
+
103
+ ```json
104
+ {
105
+ "@tarojs/react": "workspace:vite-plugin-taro-react@*",
106
+ "@tarojs/plugin-framework-react": "workspace:vite-plugin-taro-plugin-framework-react@*"
107
+ }
108
+ ```
109
+
110
+ When packed/published by pnpm, those become npm aliases to the published patched packages. vite-plugin-taro source can keep importing the upstream Taro specifiers while users receive the patched React 19-compatible packages automatically.
111
+
112
+ Run `pnpm prepare:taro` to regenerate the patched packages from upstream tarballs. Publish those runtime packages before publishing `vite-plugin-taro`.
113
+
114
+ ## Publishing
115
+
116
+ ```sh
117
+ pnpm install
118
+ pnpm prepare:taro
119
+ pnpm --filter vite-plugin-taro-react pack --dry-run
120
+ pnpm --filter vite-plugin-taro-plugin-framework-react pack --dry-run
121
+ pnpm --filter vite-plugin-taro typecheck
122
+ pnpm --filter vite-plugin-taro build
123
+ pnpm --filter vite-plugin-taro pack:dry
124
+ ```
125
+
126
+ The package publishes built ESM JavaScript and `.d.ts` files from `dist`.
@@ -0,0 +1 @@
1
+ export * from '@tarojs/components';
@@ -0,0 +1 @@
1
+ export * from "@tarojs/components";
@@ -0,0 +1,2 @@
1
+ import Taro = require('@tarojs/taro')
2
+ export = Taro
@@ -0,0 +1,10 @@
1
+ import { hooks } from "@tarojs/runtime";
2
+ import Taro from "@tarojs/taro";
3
+ export * from "@tarojs/taro";
4
+ //#region src/public/taro.ts
5
+ if (hooks.isExist("initNativeApi")) hooks.call("initNativeApi", Taro);
6
+ var taro_default = Taro;
7
+ //#endregion
8
+ export { taro_default as default };
9
+
10
+ //# sourceMappingURL=taro.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"taro.js","names":[],"sources":["../../src/public/taro.ts"],"sourcesContent":["import { hooks } from '@tarojs/runtime'\nimport Taro from '@tarojs/taro'\n\nif (hooks.isExist('initNativeApi')) {\n hooks.call('initNativeApi', Taro)\n}\n\n// @ts-expect-error @tarojs/taro declares export= types, but vite-plugin-taro target aliases expose runtime named exports.\nexport * from '@tarojs/taro'\nexport default Taro\n"],"mappings":";;;;AAGA,IAAI,MAAM,QAAQ,eAAe,GAC7B,MAAM,KAAK,iBAAiB,IAAI;AAKpC,IAAA,eAAe"}
@@ -0,0 +1,3 @@
1
+ export { createReactApp } from '@tarojs/plugin-framework-react/dist/runtime';
2
+ export { createBrowserHistory, createHashHistory, createRouter, handleAppMount } from '@tarojs/router';
3
+ export { window } from '@tarojs/runtime';
@@ -0,0 +1,6 @@
1
+ import { window } from "@tarojs/runtime";
2
+ import "@tarojs/plugin-platform-h5/dist/runtime";
3
+ import "@tarojs/components/global.css";
4
+ import { createReactApp } from "@tarojs/plugin-framework-react/dist/runtime";
5
+ import { createBrowserHistory, createHashHistory, createRouter, handleAppMount } from "@tarojs/router";
6
+ export { createBrowserHistory, createHashHistory, createReactApp, createRouter, handleAppMount, window };
@@ -0,0 +1,3 @@
1
+ export { createReactApp } from '@tarojs/plugin-framework-react/dist/runtime';
2
+ export { default as ReactDOM } from '@tarojs/react';
3
+ export { createPageConfig, createRecursiveComponentConfig } from '@tarojs/runtime';
@@ -0,0 +1,5 @@
1
+ import { createPageConfig, createRecursiveComponentConfig } from "@tarojs/runtime";
2
+ import { createReactApp } from "@tarojs/plugin-framework-react/dist/runtime";
3
+ import "@tarojs/plugin-platform-weapp/dist/runtime.js";
4
+ import ReactDOM from "@tarojs/react";
5
+ export { ReactDOM, createPageConfig, createReactApp, createRecursiveComponentConfig };
@@ -0,0 +1,2 @@
1
+ export declare const isProd: boolean;
2
+ export declare const nodeRequire: NodeJS.Require;
@@ -0,0 +1,8 @@
1
+ import { Plugin } from 'vite';
2
+ import { TaroBuildContext } from './types.js';
3
+ /**
4
+ * Applies Taro-style conditional compilation comments before Vite parses source files.
5
+ *
6
+ * Mirrors Taro's CSS #ifdef/#ifndef handling, generalized before Vite parses code.
7
+ */
8
+ export declare function createTaroConditionalDirectivePlugin(context: TaroBuildContext): Plugin;
@@ -0,0 +1,3 @@
1
+ import { PluginOption } from 'vite';
2
+ import { TaroBuildContext } from './types.js';
3
+ export declare function createTailwindcssPlugins(context: TaroBuildContext): PluginOption[];
@@ -0,0 +1,31 @@
1
+ import { HtmlTagDescriptor, PluginOption, UserConfig } from 'vite';
2
+ import { TaroBuildContext } from '../types.js';
3
+ /**
4
+ * Checks whether an id belongs to an H5 virtual module.
5
+ */
6
+ export declare function isH5VirtualModuleId(id: string): boolean;
7
+ /**
8
+ * Loads generated source for H5 virtual modules.
9
+ */
10
+ export declare function loadH5VirtualModule(cleanId: string, context: TaroBuildContext): string | undefined;
11
+ /**
12
+ * Configures the Vite pieces needed for Taro H5 resolve/runtime behavior.
13
+ */
14
+ export declare function createH5ViteConfig(): UserConfig;
15
+ /**
16
+ * Creates H5-only support plugins used before the target emitter runs.
17
+ *
18
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-h5/src/program.ts#L219-L249
19
+ */
20
+ export declare function createH5SupportPlugins(): PluginOption[];
21
+ /**
22
+ * Injects vite-plugin-taro's generated Web entry into Vite's HTML shell.
23
+ */
24
+ export declare function createWebIndexHtmlTags(context: TaroBuildContext): HtmlTagDescriptor[] | undefined;
25
+ /**
26
+ * Builds the generated Web entry around Taro's official Web router/runtime APIs.
27
+ * vite-plugin-taro omits Taro's generated pxTransform initialization because styles are handled by Tailwind.
28
+ *
29
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L120-L150
30
+ */
31
+ export declare function createWebEntry(context: TaroBuildContext): string;
@@ -0,0 +1,74 @@
1
+ import { UserConfig } from 'vite';
2
+ import { TaroBuildContext, TaroPageOption } from '../types.js';
3
+ /**
4
+ * Checks whether an id belongs to a wx virtual module.
5
+ */
6
+ export declare function isWxVirtualModuleId(id: string): boolean;
7
+ export declare function loadWxVirtualModule(cleanId: string, context: TaroBuildContext): string | undefined;
8
+ /**
9
+ * Configures wx target entry, output, and chunk layout.
10
+ */
11
+ export declare function createWxViteConfig(context: TaroBuildContext): UserConfig;
12
+ type WechatAssetSource = string | Uint8Array;
13
+ type WechatBundleModule = {
14
+ renderedExports?: string[];
15
+ };
16
+ type WechatBundleItem = {
17
+ type: 'asset' | 'chunk';
18
+ source?: WechatAssetSource;
19
+ modules?: Record<string, WechatBundleModule>;
20
+ };
21
+ type WechatBundle = Record<string, WechatBundleItem>;
22
+ type WechatChunkEmitter = {
23
+ emitFile(chunk: {
24
+ type: 'chunk';
25
+ id: string;
26
+ fileName: string;
27
+ implicitlyLoadedAfterOneOf: string[];
28
+ }): string;
29
+ };
30
+ type WechatAssetEmitter = {
31
+ emitFile(asset: {
32
+ type: 'asset';
33
+ fileName: string;
34
+ source: WechatAssetSource;
35
+ }): string;
36
+ };
37
+ /**
38
+ * Emits page and component chunks like Taro Webpack's MiniPlugin generated entries.
39
+ *
40
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L228-L243
41
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L743-L777
42
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/TaroSingleEntryPlugin.ts#L18-L38
43
+ */
44
+ export declare function emitWechatImplicitChunksForVirtualApp(emitter: WechatChunkEmitter, context: TaroBuildContext, cleanId: string): void;
45
+ /**
46
+ * Builds the generated WeChat app entry that registers Taro's React App config.
47
+ * vite-plugin-taro omits Taro's generated pxTransform initialization because styles are handled by Tailwind.
48
+ *
49
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/app.ts#L54-L63
50
+ */
51
+ export declare function createWxAppEntry(context: TaroBuildContext): string;
52
+ /**
53
+ * Builds a generated WeChat page entry that registers Taro's Page config.
54
+ *
55
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/page.ts#L52-L78
56
+ */
57
+ export declare function createWxPageEntry(pageOption: TaroPageOption): string;
58
+ /**
59
+ * Builds the generated JS companion for comp.wxml/comp.json. Without it WeChat
60
+ * can load recursive markup, but it will not have Taro's properties or `eh` event
61
+ * dispatch method.
62
+ *
63
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/template/comp.ts#L1-L4
64
+ */
65
+ export declare function createWxCompEntry(): string;
66
+ /**
67
+ * Creates Taro-style Mini Program template/config/style companion files.
68
+ *
69
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-platform-weapp/src/program.ts#L33-L55
70
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1198-L1311
71
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L1346-L1390
72
+ */
73
+ export declare function emitWechatAssets(emitter: WechatAssetEmitter, bundle: WechatBundle, context: TaroBuildContext): void;
74
+ export {};
@@ -0,0 +1,7 @@
1
+ import { PluginOption } from 'vite';
2
+ import { TaroPluginOptions } from './types.js';
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 taro(options: TaroPluginOptions): PluginOption[];
@@ -0,0 +1,38 @@
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 TaroTarget = 'wx' | 'h5';
5
+ /** Describes one React-backed page shared by WeChat Mini Program and Web builds. */
6
+ export type TaroPageOption = {
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 TaroPluginOptions {
18
+ /** Active target for this Vite invocation. */
19
+ target: TaroTarget;
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: TaroPageOption[];
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 TaroBuildContext = {
32
+ target: TaroTarget;
33
+ appComponentImport: string;
34
+ pages: TaroPageOption[];
35
+ appConfig: JsonObject;
36
+ projectConfigJson: JsonObject;
37
+ sitemapJson: JsonObject;
38
+ };
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Derives a page component import from a Taro-style page path.
3
+ *
4
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L660-L668
5
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/app.ts#L74-L90
6
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
7
+ */
8
+ export declare function createPageComponentImport(pagePath: string): string;
9
+ /**
10
+ * Converts a local file path into an absolute ESM import path for Vite.
11
+ */
12
+ export declare function toImportPath(filePath: string): string;
13
+ /**
14
+ * Removes Rollup/Vite's internal virtual-module prefix before ID comparisons.
15
+ */
16
+ export declare function stripVirtualPrefix(id: string): string;
17
+ /**
18
+ * Uses Taro-style slash normalization, plus Vite query-string stripping for module IDs.
19
+ *
20
+ * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L34
21
+ */
22
+ export declare function normalizeModuleId(id: string): string;
package/dist/vite.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default } from './vite/taro.js';
2
+ export type { TaroPageOption, TaroPluginOptions, TaroTarget } from './vite/types.js';