vite-plugin-taro 0.5.11 → 0.5.13

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.
Files changed (49) hide show
  1. package/dist/node/plugins/h5/plugins.js +22 -1
  2. package/dist/node/plugins/tailwind/tailwind-css.d.ts +1 -0
  3. package/dist/node/plugins/tailwind/tailwind-css.js +3 -0
  4. package/dist/node/plugins/wx/dev/dev-host.d.ts +6 -2
  5. package/dist/node/plugins/wx/dev/dev-host.js +71 -8
  6. package/dist/node/plugins/wx/dev/hmr-files.d.ts +9 -0
  7. package/dist/node/plugins/wx/dev/hmr-files.js +11 -0
  8. package/dist/node/plugins/wx/dev/plugins.d.ts +21 -2
  9. package/dist/node/plugins/wx/dev/plugins.js +38 -2
  10. package/dist/node/plugins/wx/dev/styles/create-style-capture-plugin.d.ts +12 -0
  11. package/dist/node/plugins/wx/dev/styles/create-style-capture-plugin.js +21 -0
  12. package/dist/node/plugins/wx/dev/styles/publish-style-hmr.d.ts +27 -0
  13. package/dist/node/plugins/wx/dev/styles/publish-style-hmr.js +48 -0
  14. package/dist/node/plugins/wx/dev/wx-dev-options.d.ts +3 -2
  15. package/dist/node/plugins/wx/dev/wx-dev-options.js +7 -2
  16. package/dist/node/plugins/wx/output/files.d.ts +1 -1
  17. package/dist/node/plugins/wx/output/files.js +6 -1
  18. package/dist/node/plugins/wx/plugins.js +13 -6
  19. package/dist/node/plugins/wx/resolve/resolver.d.ts +2 -1
  20. package/dist/node/plugins/wx/resolve/resolver.js +31 -17
  21. package/dist/node/plugins/wx/styles/plugins.d.ts +3 -0
  22. package/dist/node/plugins/wx/styles/plugins.js +92 -0
  23. package/dist/node/plugins/wx/styles/transform-wx-style.d.ts +8 -0
  24. package/dist/node/plugins/wx/styles/transform-wx-style.js +9 -0
  25. package/dist/node/plugins/wx/styles/utils.d.ts +39 -0
  26. package/dist/node/plugins/wx/styles/utils.js +95 -0
  27. package/dist/node/utils/vite.d.ts +17 -0
  28. package/dist/node/utils/vite.js +43 -0
  29. package/dist/node/vite-plugin.js +0 -2
  30. package/package.json +3 -3
  31. package/src/node/plugins/h5/plugins.ts +22 -1
  32. package/src/node/plugins/tailwind/tailwind-css.ts +4 -0
  33. package/src/node/plugins/wx/dev/dev-host.ts +88 -8
  34. package/src/node/plugins/wx/dev/hmr-files.ts +12 -0
  35. package/src/node/plugins/wx/dev/plugins.ts +44 -4
  36. package/src/node/plugins/wx/dev/styles/create-style-capture-plugin.ts +34 -0
  37. package/src/node/plugins/wx/dev/styles/publish-style-hmr.ts +71 -0
  38. package/src/node/plugins/wx/dev/wx-dev-options.ts +9 -2
  39. package/src/node/plugins/wx/output/files.ts +6 -1
  40. package/src/node/plugins/wx/plugins.ts +16 -6
  41. package/src/node/plugins/wx/resolve/resolver.ts +33 -17
  42. package/src/node/plugins/wx/styles/plugins.ts +105 -0
  43. package/src/node/plugins/wx/styles/transform-wx-style.ts +11 -0
  44. package/src/node/plugins/wx/styles/utils.ts +119 -0
  45. package/src/node/utils/vite.ts +67 -0
  46. package/src/node/vite-plugin.ts +0 -2
  47. package/dist/node/plugins/css/plugins.d.ts +0 -4
  48. package/dist/node/plugins/css/plugins.js +0 -119
  49. package/src/node/plugins/css/plugins.ts +0 -135
