vite-plugin-taro 0.5.0 → 0.5.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.
package/README.en.md CHANGED
@@ -8,7 +8,15 @@
8
8
 
9
9
  Build WeChat Mini Program and Web apps with Vite 8, React 19, Taro 4, and Tailwind CSS v4.
10
10
 
11
- Documentation website: <https://vite-plugin-taro.netlify.app>
11
+ Website: <https://vpt.js.org>
12
+
13
+ ## Quick Start
14
+
15
+ ```sh
16
+ npm create vite-taro@latest my-app
17
+ ```
18
+
19
+ Continue with the [Quick Start guide](https://vpt.js.org/guides/quick-start/).
12
20
 
13
21
  ### React Hot Reload in WeChat DevTools
14
22
 
@@ -21,23 +29,15 @@ Documentation website: <https://vite-plugin-taro.netlify.app>
21
29
  - **Built on Taro, beyond Taro** Use Taro components and APIs without the old webpack pipeline.
22
30
  - **Skyline ready** Use WeChat Skyline rendering globally or per page.
23
31
 
24
- ## Create an app
25
-
26
- ```sh
27
- npm create vite-taro@latest my-app
28
- ```
29
-
30
- Continue with the [Quick Start guide](https://vite-plugin-taro.netlify.app/guides/quick-start/).
31
-
32
32
  ## Documentation
33
33
 
34
- - [Automatic subpackages](https://vite-plugin-taro.netlify.app/guides/automatic-subpackages/)
35
- - [Native WeChat components](https://vite-plugin-taro.netlify.app/guides/native-components/)
36
- - [Hot module replacement](https://vite-plugin-taro.netlify.app/guides/hot-module-replacement/)
37
- - [Skyline mode](https://vite-plugin-taro.netlify.app/guides/skyline-mode/)
38
- - [Migrate from Taro CLI](https://vite-plugin-taro.netlify.app/guides/migrate-from-taro/)
39
- - [Configuration reference](https://vite-plugin-taro.netlify.app/references/configuration/)
40
- - [Repository management](https://vite-plugin-taro.netlify.app/references/repository-management/)
34
+ - [Automatic subpackages](https://vpt.js.org/guides/automatic-subpackages/)
35
+ - [Native WeChat components](https://vpt.js.org/guides/native-components/)
36
+ - [Hot module replacement](https://vpt.js.org/guides/hot-module-replacement/)
37
+ - [Skyline mode](https://vpt.js.org/guides/skyline-mode/)
38
+ - [Migrate from Taro CLI](https://vpt.js.org/guides/migrate-from-taro/)
39
+ - [Configuration reference](https://vpt.js.org/references/configuration/)
40
+ - [Repository management](https://vpt.js.org/references/repository-management/)
41
41
 
42
42
  ## License
43
43
 
package/README.md CHANGED
@@ -8,7 +8,16 @@
8
8
 
9
9
  使用 Vite 8、React 19、Taro 4 和 Tailwind CSS v4 构建微信小程序与 Web 应用。
10
10
 
11
- 文档网站:<https://vite-plugin-taro.netlify.app>
11
+ 官网:<https://vpt.js.org>
12
+
13
+ ## 快速开始
14
+
15
+ ```sh
16
+ npm create vite-taro@latest my-app
17
+ ```
18
+
19
+ 接下来阅读[快速开始](https://vpt.js.org/guides/quick-start/)。
20
+
12
21
 
13
22
  ### 已支持微信开发者工具的 React 热更新
14
23
 
@@ -21,23 +30,15 @@
21
30
  - **基于 Taro,超越 Taro** 使用 Taro 组件和 API,摆脱旧式 webpack 链路。
22
31
  - **Skyline 就绪** 支持全局或按页面启用微信 Skyline 渲染模式。
23
32
 
24
- ## 创建应用
25
-
26
- ```sh
27
- npm create vite-taro@latest my-app
28
- ```
29
-
30
- 接下来阅读[快速开始](https://vite-plugin-taro.netlify.app/guides/quick-start/)。
31
-
32
33
  ## 文档
33
34
 
34
- - [全自动分包](https://vite-plugin-taro.netlify.app/guides/automatic-subpackages/)
35
- - [微信原生组件](https://vite-plugin-taro.netlify.app/guides/native-components/)
36
- - [开发者工具热更新](https://vite-plugin-taro.netlify.app/guides/hot-module-replacement/)
37
- - [Skyline 模式](https://vite-plugin-taro.netlify.app/guides/skyline-mode/)
38
- - [从 Taro CLI 迁移](https://vite-plugin-taro.netlify.app/guides/migrate-from-taro/)
39
- - [配置参考](https://vite-plugin-taro.netlify.app/references/configuration/)
40
- - [仓库维护](https://vite-plugin-taro.netlify.app/references/repository-management/)
35
+ - [全自动分包](https://vpt.js.org/guides/automatic-subpackages/)
36
+ - [微信原生组件](https://vpt.js.org/guides/native-components/)
37
+ - [开发者工具热更新](https://vpt.js.org/guides/hot-module-replacement/)
38
+ - [Skyline 模式](https://vpt.js.org/guides/skyline-mode/)
39
+ - [从 Taro CLI 迁移](https://vpt.js.org/guides/migrate-from-taro/)
40
+ - [配置参考](https://vpt.js.org/references/configuration/)
41
+ - [仓库维护](https://vpt.js.org/references/repository-management/)
41
42
 
42
43
  ## 许可证
43
44
 
@@ -1,4 +1,6 @@
1
1
  import type { Plugin } from 'vite';
2
+ import type { VitePluginTaroTarget } from '../../../options.ts';
3
+ /** Public facade used by transformed application API imports. */
2
4
  export declare const clientTaroApiId = "virtual:taro/api";
3
- /** Creates the target-neutral Taro facade plugin. */
4
- export declare function createClientTaroPlugin(): Plugin;
5
+ /** Creates the shared Taro facade backed by the selected target's API implementation. */
6
+ export declare function createClientTaroPlugin(target: VitePluginTaroTarget): Plugin;
@@ -1,19 +1,54 @@
1
- import { resolvePackageFile } from '../../utils/packages.js';
1
+ import { normalizeModuleId } from '../../utils/modules.js';
2
+ import { packageRequire, resolvePackageFile } from '../../utils/packages.js';
2
3
  import { clientTaroNativeId } from './constant.js';
4
+ import { injectTaroFrameworkApis } from './inject-taro-framework-apis.js';
5
+ /*
6
+ * Taro API resolution uses one public facade while avoiding a recursive `@tarojs/taro` import:
7
+ *
8
+ * 1. Application imports of `virtual:taro/api` or `@tarojs/taro` resolve to the physical `api.js` facade.
9
+ * 2. The facade itself imports `@tarojs/taro`; its importer identifies that request as the platform implementation.
10
+ * 3. H5 receives `@tarojs/plugin-platform-h5` APIs, while WX receives the generic `@tarojs/taro` implementation.
11
+ * 4. React's framework API loader transforms the facade, assigning lifecycle hooks such as `useLaunch` to the same
12
+ * platform object and exposing them as named exports.
13
+ *
14
+ * The importer-sensitive second step removes the need for another public-looking virtual module while retaining one
15
+ * facade object for platform APIs and framework lifecycles.
16
+ */
17
+ /** Public facade used by transformed application API imports. */
3
18
  export const clientTaroApiId = 'virtual:taro/api';
19
+ const clientTaroApiPath = resolvePackageFile('dist/runtime/client/taro/api.js');
20
+ const normalizedClientTaroApiPath = normalizeModuleId(clientTaroApiPath);
4
21
  const clientTaroComponentId = 'virtual:taro/components';
5
22
  const clientTaroModules = new Map([
6
- [clientTaroApiId, resolvePackageFile('dist/runtime/client/taro/api.js')],
23
+ [clientTaroApiId, clientTaroApiPath],
7
24
  [clientTaroComponentId, resolvePackageFile('dist/runtime/client/taro/component.js')],
8
25
  [clientTaroNativeId, resolvePackageFile('dist/runtime/client/taro/define-native-component.js')]
9
26
  ]);
10
- /** Creates the target-neutral Taro facade plugin. */
11
- export function createClientTaroPlugin() {
27
+ /** Creates the shared Taro facade backed by the selected target's API implementation. */
28
+ export function createClientTaroPlugin(target) {
29
+ const platformTaroPath = resolvePlatformTaroPath(target);
12
30
  return {
13
31
  name: 'vpt:client-taro',
14
32
  enforce: 'pre',
15
- resolveId(id) {
33
+ resolveId(id, importer) {
34
+ if (id === '@tarojs/taro') {
35
+ return isClientTaroFacade(importer) ? platformTaroPath : clientTaroApiPath;
36
+ }
16
37
  return clientTaroModules.get(id);
38
+ },
39
+ // Taro's framework loader transforms source code, so apply it only to the shared physical facade.
40
+ transform(code, id) {
41
+ if (normalizeModuleId(id) === normalizedClientTaroApiPath) {
42
+ return injectTaroFrameworkApis(code);
43
+ }
17
44
  }
18
45
  };
19
46
  }
47
+ function resolvePlatformTaroPath(target) {
48
+ return target === 'h5'
49
+ ? packageRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
50
+ : packageRequire.resolve('@tarojs/taro');
51
+ }
52
+ function isClientTaroFacade(importer) {
53
+ return importer !== undefined && normalizeModuleId(importer) === normalizedClientTaroApiPath;
54
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Applies Taro React's standard source-to-source API loader. It imports the framework lifecycle hooks, assigns them to
3
+ * the facade's `taro` object, and emits matching named exports; keeping the hook inventory owned by Taro avoids drift.
4
+ */
5
+ export declare function injectTaroFrameworkApis(source: string): string;
@@ -0,0 +1,9 @@
1
+ import { packageRequire } from '../../utils/packages.js';
2
+ const apiLoader = packageRequire('@tarojs/plugin-framework-react/dist/api-loader');
3
+ /**
4
+ * Applies Taro React's standard source-to-source API loader. It imports the framework lifecycle hooks, assigns them to
5
+ * the facade's `taro` object, and emits matching named exports; keeping the hook inventory owned by Taro avoids drift.
6
+ */
7
+ export function injectTaroFrameworkApis(source) {
8
+ return apiLoader(source);
9
+ }
@@ -1,4 +1,4 @@
1
1
  import type { PluginOption } from 'vite';
2
2
  import type { VitePluginTaroTarget } from '../../../options.ts';
3
- /** Creates the target-aware Tailwind CSS plugins. */
3
+ /** Creates the target-aware Tailwind pipeline. */
4
4
  export declare function createCssPlugins(target: VitePluginTaroTarget): PluginOption[];
@@ -2,83 +2,118 @@ import path from 'node:path';
2
2
  import { createStyleHandler } from '@weapp-tailwindcss/postcss';
3
3
  import { WeappTailwindcss } from 'weapp-tailwindcss/vite';
4
4
  import { packageRequire } from '../../utils/packages.js';
5
- // Keep the Vite plugin and the compatibility finalizer on identical WX conversion settings. A difference here can
6
- // make the second pass preserve browser units or apply a transformation that the first pass did not expect.
5
+ /*
6
+ * CSS output order for WX:
7
+ *
8
+ * weapp-tailwindcss output hooks
9
+ * → vpt:wx-style-finalizer
10
+ * → vpt:wx native companion emission
11
+ *
12
+ * All three generateBundle hooks retain hook-level `order: 'post'` and therefore execute in registration order. The
13
+ * upstream plugin normally also uses plugin-level `enforce: 'post'`, which would move it behind both VPT plugins and
14
+ * break this sequence. `alignWxGenerateBundleOrder` removes only that broader phase from upstream output hooks.
15
+ */
16
+ // Tailwind belongs to VPT, not necessarily to the application. Resolving from VPT keeps strict package managers and
17
+ // bundled development from looking for Tailwind in the application's node_modules.
7
18
  const tailwindcssBasedir = path.dirname(packageRequire.resolve('tailwindcss/package.json'));
19
+ // Both upstream generation and VPT's final whole-file pass use one conversion policy. If these options diverge, the
20
+ // second pass can preserve browser units or reinterpret syntax that the first pass generated.
8
21
  const wxStyleOptions = {
9
22
  cssCalc: false,
10
23
  autoprefixer: false,
11
24
  rem2rpx: true,
12
25
  px2rpx: true
13
26
  };
14
- /** Creates the target-aware Tailwind CSS plugins. */
27
+ // The handler is immutable and reusable across builds; only each emitted asset's source is replaced.
28
+ const transformWxStyle = createStyleHandler(wxStyleOptions);
29
+ /** Creates the target-aware Tailwind pipeline. */
15
30
  export function createCssPlugins(target) {
16
31
  const wx = target === 'wx';
32
+ const tailwindPlugins = WeappTailwindcss({
33
+ // VPT is a custom Vite compiler. Using Taro's adapter would import Taro-specific CSS ownership rules.
34
+ appType: 'weapp-vite',
35
+ // WX generation rewrites Tailwind's split package imports before Vite tries to resolve them in the app.
36
+ // Without this, strict workspaces fail on imports such as `tailwindcss/theme.css`.
37
+ rewriteCssImports: wx,
38
+ platform: wx ? 'weapp' : 'web',
39
+ tailwindcssBasedir,
40
+ generator: {
41
+ target: wx ? 'weapp' : 'web'
42
+ },
43
+ cssOptions: {
44
+ ...wxStyleOptions,
45
+ // Browser output still needs vendor prefixes; WXSS does not support or need that browser pass.
46
+ autoprefixer: !wx
47
+ },
48
+ logLevel: 'warn'
49
+ }) ?? [];
17
50
  return [
18
- ...(WeappTailwindcss({
19
- appType: 'weapp-vite',
20
- // WX must enable this for split Tailwind imports such as `tailwindcss/theme.css`. Otherwise Vite's
21
- // PostCSS resolver tries to resolve those imports from the application and fails when Tailwind is owned
22
- // by vpt. The web generator consumes the imports before that resolver runs, so H5 keeps the
23
- // upstream default.
24
- rewriteCssImports: wx,
25
- // Tailwind is a plugin dependency, not an application dependency. Give weapp-tailwindcss the owning package
26
- // directory explicitly so bundled development and strict package managers resolve split CSS imports equally.
27
- tailwindcssBasedir,
28
- generator: {
29
- target: wx ? 'weapp' : 'web'
30
- // webCompat: {
31
- // preset: 'legacy-web'
32
- // }
33
- },
34
- cssOptions: {
35
- ...wxStyleOptions,
36
- autoprefixer: !wx
37
- },
38
- logLevel: 'warn'
39
- }) ?? []),
40
- wx ? createWxssCompatibilityFinalizer() : undefined
51
+ ...(wx ? tailwindPlugins.map(alignWxGenerateBundleOrder) : tailwindPlugins),
52
+ wx ? createWxStyleFinalizer() : undefined
41
53
  ];
42
54
  }
43
55
  /**
44
- * Completes WXSS adaptation that weapp-tailwindcss leaves pending after rewriting split Tailwind imports.
56
+ * Finalizes the one global stylesheet after upstream Tailwind generation.
45
57
  *
46
- * `rewriteCssImports: true` makes the early Vite transform generate the Tailwind CSS, but the non-web generator
47
- * also defers CSS adaptation. The generated asset is consequently browser-shaped
48
- * CSS containing values and syntax such as `rem`, escaped class selectors, and `@property`. It is then recorded as a
49
- * processed Vite asset, so the upstream output finalizer does not perform the missing complete WXSS adaptation.
50
- *
51
- * This plugin runs after the upstream finalizer, repeats only the compatibility transform, and restores the captured
52
- * global asset to WeChat's required `app.wxss` path. Exact path correlation leaves Page WXSS companions untouched.
53
- * Remove it when upstream both completes adaptation and preserves the bundler-selected filename.
58
+ * `cssCodeSplit: false` makes the compiler style global, but upstream can name it `.css` or `.wxss` depending on build
59
+ * mode. This hook converts its complete final contents once and gives it the root `app.wxss` identity required by
60
+ * WeChat. Running earlier loses CSS from dynamic chunks; running after native companion emission would also see Page
61
+ * and native-component WXSS files that must remain opaque.
54
62
  */
55
- function createWxssCompatibilityFinalizer() {
56
- // This compatibility pass only needs the PostCSS style pipeline. Creating a complete weapp-tailwindcss context here
57
- // would initialize another Tailwind compiler and source scanner for CSS that the upstream Vite plugin already generated.
58
- const transformWxss = createStyleHandler(wxStyleOptions);
63
+ function createWxStyleFinalizer() {
59
64
  return {
60
- name: 'vpt:wxss-compatibility-finalizer',
61
- enforce: 'post',
65
+ name: 'vpt:wx-style-finalizer',
62
66
  generateBundle: {
63
67
  order: 'post',
64
68
  async handler(_, bundle) {
65
- // With cssCodeSplit: false the global style is the only asset with a logical
66
- // .css source name, even when an earlier hook already changed its final suffix.
67
- const globalStyleAsset = Object.values(bundle).find((output) => output.type === 'asset' &&
68
- (output.fileName.replaceAll('\\', '/').endsWith('.css') ||
69
- output.names.some((name) => name.replaceAll('\\', '/').endsWith('.css'))));
70
- if (!globalStyleAsset)
71
- return;
72
- // Both finalizers use a post-ordered generateBundle hook. Array order places this hook after the
73
- // upstream finalizer, where the single Vite global style asset has its final contents and filename.
74
- const source = typeof globalStyleAsset.source === 'string'
75
- ? globalStyleAsset.source
76
- : new TextDecoder().decode(globalStyleAsset.source);
77
- if (source.length > 0) {
78
- globalStyleAsset.source = (await transformWxss(source)).css;
69
+ const styles = Object.values(bundle).filter(isStyleAsset);
70
+ // More than one compiler style means cssCodeSplit was re-enabled. Choosing one would silently lose CSS.
71
+ if (styles.length > 1) {
72
+ throw new Error('WX builds require one global compiler-emitted stylesheet');
79
73
  }
80
- globalStyleAsset.fileName = 'app.wxss';
74
+ // CSS is optional; applications without styles do not need an empty app.wxss.
75
+ if (styles.length === 0)
76
+ return;
77
+ const [style] = styles;
78
+ const source = typeof style.source === 'string' ? style.source : new TextDecoder().decode(style.source);
79
+ // generateBundle exposes the final asset as mutable so conversion and native placement remain atomic.
80
+ // Without the compatibility pass, browser-only selectors, escaped classes, rem and @property can reach
81
+ // WeChat. Without the rename, bundled development writes paths such as src/app.wxss, which WeChat does
82
+ // not load as the application's global stylesheet.
83
+ style.source = (await transformWxStyle(source)).css;
84
+ style.fileName = 'app.wxss';
81
85
  }
82
86
  }
83
87
  };
84
88
  }
89
+ /**
90
+ * Adapts upstream plugin descriptors without mutating `weapp-tailwindcss` or patching node_modules.
91
+ *
92
+ * Vite first groups whole plugins by `enforce`, then orders individual hooks. Upstream's output plugins specify both
93
+ * `enforce: 'post'` and `generateBundle.order: 'post'`. The plugin-level phase overrides their earlier registration and
94
+ * places them after VPT's normal plugins, so VPT observes incomplete CSS. Making all of VPT post-enforced would fix that
95
+ * one hook while unnecessarily reordering resolution and transforms.
96
+ *
97
+ * For upstream plugins that actually own generateBundle, clone the descriptor without plugin-level enforcement. Keep
98
+ * hook-level `order: 'post'`: it still waits for ordinary bundle generation, while registration order becomes the sole
99
+ * tie-breaker between upstream generation, VPT finalization and native output. H5 descriptors remain untouched.
100
+ */
101
+ function alignWxGenerateBundleOrder(pluginOption) {
102
+ // PluginOption permits nested arrays. Preserve their shape while adapting every concrete plugin recursively.
103
+ if (Array.isArray(pluginOption))
104
+ return pluginOption.map(alignWxGenerateBundleOrder);
105
+ if (!pluginOption ||
106
+ typeof pluginOption !== 'object' ||
107
+ !('enforce' in pluginOption) ||
108
+ pluginOption.enforce !== 'post' ||
109
+ !('generateBundle' in pluginOption) ||
110
+ pluginOption.generateBundle === undefined) {
111
+ return pluginOption;
112
+ }
113
+ // Clone rather than mutate: upstream may retain or reuse the descriptor returned by its factory.
114
+ return { ...pluginOption, enforce: undefined };
115
+ }
116
+ /** Selects only the compiler stylesheet; native WXSS assets are emitted by the later WX hook. */
117
+ function isStyleAsset(output) {
118
+ return output.type === 'asset' && /\.(?:css|wxss)$/.test(output.fileName);
119
+ }
@@ -34,10 +34,6 @@ function createH5TargetPlugin(options) {
34
34
  {
35
35
  find: /^@tarojs\/components\/dist\/components$/,
36
36
  replacement: packageRequire.resolve('@tarojs/components/dist/components')
37
- },
38
- {
39
- find: /^@tarojs\/taro$/,
40
- replacement: packageRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
41
37
  }
42
38
  ]
43
39
  },
@@ -98,8 +98,7 @@ export function createPlacer() {
98
98
  // into every physical chunk filename; content identity alone is sufficient beneath that root.
99
99
  return `${location.root}/assets/[hash].js`;
100
100
  },
101
- // Keep generic assets independent of stylesheet conventions. The CSS adapter sees Tailwind's final
102
- // mutation and owns the exact global `app.wxss` identity without confusing native Page companions.
101
+ // Keep generic assets independent of native output identities assigned after bundling.
103
102
  assetFileNames: 'assets/[name]-[hash][extname]'
104
103
  },
105
104
  // Rolldown rejects strict entry signatures when code-splitting groups disable recursive dependency capture.
@@ -36,8 +36,11 @@ function createWxPlugin(options) {
36
36
  },
37
37
  build: {
38
38
  modulePreload: false,
39
+ // Mini Program styles are intentionally global. This guarantees one compiler stylesheet for the CSS
40
+ // finalizer; enabling splitting would require Page ownership and must not be silently flattened.
39
41
  cssCodeSplit: false,
40
- // Let weapp-tailwindcss own final WXSS transformation.
42
+ // Preserve readable source for the final WX compatibility pass; browser minification can emit syntax
43
+ // unsupported by WeChat and would make the subsequent whole-file conversion harder to reason about.
41
44
  cssMinify: false,
42
45
  // No base64 assets: Taro warns on image srcs above ~2KB, and inlined
43
46
  // images bloat the JS bundle toward the mini program package limit.
@@ -99,6 +102,12 @@ function createWxPlugin(options) {
99
102
  }
100
103
  },
101
104
  generateBundle: {
105
+ /*
106
+ * This hook is registered after createCssPlugins() and shares hook-level `order: 'post'` with the adapted
107
+ * upstream hooks and VPT style finalizer. Registration order therefore guarantees that app.wxss is complete
108
+ * before native Page/component companions are emitted. Without this order, the CSS finalizer could consume
109
+ * incomplete Tailwind output or mistake native WXSS companions for additional compiler styles.
110
+ */
102
111
  order: 'post',
103
112
  async handler(_, bundle) {
104
113
  const subpackages = placer.getSubpackages(bundle);
@@ -8,7 +8,7 @@ import { createWxTargetPlugins } from './plugins/wx/plugins.js';
8
8
  export default function vitePluginTaro(options) {
9
9
  return [
10
10
  createConditionalDirectivePlugin(options.target),
11
- createClientTaroPlugin(),
11
+ createClientTaroPlugin(options.target),
12
12
  ...createCssPlugins(options.target),
13
13
  ...react(),
14
14
  ...(options.target === 'wx' ? createWxTargetPlugins(options) : []),
@@ -1,4 +1,3 @@
1
- import '@tarojs/plugin-framework-react/dist/runtime';
2
- import Taro from '@tarojs/taro';
1
+ import taro from '@tarojs/taro';
3
2
  export * from '@tarojs/taro';
4
- export default Taro;
3
+ export default taro;
@@ -1,9 +1,10 @@
1
- import '@tarojs/plugin-framework-react/dist/runtime';
2
- import { hooks } from '@tarojs/runtime';
3
- import Taro from '@tarojs/taro';
4
- if (hooks.isExist('initNativeApi')) {
5
- hooks.call('initNativeApi', Taro);
6
- }
7
- // @ts-expect-error @tarojs/taro declares export= types, but the facade also exposes its runtime named exports.
1
+ /*
2
+ * This physical module is the shared facade, not the generic Taro implementation. Its own `@tarojs/taro` request is
3
+ * resolved by the client plugin to the selected platform APIs, while application requests resolve back to this facade.
4
+ * React's API loader then extends this same object with framework lifecycle hooks.
5
+ */
6
+ import taro from '@tarojs/taro';
7
+ // Re-export platform APIs while preserving object identity for the framework loader's lifecycle assignments.
8
+ // @ts-expect-error @tarojs/taro uses export= types while Rolldown exposes its runtime properties as named exports.
8
9
  export * from '@tarojs/taro';
9
- export default Taro;
10
+ export default taro;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-taro",
3
- "version": "0.5.0",
3
+ "version": "0.5.3",
4
4
  "author": "sep2",
5
5
  "description": "Vite 8 plugin for building one React/Taro codebase for WeChat Mini Program and H5 targets.",
6
6
  "type": "module",
@@ -74,8 +74,8 @@
74
74
  "rolldown": "1.2.3",
75
75
  "tailwindcss": "^4.3.3",
76
76
  "weapp-tailwindcss": "^5.2.11",
77
- "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@0.5.0",
78
- "@tarojs/react": "npm:vite-plugin-taro-react@0.5.0"
77
+ "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@0.5.3",
78
+ "@tarojs/react": "npm:vite-plugin-taro-react@0.5.3"
79
79
  },
80
80
  "peerDependencies": {
81
81
  "react": "^19.0.0",
@@ -1,23 +1,66 @@
1
1
  import type { Plugin } from 'vite'
2
- import { resolvePackageFile } from '../../utils/packages.ts'
2
+ import type { VitePluginTaroTarget } from '../../../options.ts'
3
+ import { normalizeModuleId } from '../../utils/modules.ts'
4
+ import { packageRequire, resolvePackageFile } from '../../utils/packages.ts'
3
5
  import { clientTaroNativeId } from './constant.ts'
6
+ import { injectTaroFrameworkApis } from './inject-taro-framework-apis.ts'
4
7
 
8
+ /*
9
+ * Taro API resolution uses one public facade while avoiding a recursive `@tarojs/taro` import:
10
+ *
11
+ * 1. Application imports of `virtual:taro/api` or `@tarojs/taro` resolve to the physical `api.js` facade.
12
+ * 2. The facade itself imports `@tarojs/taro`; its importer identifies that request as the platform implementation.
13
+ * 3. H5 receives `@tarojs/plugin-platform-h5` APIs, while WX receives the generic `@tarojs/taro` implementation.
14
+ * 4. React's framework API loader transforms the facade, assigning lifecycle hooks such as `useLaunch` to the same
15
+ * platform object and exposing them as named exports.
16
+ *
17
+ * The importer-sensitive second step removes the need for another public-looking virtual module while retaining one
18
+ * facade object for platform APIs and framework lifecycles.
19
+ */
20
+ /** Public facade used by transformed application API imports. */
5
21
  export const clientTaroApiId = 'virtual:taro/api'
22
+
23
+ const clientTaroApiPath = resolvePackageFile('dist/runtime/client/taro/api.js')
24
+ const normalizedClientTaroApiPath = normalizeModuleId(clientTaroApiPath)
25
+
6
26
  const clientTaroComponentId = 'virtual:taro/components'
27
+
7
28
  const clientTaroModules = new Map([
8
- [clientTaroApiId, resolvePackageFile('dist/runtime/client/taro/api.js')],
29
+ [clientTaroApiId, clientTaroApiPath],
9
30
  [clientTaroComponentId, resolvePackageFile('dist/runtime/client/taro/component.js')],
10
31
  [clientTaroNativeId, resolvePackageFile('dist/runtime/client/taro/define-native-component.js')]
11
32
  ])
12
33
 
13
- /** Creates the target-neutral Taro facade plugin. */
14
- export function createClientTaroPlugin(): Plugin {
34
+ /** Creates the shared Taro facade backed by the selected target's API implementation. */
35
+ export function createClientTaroPlugin(target: VitePluginTaroTarget): Plugin {
36
+ const platformTaroPath = resolvePlatformTaroPath(target)
37
+
15
38
  return {
16
39
  name: 'vpt:client-taro',
17
40
  enforce: 'pre',
18
41
 
19
- resolveId(id) {
42
+ resolveId(id, importer) {
43
+ if (id === '@tarojs/taro') {
44
+ return isClientTaroFacade(importer) ? platformTaroPath : clientTaroApiPath
45
+ }
20
46
  return clientTaroModules.get(id)
47
+ },
48
+
49
+ // Taro's framework loader transforms source code, so apply it only to the shared physical facade.
50
+ transform(code, id) {
51
+ if (normalizeModuleId(id) === normalizedClientTaroApiPath) {
52
+ return injectTaroFrameworkApis(code)
53
+ }
21
54
  }
22
55
  }
23
56
  }
57
+
58
+ function resolvePlatformTaroPath(target: VitePluginTaroTarget): string {
59
+ return target === 'h5'
60
+ ? packageRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
61
+ : packageRequire.resolve('@tarojs/taro')
62
+ }
63
+
64
+ function isClientTaroFacade(importer: string | undefined): boolean {
65
+ return importer !== undefined && normalizeModuleId(importer) === normalizedClientTaroApiPath
66
+ }
@@ -0,0 +1,11 @@
1
+ import { packageRequire } from '../../utils/packages.ts'
2
+
3
+ const apiLoader: (source: string) => string = packageRequire('@tarojs/plugin-framework-react/dist/api-loader')
4
+
5
+ /**
6
+ * Applies Taro React's standard source-to-source API loader. It imports the framework lifecycle hooks, assigns them to
7
+ * the facade's `taro` object, and emits matching named exports; keeping the hook inventory owned by Taro avoids drift.
8
+ */
9
+ export function injectTaroFrameworkApis(source: string): string {
10
+ return apiLoader(source)
11
+ }
@@ -5,10 +5,24 @@ import { WeappTailwindcss } from 'weapp-tailwindcss/vite'
5
5
  import type { VitePluginTaroTarget } from '../../../options.ts'
6
6
  import { packageRequire } from '../../utils/packages.ts'
7
7
 
8
- // Keep the Vite plugin and the compatibility finalizer on identical WX conversion settings. A difference here can
9
- // make the second pass preserve browser units or apply a transformation that the first pass did not expect.
8
+ /*
9
+ * CSS output order for WX:
10
+ *
11
+ * weapp-tailwindcss output hooks
12
+ * → vpt:wx-style-finalizer
13
+ * → vpt:wx native companion emission
14
+ *
15
+ * All three generateBundle hooks retain hook-level `order: 'post'` and therefore execute in registration order. The
16
+ * upstream plugin normally also uses plugin-level `enforce: 'post'`, which would move it behind both VPT plugins and
17
+ * break this sequence. `alignWxGenerateBundleOrder` removes only that broader phase from upstream output hooks.
18
+ */
19
+
20
+ // Tailwind belongs to VPT, not necessarily to the application. Resolving from VPT keeps strict package managers and
21
+ // bundled development from looking for Tailwind in the application's node_modules.
10
22
  const tailwindcssBasedir = path.dirname(packageRequire.resolve('tailwindcss/package.json'))
11
23
 
24
+ // Both upstream generation and VPT's final whole-file pass use one conversion policy. If these options diverge, the
25
+ // second pass can preserve browser units or reinterpret syntax that the first pass generated.
12
26
  const wxStyleOptions = {
13
27
  cssCalc: false,
14
28
  autoprefixer: false,
@@ -16,81 +30,106 @@ const wxStyleOptions = {
16
30
  px2rpx: true
17
31
  } as const
18
32
 
19
- /** Creates the target-aware Tailwind CSS plugins. */
33
+ // The handler is immutable and reusable across builds; only each emitted asset's source is replaced.
34
+ const transformWxStyle = createStyleHandler(wxStyleOptions)
35
+
36
+ /** Creates the target-aware Tailwind pipeline. */
20
37
  export function createCssPlugins(target: VitePluginTaroTarget): PluginOption[] {
21
38
  const wx = target === 'wx'
22
39
 
23
- return [
24
- ...(WeappTailwindcss({
40
+ const tailwindPlugins =
41
+ WeappTailwindcss({
42
+ // VPT is a custom Vite compiler. Using Taro's adapter would import Taro-specific CSS ownership rules.
25
43
  appType: 'weapp-vite',
26
- // WX must enable this for split Tailwind imports such as `tailwindcss/theme.css`. Otherwise Vite's
27
- // PostCSS resolver tries to resolve those imports from the application and fails when Tailwind is owned
28
- // by vpt. The web generator consumes the imports before that resolver runs, so H5 keeps the
29
- // upstream default.
44
+ // WX generation rewrites Tailwind's split package imports before Vite tries to resolve them in the app.
45
+ // Without this, strict workspaces fail on imports such as `tailwindcss/theme.css`.
30
46
  rewriteCssImports: wx,
31
- // Tailwind is a plugin dependency, not an application dependency. Give weapp-tailwindcss the owning package
32
- // directory explicitly so bundled development and strict package managers resolve split CSS imports equally.
47
+ platform: wx ? 'weapp' : 'web',
33
48
  tailwindcssBasedir,
34
49
  generator: {
35
50
  target: wx ? 'weapp' : 'web'
36
- // webCompat: {
37
- // preset: 'legacy-web'
38
- // }
39
51
  },
40
52
  cssOptions: {
41
53
  ...wxStyleOptions,
54
+ // Browser output still needs vendor prefixes; WXSS does not support or need that browser pass.
42
55
  autoprefixer: !wx
43
56
  },
44
57
  logLevel: 'warn'
45
- }) ?? []),
46
- wx ? createWxssCompatibilityFinalizer() : undefined
58
+ }) ?? []
59
+
60
+ return [
61
+ ...(wx ? tailwindPlugins.map(alignWxGenerateBundleOrder) : tailwindPlugins),
62
+ wx ? createWxStyleFinalizer() : undefined
47
63
  ]
48
64
  }
49
65
 
50
66
  /**
51
- * Completes WXSS adaptation that weapp-tailwindcss leaves pending after rewriting split Tailwind imports.
52
- *
53
- * `rewriteCssImports: true` makes the early Vite transform generate the Tailwind CSS, but the non-web generator
54
- * also defers CSS adaptation. The generated asset is consequently browser-shaped
55
- * CSS containing values and syntax such as `rem`, escaped class selectors, and `@property`. It is then recorded as a
56
- * processed Vite asset, so the upstream output finalizer does not perform the missing complete WXSS adaptation.
67
+ * Finalizes the one global stylesheet after upstream Tailwind generation.
57
68
  *
58
- * This plugin runs after the upstream finalizer, repeats only the compatibility transform, and restores the captured
59
- * global asset to WeChat's required `app.wxss` path. Exact path correlation leaves Page WXSS companions untouched.
60
- * Remove it when upstream both completes adaptation and preserves the bundler-selected filename.
69
+ * `cssCodeSplit: false` makes the compiler style global, but upstream can name it `.css` or `.wxss` depending on build
70
+ * mode. This hook converts its complete final contents once and gives it the root `app.wxss` identity required by
71
+ * WeChat. Running earlier loses CSS from dynamic chunks; running after native companion emission would also see Page
72
+ * and native-component WXSS files that must remain opaque.
61
73
  */
62
- function createWxssCompatibilityFinalizer(): Plugin {
63
- // This compatibility pass only needs the PostCSS style pipeline. Creating a complete weapp-tailwindcss context here
64
- // would initialize another Tailwind compiler and source scanner for CSS that the upstream Vite plugin already generated.
65
- const transformWxss = createStyleHandler(wxStyleOptions)
66
-
74
+ function createWxStyleFinalizer(): Plugin {
67
75
  return {
68
- name: 'vpt:wxss-compatibility-finalizer',
69
- enforce: 'post',
76
+ name: 'vpt:wx-style-finalizer',
70
77
  generateBundle: {
71
78
  order: 'post',
72
79
  async handler(_, bundle) {
73
- // With cssCodeSplit: false the global style is the only asset with a logical
74
- // .css source name, even when an earlier hook already changed its final suffix.
75
- const globalStyleAsset = Object.values(bundle).find(
76
- (output): output is Rolldown.OutputAsset =>
77
- output.type === 'asset' &&
78
- (output.fileName.replaceAll('\\', '/').endsWith('.css') ||
79
- output.names.some((name) => name.replaceAll('\\', '/').endsWith('.css')))
80
- )
81
- if (!globalStyleAsset) return
82
-
83
- // Both finalizers use a post-ordered generateBundle hook. Array order places this hook after the
84
- // upstream finalizer, where the single Vite global style asset has its final contents and filename.
85
- const source =
86
- typeof globalStyleAsset.source === 'string'
87
- ? globalStyleAsset.source
88
- : new TextDecoder().decode(globalStyleAsset.source)
89
- if (source.length > 0) {
90
- globalStyleAsset.source = (await transformWxss(source)).css
80
+ const styles = Object.values(bundle).filter(isStyleAsset)
81
+ // More than one compiler style means cssCodeSplit was re-enabled. Choosing one would silently lose CSS.
82
+ if (styles.length > 1) {
83
+ throw new Error('WX builds require one global compiler-emitted stylesheet')
91
84
  }
92
- globalStyleAsset.fileName = 'app.wxss'
85
+ // CSS is optional; applications without styles do not need an empty app.wxss.
86
+ if (styles.length === 0) return
87
+
88
+ const [style] = styles
89
+ const source = typeof style.source === 'string' ? style.source : new TextDecoder().decode(style.source)
90
+ // generateBundle exposes the final asset as mutable so conversion and native placement remain atomic.
91
+ // Without the compatibility pass, browser-only selectors, escaped classes, rem and @property can reach
92
+ // WeChat. Without the rename, bundled development writes paths such as src/app.wxss, which WeChat does
93
+ // not load as the application's global stylesheet.
94
+ style.source = (await transformWxStyle(source)).css
95
+ style.fileName = 'app.wxss'
93
96
  }
94
97
  }
95
98
  }
96
99
  }
100
+
101
+ /**
102
+ * Adapts upstream plugin descriptors without mutating `weapp-tailwindcss` or patching node_modules.
103
+ *
104
+ * Vite first groups whole plugins by `enforce`, then orders individual hooks. Upstream's output plugins specify both
105
+ * `enforce: 'post'` and `generateBundle.order: 'post'`. The plugin-level phase overrides their earlier registration and
106
+ * places them after VPT's normal plugins, so VPT observes incomplete CSS. Making all of VPT post-enforced would fix that
107
+ * one hook while unnecessarily reordering resolution and transforms.
108
+ *
109
+ * For upstream plugins that actually own generateBundle, clone the descriptor without plugin-level enforcement. Keep
110
+ * hook-level `order: 'post'`: it still waits for ordinary bundle generation, while registration order becomes the sole
111
+ * tie-breaker between upstream generation, VPT finalization and native output. H5 descriptors remain untouched.
112
+ */
113
+ function alignWxGenerateBundleOrder(pluginOption: PluginOption): PluginOption {
114
+ // PluginOption permits nested arrays. Preserve their shape while adapting every concrete plugin recursively.
115
+ if (Array.isArray(pluginOption)) return pluginOption.map(alignWxGenerateBundleOrder)
116
+
117
+ if (
118
+ !pluginOption ||
119
+ typeof pluginOption !== 'object' ||
120
+ !('enforce' in pluginOption) ||
121
+ pluginOption.enforce !== 'post' ||
122
+ !('generateBundle' in pluginOption) ||
123
+ pluginOption.generateBundle === undefined
124
+ ) {
125
+ return pluginOption
126
+ }
127
+
128
+ // Clone rather than mutate: upstream may retain or reuse the descriptor returned by its factory.
129
+ return { ...pluginOption, enforce: undefined }
130
+ }
131
+
132
+ /** Selects only the compiler stylesheet; native WXSS assets are emitted by the later WX hook. */
133
+ function isStyleAsset(output: Rolldown.OutputBundle[string]): output is Rolldown.OutputAsset {
134
+ return output.type === 'asset' && /\.(?:css|wxss)$/.test(output.fileName)
135
+ }
@@ -41,10 +41,6 @@ function createH5TargetPlugin(options: VitePluginTaroOptions): Plugin {
41
41
  {
42
42
  find: /^@tarojs\/components\/dist\/components$/,
43
43
  replacement: packageRequire.resolve('@tarojs/components/dist/components')
44
- },
45
- {
46
- find: /^@tarojs\/taro$/,
47
- replacement: packageRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
48
44
  }
49
45
  ]
50
46
  },
@@ -121,8 +121,7 @@ export function createPlacer() {
121
121
  // into every physical chunk filename; content identity alone is sufficient beneath that root.
122
122
  return `${location.root}/assets/[hash].js`
123
123
  },
124
- // Keep generic assets independent of stylesheet conventions. The CSS adapter sees Tailwind's final
125
- // mutation and owns the exact global `app.wxss` identity without confusing native Page companions.
124
+ // Keep generic assets independent of native output identities assigned after bundling.
126
125
  assetFileNames: 'assets/[name]-[hash][extname]'
127
126
  },
128
127
  // Rolldown rejects strict entry signatures when code-splitting groups disable recursive dependency capture.
@@ -46,8 +46,11 @@ function createWxPlugin(options: VitePluginTaroOptions): Plugin {
46
46
 
47
47
  build: {
48
48
  modulePreload: false,
49
+ // Mini Program styles are intentionally global. This guarantees one compiler stylesheet for the CSS
50
+ // finalizer; enabling splitting would require Page ownership and must not be silently flattened.
49
51
  cssCodeSplit: false,
50
- // Let weapp-tailwindcss own final WXSS transformation.
52
+ // Preserve readable source for the final WX compatibility pass; browser minification can emit syntax
53
+ // unsupported by WeChat and would make the subsequent whole-file conversion harder to reason about.
51
54
  cssMinify: false,
52
55
 
53
56
  // No base64 assets: Taro warns on image srcs above ~2KB, and inlined
@@ -123,6 +126,12 @@ function createWxPlugin(options: VitePluginTaroOptions): Plugin {
123
126
  },
124
127
 
125
128
  generateBundle: {
129
+ /*
130
+ * This hook is registered after createCssPlugins() and shares hook-level `order: 'post'` with the adapted
131
+ * upstream hooks and VPT style finalizer. Registration order therefore guarantees that app.wxss is complete
132
+ * before native Page/component companions are emitted. Without this order, the CSS finalizer could consume
133
+ * incomplete Tailwind output or mistake native WXSS companions for additional compiler styles.
134
+ */
126
135
  order: 'post',
127
136
  async handler(_, bundle) {
128
137
  const subpackages = placer.getSubpackages(bundle)
@@ -11,7 +11,7 @@ import { createWxTargetPlugins } from './plugins/wx/plugins.ts'
11
11
  export default function vitePluginTaro(options: VitePluginTaroOptions): PluginOption[] {
12
12
  return [
13
13
  createConditionalDirectivePlugin(options.target),
14
- createClientTaroPlugin(),
14
+ createClientTaroPlugin(options.target),
15
15
  ...createCssPlugins(options.target),
16
16
  ...react(),
17
17
  ...(options.target === 'wx' ? createWxTargetPlugins(options) : []),
@@ -1,11 +1,11 @@
1
- import '@tarojs/plugin-framework-react/dist/runtime'
2
- import { hooks } from '@tarojs/runtime'
3
- import Taro from '@tarojs/taro'
1
+ /*
2
+ * This physical module is the shared facade, not the generic Taro implementation. Its own `@tarojs/taro` request is
3
+ * resolved by the client plugin to the selected platform APIs, while application requests resolve back to this facade.
4
+ * React's API loader then extends this same object with framework lifecycle hooks.
5
+ */
6
+ import taro from '@tarojs/taro'
4
7
 
5
- if (hooks.isExist('initNativeApi')) {
6
- hooks.call('initNativeApi', Taro)
7
- }
8
-
9
- // @ts-expect-error @tarojs/taro declares export= types, but the facade also exposes its runtime named exports.
8
+ // Re-export platform APIs while preserving object identity for the framework loader's lifecycle assignments.
9
+ // @ts-expect-error @tarojs/taro uses export= types while Rolldown exposes its runtime properties as named exports.
10
10
  export * from '@tarojs/taro'
11
- export default Taro
11
+ export default taro