vite-plugin-taro 0.5.4 → 0.5.5

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 (31) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/dist/node/plugins/h5/create-stencil-client-adapter.d.ts +10 -13
  4. package/dist/node/plugins/h5/create-stencil-client-adapter.js +63 -51
  5. package/dist/node/plugins/h5/plugins.d.ts +10 -0
  6. package/dist/node/plugins/h5/plugins.js +39 -11
  7. package/dist/node/plugins/wx/dev/dev-host.js +103 -216
  8. package/dist/node/plugins/wx/dev/hmr-files.d.ts +4 -5
  9. package/dist/node/plugins/wx/dev/hmr-files.js +18 -8
  10. package/dist/node/plugins/wx/dev/patch-publisher.d.ts +25 -5
  11. package/dist/node/plugins/wx/dev/patch-publisher.js +30 -9
  12. package/dist/node/plugins/wx/dev/react-refresh.d.ts +12 -0
  13. package/dist/node/plugins/wx/dev/react-refresh.js +111 -108
  14. package/dist/node/plugins/wx/dev/wx-dev-options.d.ts +25 -0
  15. package/dist/node/plugins/wx/dev/wx-dev-options.js +147 -0
  16. package/dist/node/utils/oxc-transform.d.ts +21 -0
  17. package/dist/node/utils/oxc-transform.js +58 -0
  18. package/dist/node/utils/serialized-task-queue.d.ts +6 -2
  19. package/dist/node/utils/serialized-task-queue.js +10 -2
  20. package/dist/runtime/wx/dev/dev-runtime.js +70 -26
  21. package/package.json +4 -3
  22. package/src/node/plugins/h5/create-stencil-client-adapter.ts +74 -73
  23. package/src/node/plugins/h5/plugins.ts +42 -13
  24. package/src/node/plugins/wx/dev/dev-host.ts +121 -256
  25. package/src/node/plugins/wx/dev/hmr-files.ts +20 -8
  26. package/src/node/plugins/wx/dev/patch-publisher.ts +36 -10
  27. package/src/node/plugins/wx/dev/react-refresh.ts +125 -129
  28. package/src/node/plugins/wx/dev/wx-dev-options.ts +200 -0
  29. package/src/node/utils/oxc-transform.ts +77 -0
  30. package/src/node/utils/serialized-task-queue.ts +15 -3
  31. package/src/runtime/wx/dev/dev-runtime.ts +80 -28