@@ -0,0 +1,67 @@
1
+ import type { HookHandler, Plugin, PluginOption } from 'vite'
2
+
3
+ type TransformHook = HookHandler<NonNullable<Plugin['transform']>>
4
+ export type TransformHookResult = Awaited<ReturnType<TransformHook>>
5
+ export type AsyncTransformHook = (
6
+ this: ThisParameterType<TransformHook>,
7
+ ...args: Parameters<TransformHook>
8
+ ) => Promise<TransformHookResult>
9
+ export type TransformHookWrapper = (transform: AsyncTransformHook) => AsyncTransformHook
10
+
11
+ export type PluginMapper = (plugin: Plugin) => Plugin
12
+
13
+ /** Transforms every concrete plugin while preserving nested arrays, falsy options, and promised options. */
14
+ export function transformVitePlugin(pluginOptions: PluginOption[], mapPlugin: PluginMapper): PluginOption[] {
15
+ return pluginOptions.map((option) => transformPluginOption(option, mapPlugin))
16
+ }
17
+
18
+ function transformPluginOption(option: PluginOption, mapPlugin: PluginMapper): PluginOption {
19
+ if (option instanceof Promise) {
20
+ return option.then((resolvedOption) => transformPluginOption(resolvedOption, mapPlugin))
21
+ }
22
+
23
+ if (Array.isArray(option)) {
24
+ return transformVitePlugin(option, mapPlugin)
25
+ }
26
+
27
+ return isPlugin(option) ? mapPlugin(option) : option
28
+ }
29
+
30
+ function isPlugin(option: PluginOption): option is Plugin {
31
+ return (
32
+ option !== null &&
33
+ option !== false &&
34
+ option !== undefined &&
35
+ typeof option === 'object' &&
36
+ !Array.isArray(option) &&
37
+ 'name' in option
38
+ )
39
+ }
40
+
41
+ /**
42
+ * Clones a Vite transform hook with middleware that controls execution of the original handler.
43
+ *
44
+ * Function and object hook forms retain their original plugin context. Object metadata such as `order` and `filter` is copied
45
+ * unchanged, and the input descriptor is never mutated. The wrapper receives the normalized asynchronous transform with its
46
+ * complete plugin context, code, ID, and metadata signature, and returns the handler that continues Vite's plugin pipeline.
47
+ */
48
+ export function wrapPluginTransform(plugin: Plugin, wrapper: TransformHookWrapper): Plugin {
49
+ const { transform } = plugin
50
+
51
+ if (!transform) {
52
+ throw new Error(`${plugin.name} must expose a transform hook`)
53
+ }
54
+
55
+ const isTransformFunction = typeof transform === 'function'
56
+
57
+ const handler = isTransformFunction ? transform : transform.handler
58
+
59
+ const wrappedHandler = wrapper(async function (code, id, meta) {
60
+ return handler.call(this, code, id, meta)
61
+ })
62
+
63
+ return {
64
+ ...plugin,
65
+ transform: isTransformFunction ? wrappedHandler : { ...transform, handler: wrappedHandler }
66
+ }
67
+ }
@@ -3,7 +3,6 @@ import type { PluginOption } from 'vite'
3
3
  import type { VitePluginTaroOptions } from '../options.ts'
4
4
  import { createClientTaroPlugin } from './plugins/client/client-taro.ts'
5
5
  import { createConditionalDirectivePlugin } from './plugins/conditional/conditional-directives.ts'
6
- import { createCssPlugins } from './plugins/css/plugins.ts'
7
6
  import { createH5TargetPlugins } from './plugins/h5/plugins.ts'
8
7
  import { createWxTargetPlugins } from './plugins/wx/plugins.ts'
9
8
 
