vite-plugin-taro 0.4.2 → 0.5.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.
@@ -1,4 +1,6 @@
1
1
  import type { PluginOption } from 'vite';
2
2
  import type { VitePluginTaroTarget } from '../../../options.ts';
3
+ /** Completes the final compatibility pass required by generated WXSS. */
4
+ export declare function adaptWxss(source: string): Promise<string>;
3
5
  /** Creates the target-aware Tailwind CSS plugins. */
4
6
  export declare function createCssPlugins(target: VitePluginTaroTarget): PluginOption[];
@@ -11,6 +11,11 @@ const wxStyleOptions = {
11
11
  rem2rpx: true,
12
12
  px2rpx: true
13
13
  };
14
+ const transformWxss = createStyleHandler(wxStyleOptions);
15
+ /** Completes the final compatibility pass required by generated WXSS. */
16
+ export async function adaptWxss(source) {
17
+ return (await transformWxss(source)).css;
18
+ }
14
19
  /** Creates the target-aware Tailwind CSS plugins. */
15
20
  export function createCssPlugins(target) {
16
21
  const wx = target === 'wx';
@@ -53,9 +58,6 @@ export function createCssPlugins(target) {
53
58
  * Remove it when upstream both completes adaptation and preserves the bundler-selected filename.
54
59
  */
55
60
  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);
59
61
  return {
60
62
  name: 'vpt:wxss-compatibility-finalizer',
61
63
  enforce: 'post',
@@ -75,7 +77,7 @@ function createWxssCompatibilityFinalizer() {
75
77
  ? globalStyleAsset.source
76
78
  : new TextDecoder().decode(globalStyleAsset.source);
77
79
  if (source.length > 0) {
78
- globalStyleAsset.source = (await transformWxss(source)).css;
80
+ globalStyleAsset.source = await adaptWxss(source);
79
81
  }
80
82
  globalStyleAsset.fileName = 'app.wxss';
81
83
  }
@@ -0,0 +1,6 @@
1
+ import type { InputOptions } from 'rolldown';
2
+ type DevMode = NonNullable<NonNullable<InputOptions['experimental']>['devMode']>;
3
+ type DevModeOptions = Exclude<DevMode, boolean>;
4
+ /** Installs the custom WX runtime while restoring Rolldown's common runtime base. */
5
+ export declare function createWxDevMode(devMode: DevMode | undefined, implement: string): DevModeOptions;
6
+ export {};
@@ -0,0 +1,9 @@
1
+ /** Installs the custom WX runtime while restoring Rolldown's common runtime base. */
2
+ export function createWxDevMode(devMode, implement) {
3
+ return {
4
+ ...(typeof devMode === 'object' ? devMode : {}),
5
+ implement,
6
+ lazy: false,
7
+ skipCommonRuntimeInjection: false
8
+ };
9
+ }
@@ -5,9 +5,11 @@ import { dev, viteReporterPlugin } from 'rolldown/experimental';
5
5
  import { once } from '../../../utils/once.js';
6
6
  import { resolvePackageFile } from '../../../utils/packages.js';
7
7
  import { appShellFileName } from '../module.js';
8
+ import { createWxDevMode } from './create-wx-dev-mode.js';
8
9
  import { emptyOutputDirectory } from './empty-output-directory.js';
9
10
  import { hmrControlPath, hmrInfoFileName, hmrPatchesFileName, renderHmrInfo, renderInitialHmrPatches, writeHmrFile } from './hmr-files.js';
10
11
  import { PatchPublisher } from './patch-publisher.js';
12
+ import { publishWxDevStyle } from './publish-wx-dev-style.js';
11
13
  /**
12
14
  * Creates the wx dev host: the adapter that owns the physical Rolldown DevEngine (created
13
15
  * with dev(...)) and the patch publisher, and replaces Vite's bundledDev.listen so the
@@ -116,6 +118,7 @@ export async function createWxDevHost({ server, options }) {
116
118
  throw new Error('wx development requires exactly one Rolldown output.');
117
119
  }
118
120
  return dev(rolldownOptions, rolldownOptions.output, {
121
+ onAdditionalAssets: (output) => publishWxDevStyle(output, server.config.build.outDir),
119
122
  onHmrUpdates: async (result) => {
120
123
  if (result instanceof Error) {
121
124
  logWxError(server.config.logger, 'wx HMR update failed', result);
@@ -148,6 +151,7 @@ export async function createWxDevHost({ server, options }) {
148
151
  logWxError(server.config.logger, 'wx dev build failed', result);
149
152
  return;
150
153
  }
154
+ await publishWxDevStyle(result, server.config.build.outDir);
151
155
  // A fresh build identity per complete physical build; the App runtime reads it
152
156
  // from hmr/info.js before any module registers.
153
157
  await startFreshBuild();
@@ -185,13 +189,7 @@ export async function createWxDevHost({ server, options }) {
185
189
  sourcemap: false
186
190
  });
187
191
  rolldownOptions.experimental ??= {};
188
- rolldownOptions.experimental.devMode = {
189
- ...(typeof rolldownOptions.experimental.devMode === 'object'
190
- ? rolldownOptions.experimental.devMode
191
- : {}),
192
- implement: await bundleRuntimeSource(),
193
- lazy: false
194
- };
192
+ rolldownOptions.experimental.devMode = createWxDevMode(rolldownOptions.experimental.devMode, await bundleRuntimeSource());
195
193
  const emptyOutputDirectoryPlugin = {
196
194
  name: 'vpt:wx-empty-output-directory',
197
195
  renderStart: {
@@ -0,0 +1,11 @@
1
+ type DevOutputFile = Readonly<{
2
+ type: string;
3
+ fileName: string;
4
+ source?: string | Uint8Array;
5
+ }>;
6
+ type DevOutput = Readonly<{
7
+ output: readonly DevOutputFile[];
8
+ }>;
9
+ /** Publishes Vite bundled-dev's source-addressed global WXSS at WeChat's required root path. */
10
+ export declare function publishWxDevStyle(output: DevOutput, outDir: string): Promise<void>;
11
+ export {};
@@ -0,0 +1,12 @@
1
+ import { writeFile } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { adaptWxss } from '../../css/plugins.js';
4
+ /** Publishes Vite bundled-dev's source-addressed global WXSS at WeChat's required root path. */
5
+ export async function publishWxDevStyle(output, outDir) {
6
+ // WX disables CSS splitting, so the only non-empty WXSS asset is the application stylesheet; Page companions are empty.
7
+ const globalStyle = output.output.find((file) => file.type === 'asset' && file.fileName.endsWith('.wxss') && file.source && file.source.length > 0);
8
+ if (!globalStyle?.source)
9
+ return;
10
+ const source = typeof globalStyle.source === 'string' ? globalStyle.source : new TextDecoder().decode(globalStyle.source);
11
+ await writeFile(path.join(outDir, 'app.wxss'), await adaptWxss(source));
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-taro",
3
- "version": "0.4.2",
3
+ "version": "0.5.1",
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",
@@ -71,16 +71,16 @@
71
71
  "picocolors": "^1.1.1",
72
72
  "react-reconciler": "0.33.0",
73
73
  "react-refresh": "^0.18.0",
74
- "rolldown": "~1.2.2",
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.4.2",
78
- "@tarojs/react": "npm:vite-plugin-taro-react@0.4.2"
77
+ "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@0.5.1",
78
+ "@tarojs/react": "npm:vite-plugin-taro-react@0.5.1"
79
79
  },
80
80
  "peerDependencies": {
81
81
  "react": "^19.0.0",
82
82
  "react-dom": "^19.0.0",
83
- "vite": "^8.2.0"
83
+ "vite": "8.2.1"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@types/babel__core": "^7.20.5",
@@ -90,7 +90,7 @@
90
90
  "@types/react-dom": "^19.2.3",
91
91
  "@types/react-refresh": "^0.14.7",
92
92
  "typescript": "^7.0.2",
93
- "vite": "^8.2.0"
93
+ "vite": "8.2.1"
94
94
  },
95
95
  "scripts": {
96
96
  "build": "node ../../scripts/sync-plugin-readme.ts && node ../../scripts/clean-directory.ts dist && tsc --project tsconfig.build.json",
@@ -16,6 +16,13 @@ const wxStyleOptions = {
16
16
  px2rpx: true
17
17
  } as const
18
18
 
19
+ const transformWxss = createStyleHandler(wxStyleOptions)
20
+
21
+ /** Completes the final compatibility pass required by generated WXSS. */
22
+ export async function adaptWxss(source: string): Promise<string> {
23
+ return (await transformWxss(source)).css
24
+ }
25
+
19
26
  /** Creates the target-aware Tailwind CSS plugins. */
20
27
  export function createCssPlugins(target: VitePluginTaroTarget): PluginOption[] {
21
28
  const wx = target === 'wx'
@@ -60,10 +67,6 @@ export function createCssPlugins(target: VitePluginTaroTarget): PluginOption[] {
60
67
  * Remove it when upstream both completes adaptation and preserves the bundler-selected filename.
61
68
  */
62
69
  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
-
67
70
  return {
68
71
  name: 'vpt:wxss-compatibility-finalizer',
69
72
  enforce: 'post',
@@ -87,7 +90,7 @@ function createWxssCompatibilityFinalizer(): Plugin {
87
90
  ? globalStyleAsset.source
88
91
  : new TextDecoder().decode(globalStyleAsset.source)
89
92
  if (source.length > 0) {
90
- globalStyleAsset.source = (await transformWxss(source)).css
93
+ globalStyleAsset.source = await adaptWxss(source)
91
94
  }
92
95
  globalStyleAsset.fileName = 'app.wxss'
93
96
  }
@@ -0,0 +1,14 @@
1
+ import type { InputOptions } from 'rolldown'
2
+
3
+ type DevMode = NonNullable<NonNullable<InputOptions['experimental']>['devMode']>
4
+ type DevModeOptions = Exclude<DevMode, boolean>
5
+
6
+ /** Installs the custom WX runtime while restoring Rolldown's common runtime base. */
7
+ export function createWxDevMode(devMode: DevMode | undefined, implement: string): DevModeOptions {
8
+ return {
9
+ ...(typeof devMode === 'object' ? devMode : {}),
10
+ implement,
11
+ lazy: false,
12
+ skipCommonRuntimeInjection: false
13
+ }
14
+ }
@@ -9,6 +9,7 @@ import type { VitePluginTaroOptions } from '../../../../options.ts'
9
9
  import { once } from '../../../utils/once.ts'
10
10
  import { resolvePackageFile } from '../../../utils/packages.ts'
11
11
  import { appShellFileName } from '../module.ts'
12
+ import { createWxDevMode } from './create-wx-dev-mode.ts'
12
13
  import { emptyOutputDirectory } from './empty-output-directory.ts'
13
14
  import {
14
15
  type HmrInfo,
@@ -21,6 +22,7 @@ import {
21
22
  writeHmrFile
22
23
  } from './hmr-files.ts'
23
24
  import { PatchPublisher } from './patch-publisher.ts'
25
+ import { publishWxDevStyle } from './publish-wx-dev-style.ts'
24
26
 
25
27
  export type WxDevHost = Readonly<{
26
28
  close: () => Promise<void>
@@ -168,6 +170,7 @@ export async function createWxDevHost({
168
170
  }
169
171
 
170
172
  return dev(rolldownOptions, rolldownOptions.output, {
173
+ onAdditionalAssets: (output) => publishWxDevStyle(output, server.config.build.outDir),
171
174
  onHmrUpdates: async (result) => {
172
175
  if (result instanceof Error) {
173
176
  logWxError(server.config.logger, 'wx HMR update failed', result)
@@ -203,6 +206,7 @@ export async function createWxDevHost({
203
206
  logWxError(server.config.logger, 'wx dev build failed', result)
204
207
  return
205
208
  }
209
+ await publishWxDevStyle(result, server.config.build.outDir)
206
210
  // A fresh build identity per complete physical build; the App runtime reads it
207
211
  // from hmr/info.js before any module registers.
208
212
  await startFreshBuild()
@@ -245,13 +249,10 @@ export async function createWxDevHost({
245
249
  })
246
250
 
247
251
  rolldownOptions.experimental ??= {}
248
- rolldownOptions.experimental.devMode = {
249
- ...(typeof rolldownOptions.experimental.devMode === 'object'
250
- ? rolldownOptions.experimental.devMode
251
- : {}),
252
- implement: await bundleRuntimeSource(),
253
- lazy: false
254
- }
252
+ rolldownOptions.experimental.devMode = createWxDevMode(
253
+ rolldownOptions.experimental.devMode,
254
+ await bundleRuntimeSource()
255
+ )
255
256
 
256
257
  const emptyOutputDirectoryPlugin: Plugin = {
257
258
  name: 'vpt:wx-empty-output-directory',
@@ -0,0 +1,26 @@
1
+ import { writeFile } from 'node:fs/promises'
2
+ import path from 'node:path'
3
+ import { adaptWxss } from '../../css/plugins.ts'
4
+
5
+ type DevOutputFile = Readonly<{
6
+ type: string
7
+ fileName: string
8
+ source?: string | Uint8Array
9
+ }>
10
+
11
+ type DevOutput = Readonly<{
12
+ output: readonly DevOutputFile[]
13
+ }>
14
+
15
+ /** Publishes Vite bundled-dev's source-addressed global WXSS at WeChat's required root path. */
16
+ export async function publishWxDevStyle(output: DevOutput, outDir: string): Promise<void> {
17
+ // WX disables CSS splitting, so the only non-empty WXSS asset is the application stylesheet; Page companions are empty.
18
+ const globalStyle = output.output.find(
19
+ (file) => file.type === 'asset' && file.fileName.endsWith('.wxss') && file.source && file.source.length > 0
20
+ )
21
+ if (!globalStyle?.source) return
22
+
23
+ const source =
24
+ typeof globalStyle.source === 'string' ? globalStyle.source : new TextDecoder().decode(globalStyle.source)
25
+ await writeFile(path.join(outDir, 'app.wxss'), await adaptWxss(source))
26
+ }