package/README.en.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # vite-plugin-taro
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/vite-plugin-taro.svg)](https://www.npmjs.com/package/vite-plugin-taro)
4
- ![Vite compatibility](https://registry.vite.dev/api/badges?package=vite-plugin-taro&tool=vite)
4
+ ![Vite](https://img.shields.io/npm/dependency-version/vite-plugin-taro/peer/vite?label=Vite)
5
5
  [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
6
6
 
7
7
  [简体中文](README.md) | English
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # vite-plugin-taro
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/vite-plugin-taro.svg)](https://www.npmjs.com/package/vite-plugin-taro)
4
- ![Vite compatibility](https://registry.vite.dev/api/badges?package=vite-plugin-taro&tool=vite)
4
+ ![Vite](https://img.shields.io/npm/dependency-version/vite-plugin-taro/peer/vite?label=Vite)
5
5
  [![license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
6
6
 
7
7
  简体中文 | [English](README.en.md)
@@ -9,16 +9,13 @@ import type { Plugin } from 'vite';
9
9
  * exclusion while keeping one transformation implementation.
10
10
  */
11
11
  export declare function createStencilClientAdapter(): Plugin;
12
- /** Applies the shared Stencil adaptation in either the application or dependency-optimization pipeline. */
13
- export declare function adaptStencilClient(code: string, id: string): Promise<{
14
- code: string;
15
- map: {
16
- version: number;
17
- sources: string[];
18
- names: string[];
19
- sourceRoot?: string | undefined;
20
- sourcesContent?: string[] | undefined;
21
- mappings: string;
22
- file: string;
23
- } | null | undefined;
24
- } | undefined>;
12
+ /**
13
+ * Applies the shared Stencil adaptation with one Oxc parse and one range edit.
14
+ *
15
+ * The physical module check is essential because the optimizer and application pipelines can
16
+ * present query-suffixed or platform-normalized IDs. Transforming by source text alone could
17
+ * modify user code that happens to contain the same insertion expression. Source maps remain
18
+ * enabled because this adapter runs before Vite/Rolldown's later transforms in both pipelines;
19
+ * dropping the map would attribute downstream diagnostics to the edited generated positions.
20
+ */
21
+ export declare function adaptStencilClient(code: string, id: string): import("../../utils/transform.ts").AstTransformResult | undefined;
@@ -1,5 +1,5 @@
1
- import { transformAsync, types } from '@babel/core';
2
1
  import { normalizeModuleId } from '../../utils/modules.js';
2
+ import { transformWithOxcWalker } from '../../utils/oxc-transform.js';
3
3
  import { packageRequire } from '../../utils/packages.js';
4
4
  const stencilClientPath = packageRequire.resolve('@stencil/core/internal/client', {
5
5
  paths: [packageRequire.resolve('@tarojs/components/package.json')]
@@ -20,61 +20,73 @@ export function createStencilClientAdapter() {
20
20
  transform: adaptStencilClient
21
21
  };
22
22
  }
23
- /** Applies the shared Stencil adaptation in either the application or dependency-optimization pipeline. */
24
- export async function adaptStencilClient(code, id) {
23
+ /**
24
+ * Applies the shared Stencil adaptation with one Oxc parse and one range edit.
25
+ *
26
+ * The physical module check is essential because the optimizer and application pipelines can
27
+ * present query-suffixed or platform-normalized IDs. Transforming by source text alone could
28
+ * modify user code that happens to contain the same insertion expression. Source maps remain
29
+ * enabled because this adapter runs before Vite/Rolldown's later transforms in both pipelines;
30
+ * dropping the map would attribute downstream diagnostics to the edited generated positions.
31
+ */
32
+ export function adaptStencilClient(code, id) {
25
33
  if (normalizeModuleId(id) !== normalizedStencilClientPath) {
26
34
  return;
27
35
  }
28
- const transformed = await transformAsync(code, {
29
- babelrc: false,
30
- configFile: false,
31
- filename: stencilClientPath,
32
- plugins: [rewriteStencilStyleInsertion],
33
- sourceFileName: stencilClientPath,
34
- sourceMaps: true
36
+ return transformWithOxcWalker({
37
+ code,
38
+ filename: id,
39
+ sourcemap: true,
40
+ createVisitor: createStencilVisitor
35
41
  });
36
- if (transformed?.code === undefined || transformed.code === null) {
37
- throw new Error(`Failed to adapt Stencil client: ${stencilClientPath}`);
38
- }
39
- return {
40
- code: transformed.code,
41
- map: transformed.map
42
- };
43
42
  }
44
- /** Keeps Stencil-injected Taro component styles before application stylesheets. */
45
- function rewriteStencilStyleInsertion() {
46
- return {
47
- name: 'vpt:rewrite-stencil-style-insertion',
48
- visitor: {
49
- CallExpression(callPath) {
50
- if (!isStencilStyleInsertBeforeCall(callPath)) {
51
- return;
52
- }
53
- callPath
54
- .get('arguments.1')
55
- .replaceWith(types.conditionalExpression(types.callExpression(types.memberExpression(types.identifier('scopeId'), types.identifier('startsWith')), [types.stringLiteral('sc-taro-')]), createStyleQuery('style,link[rel="stylesheet"]'), createStyleQuery('link')));
56
- }
43
+ /**
44
+ * Finds Stencil's exact default style insertion call and replaces only its insertion anchor.
45
+ *
46
+ * Stencil normally inserts `styleElm` before the first stylesheet link. During Vite development,
47
+ * application CSS is commonly represented by a `<style>` element instead, so that link-only
48
+ * lookup returns `null` and Taro component defaults are appended after application CSS. Those
49
+ * defaults then override the application's selectors despite having the same specificity.
50
+ *
51
+ * Taro Stencil components are identified by the `sc-taro-` scope prefix. For those components,
52
+ * the replacement anchors before the first `<style>` or stylesheet `<link>`, keeping component
53
+ * defaults before application rules. The original link-only lookup is preserved for every other
54
+ * Stencil component so VPT does not globally redefine upstream style-ordering behavior.
55
+ */
56
+ function createStencilVisitor(editor) {
57
+ return function enter(node) {
58
+ if (node.type !== 'CallExpression' ||
59
+ node.callee.type !== 'MemberExpression' ||
60
+ node.callee.computed ||
61
+ node.callee.object.type !== 'Identifier' ||
62
+ node.callee.object.name !== 'styleContainerNode' ||
63
+ node.callee.property.type !== 'Identifier' ||
64
+ node.callee.property.name !== 'insertBefore') {
65
+ return;
66
+ }
67
+ // Match the complete upstream call, not merely `insertBefore`: range edits have no
68
+ // generated-AST type safety, so a broad match could silently alter unrelated runtime
69
+ // behavior when Stencil changes its implementation.
70
+ const [style, query] = node.arguments;
71
+ const selector = query?.type === 'CallExpression' ? query.arguments[0] : undefined;
72
+ if (node.arguments.length !== 2 ||
73
+ style?.type !== 'Identifier' ||
74
+ style.name !== 'styleElm' ||
75
+ query?.type !== 'CallExpression' ||
76
+ query.callee.type !== 'MemberExpression' ||
77
+ query.callee.computed ||
78
+ query.callee.object.type !== 'Identifier' ||
79
+ query.callee.object.name !== 'styleContainerNode' ||
80
+ query.callee.property.type !== 'Identifier' ||
81
+ query.callee.property.name !== 'querySelector' ||
82
+ selector?.type !== 'Literal' ||
83
+ selector.value !== 'link') {
84
+ return;
57
85
  }
86
+ // Replace only the second argument expression. Preserving the surrounding Stencil
87
+ // source avoids Babel-style whole-file regeneration and keeps its formatting, comments,
88
+ // and source positions stable. Removing this edit restores the H5 cascade bug described
89
+ // above; applying it unconditionally would change non-Taro Stencil components.
90
+ editor.overwrite(query.start, query.end, 'scopeId.startsWith("sc-taro-") ? styleContainerNode.querySelector("style,link[rel=\\"stylesheet\\"]") : styleContainerNode.querySelector("link")');
58
91
  };
59
92
  }
60
- /** Identifies Stencil's default component-style insertion call. */
61
- function isStencilStyleInsertBeforeCall(callPath) {
62
- const { callee, arguments: callArguments } = callPath.node;
63
- return (types.isMemberExpression(callee) &&
64
- types.isIdentifier(callee.object, { name: 'styleContainerNode' }) &&
65
- types.isIdentifier(callee.property, { name: 'insertBefore' }) &&
66
- types.isIdentifier(callArguments[0], { name: 'styleElm' }) &&
67
- isStyleQuery(callArguments[1], 'link'));
68
- }
69
- /** Identifies one style-container querySelector call. */
70
- function isStyleQuery(node, selector) {
71
- return (types.isCallExpression(node) &&
72
- types.isMemberExpression(node.callee) &&
73
- types.isIdentifier(node.callee.object, { name: 'styleContainerNode' }) &&
74
- types.isIdentifier(node.callee.property, { name: 'querySelector' }) &&
75
- types.isStringLiteral(node.arguments[0], { value: selector }));
76
- }
77
- /** Creates one style-container querySelector call. */
78
- function createStyleQuery(selector) {
79
- return types.callExpression(types.memberExpression(types.identifier('styleContainerNode'), types.identifier('querySelector')), [types.stringLiteral(selector)]);
80
- }
@@ -2,3 +2,13 @@ import type { PluginOption } from 'vite';
2
2
  import type { VitePluginTaroOptions } from '../../../options.ts';
3
3
  /** Creates the plugins that own the H5 target. */
4
4
  export declare function createH5TargetPlugins(options: VitePluginTaroOptions): PluginOption[];
5
+ /**
6
+ * Coarse source prefilter for the upstream Taro API transform.
7
+ *
8
+ * The plugin has two independent responsibilities: adapting imports from the compiler facade and normalizing camel-case
9
+ * H5 ARIA attributes. Matching `aria` followed by any uppercase letter is intentionally broader than Taro's current
10
+ * attribute table. A duplicated exact list would silently stop routing files through Babel when upstream adds another
11
+ * attribute, while a broad false positive costs only one unnecessary transform. Ordinary application modules match
12
+ * neither branch and remain entirely outside Babel's parser and generator.
13
+ */
14
+ export declare const h5TaroApiTransformCodeFilter: RegExp;
@@ -1,4 +1,4 @@
1
- import babel from '@rolldown/plugin-babel';
1
+ import babel, { defineRolldownBabelPreset } from '@rolldown/plugin-babel';
2
2
  import { esTarget } from '../../utils/constant.js';
3
3
  import { toViteFileImportPath } from '../../utils/modules.js';
4
4
  import { packageRequire } from '../../utils/packages.js';
@@ -88,20 +88,48 @@ function createH5IndexHtmlTags() {
88
88
  }
89
89
  ];
90
90
  }
91
- /** Creates H5-only Babel transforms for Stencil CSS ordering and Taro API imports. */
91
+ /**
92
+ * Coarse source prefilter for the upstream Taro API transform.
93
+ *
94
+ * The plugin has two independent responsibilities: adapting imports from the compiler facade and normalizing camel-case
95
+ * H5 ARIA attributes. Matching `aria` followed by any uppercase letter is intentionally broader than Taro's current
96
+ * attribute table. A duplicated exact list would silently stop routing files through Babel when upstream adds another
97
+ * attribute, while a broad false positive costs only one unnecessary transform. Ordinary application modules match
98
+ * neither branch and remain entirely outside Babel's parser and generator.
99
+ */
100
+ export const h5TaroApiTransformCodeFilter = /virtual:taro\/api|\baria[A-Z]/;
101
+ /** Creates a filterable Babel preset containing Taro's upstream, scope-aware API transform. */
102
+ function createH5TaroApiPreset() {
103
+ const transformTaroApiPath = packageRequire.resolve('babel-plugin-transform-taroapi');
104
+ const definition = packageRequire(packageRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json'));
105
+ return defineRolldownBabelPreset({
106
+ preset: function h5TaroApiPreset() {
107
+ return {
108
+ plugins: [
109
+ [
110
+ transformTaroApiPath,
111
+ {
112
+ packageName: clientTaroApiId,
113
+ definition
114
+ }
115
+ ]
116
+ ]
117
+ };
118
+ },
119
+ rolldown: {
120
+ // @rolldown/plugin-babel can lift a preset filter into its native transform hook. The Taro plugin must be
121
+ // nested in this preset rather than passed through Babel's top-level `plugins`: explicit plugins may apply
122
+ // to every module, so their presence deliberately disables Rolldown's preset-level code filtering.
123
+ filter: { code: h5TaroApiTransformCodeFilter }
124
+ }
125
+ });
126
+ }
127
+ /** Creates H5-only transforms for Stencil CSS ordering and Taro API imports. */
92
128
  function createH5SupportPlugins() {
93
129
  return [
94
130
  createStencilClientAdapter(),
95
131
  babel({
96
- plugins: [
97
- [
98
- packageRequire.resolve('babel-plugin-transform-taroapi'),
99
- {
100
- packageName: clientTaroApiId,
101
- definition: packageRequire(packageRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json'))
102
- }
103
- ]
104
- ]
132
+ presets: [createH5TaroApiPreset()]
105
133
  })
106
134
  ];
107
135
  }