@@ -12,7 +11,6 @@ export default function vitePluginTaro(options: VitePluginTaroOptions): PluginOp
12
11
  return [
13
12
  createConditionalDirectivePlugin(options.target),
14
13
  createClientTaroPlugin(options.target),
15
- ...createCssPlugins(options.target),
16
14
  ...react(),
17
15
  ...(options.target === 'wx' ? createWxTargetPlugins(options) : []),
18
16
  ...(options.target === 'h5' ? createH5TargetPlugins(options) : [])
@@ -1,4 +0,0 @@
1
- import type { PluginOption } from 'vite';
2
- import type { VitePluginTaroTarget } from '../../../options.ts';
3
- /** Creates the target-aware Tailwind pipeline. */
4
- export declare function createCssPlugins(target: VitePluginTaroTarget): PluginOption[];
@@ -1,119 +0,0 @@
1
- import path from 'node:path';
2
- import { createStyleHandler } from '@weapp-tailwindcss/postcss';
3
- import { WeappTailwindcss } from 'weapp-tailwindcss/vite';
4
- import { packageRequire } from '../../utils/packages.js';
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.
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.
21
- const wxStyleOptions = {
22
- cssCalc: false,
23
- autoprefixer: false,
24
- rem2rpx: true,
25
- px2rpx: true
26
- };
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. */
30
- export function createCssPlugins(target) {
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
- }) ?? [];
50
- return [
51
- ...(wx ? tailwindPlugins.map(alignWxGenerateBundleOrder) : tailwindPlugins),
52
- wx ? createWxStyleFinalizer() : undefined
53
- ];
54
- }
55
- /**
56
- * Finalizes the one global stylesheet after upstream Tailwind generation.
57
- *
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.
62
- */
63
- function createWxStyleFinalizer() {
64
- return {
65
- name: 'vpt:wx-style-finalizer',
66
- generateBundle: {
67
- order: 'post',
68
- async handler(_, bundle) {
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');
73
- }
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';
85
- }
86
- }
87
- };
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
- }
@@ -1,135 +0,0 @@
1
- import path from 'node:path'
2
- import { createStyleHandler } from '@weapp-tailwindcss/postcss'
3
- import type { Plugin, PluginOption, Rolldown } from 'vite'
4
- import { WeappTailwindcss } from 'weapp-tailwindcss/vite'
5
- import type { VitePluginTaroTarget } from '../../../options.ts'
6
- import { packageRequire } from '../../utils/packages.ts'
7
-
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.
22
- const tailwindcssBasedir = path.dirname(packageRequire.resolve('tailwindcss/package.json'))
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.
26
- const wxStyleOptions = {
27
- cssCalc: false,
28
- autoprefixer: false,
29
- rem2rpx: true,
30
- px2rpx: true
31
- } as const
32
-
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. */
37
- export function createCssPlugins(target: VitePluginTaroTarget): PluginOption[] {
38
- const wx = target === 'wx'
39
-
40
- const tailwindPlugins =
41
- WeappTailwindcss({
42
- // VPT is a custom Vite compiler. Using Taro's adapter would import Taro-specific CSS ownership rules.
43
- appType: 'weapp-vite',
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`.
46
- rewriteCssImports: wx,
47
- platform: wx ? 'weapp' : 'web',
48
- tailwindcssBasedir,
49
- generator: {
50
- target: wx ? 'weapp' : 'web'
51
- },
52
- cssOptions: {
53
- ...wxStyleOptions,
54
- // Browser output still needs vendor prefixes; WXSS does not support or need that browser pass.
55
- autoprefixer: !wx
56
- },
57
- logLevel: 'warn'
58
- }) ?? []
59
-
60
- return [
61
- ...(wx ? tailwindPlugins.map(alignWxGenerateBundleOrder) : tailwindPlugins),
62
- wx ? createWxStyleFinalizer() : undefined
63
- ]
64
- }
65
-
66
- /**
67
- * Finalizes the one global stylesheet after upstream Tailwind generation.
68
- *
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.
73
- */
74
- function createWxStyleFinalizer(): Plugin {
75
- return {
76
- name: 'vpt:wx-style-finalizer',
77
- generateBundle: {
78
- order: 'post',
79
- async handler(_, bundle) {
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')
84
- }
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'
96
- }
97
- }
98
- }
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
- }