@vitarx/plugin-vite 0.0.1-beta.3 → 0.0.1-beta.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.
@@ -27,7 +27,8 @@ export default class HMRManager {
27
27
  /**
28
28
  * 置换新模块
29
29
  *
30
- * 此方法提供给`jsxDev`函数调用,保持每次创建组件实例都是最新的模块!
30
+ * 此方法提供给`createView`函数调用,保持每次创建组件实例都是最新的模块!
31
+ *
31
32
  * @param component
32
33
  */
33
34
  resolveComponent(component: Component): Component;
@@ -51,7 +51,8 @@ class HMRManager {
51
51
  /**
52
52
  * 置换新模块
53
53
  *
54
- * 此方法提供给`jsxDev`函数调用,保持每次创建组件实例都是最新的模块!
54
+ * 此方法提供给`createView`函数调用,保持每次创建组件实例都是最新的模块!
55
+ *
55
56
  * @param component
56
57
  */
57
58
  resolveComponent(component) {
@@ -21,7 +21,9 @@ export function processUpdate(view, newComponent) {
21
21
  view.component = newComponent;
22
22
  // 如果逻辑有变化,则完全重新挂载组件
23
23
  if (logic) {
24
+ const ctx = view.ctx;
24
25
  view.dispose();
26
+ view.init(ctx);
25
27
  view.mount(placeholder, 'replace');
26
28
  return;
27
29
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type Plugin } from 'vite';
2
- export type * from './components.js';
2
+ export type * from './types.js';
3
3
  /**
4
4
  * vite-plugin-vitarx 配置选项
5
5
  *
package/dist/index.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import path from 'node:path';
2
2
  import { fileURLToPath } from 'node:url';
3
- import { defineConfig, version } from 'vite';
3
+ import { version } from 'vite';
4
4
  import { transform } from './transform.js';
5
5
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
6
+ const IS_V8 = version.startsWith('8');
6
7
  let viteTransform;
7
- if (version.startsWith('8')) {
8
+ if (IS_V8) {
8
9
  // @ts-ignore
9
10
  viteTransform = (await import('vite')).transformWithOxc;
10
11
  }
@@ -34,16 +35,11 @@ export default function vitarx(_options) {
34
35
  isDEV = env.command === 'serve' && !env.isPreview;
35
36
  const configSSR = !!config.build?.ssr;
36
37
  isSSR = env.isSsrBuild === true || configSSR;
37
- return defineConfig({
38
- // @ts-ignore
39
- oxc: {
40
- jsx: 'preserve',
41
- exclude: /\.[jt]sx$/
42
- },
43
- esbuild: {
44
- jsx: 'preserve',
45
- exclude: /\.[jt]sx$/
46
- },
38
+ const buildConfig = {
39
+ jsx: 'preserve',
40
+ exclude: /\.[jt]sx$/
41
+ };
42
+ const _config = {
47
43
  define: {
48
44
  __VITARX_DEV__: JSON.stringify(isDEV),
49
45
  __VITARX_SSR__: JSON.stringify(isSSR)
@@ -53,7 +49,9 @@ export default function vitarx(_options) {
53
49
  '@vitarx/vite-plugin/hmr-client': path.join(__dirname, 'hmr-client/index.js')
54
50
  }
55
51
  }
56
- });
52
+ };
53
+ _config[IS_V8 ? 'oxc' : 'esbuild'] = buildConfig;
54
+ return _config;
57
55
  },
58
56
  configResolved(config) {
59
57
  viteConfig = config;
@@ -6,6 +6,7 @@
6
6
  import * as t from '@babel/types';
7
7
  /**
8
8
  * 收集现有导入信息
9
+ * 只收集普通的 import 语句,跳过 import type 语句
9
10
  * @param program - AST Program 节点
10
11
  * @returns 本地变量名集合和 vitarx 导入映射
11
12
  */
@@ -16,6 +17,7 @@ export declare function collectExistingImports(program: t.Program): {
16
17
  /**
17
18
  * 收集本地变量绑定
18
19
  * 包括导入绑定、变量声明、函数声明、类声明
20
+ * 只收集运行时存在的绑定,跳过 import type
19
21
  * @param program - AST Program 节点
20
22
  * @returns 本地变量名集合
21
23
  */
@@ -2,6 +2,7 @@ import { VITARX_MODULE } from '../../constants/index.js';
2
2
  import { collectPatternBindings } from '../../utils/index.js';
3
3
  /**
4
4
  * 收集现有导入信息
5
+ * 只收集普通的 import 语句,跳过 import type 语句
5
6
  * @param program - AST Program 节点
6
7
  * @returns 本地变量名集合和 vitarx 导入映射
7
8
  */
@@ -11,6 +12,9 @@ export function collectExistingImports(program) {
11
12
  for (const node of program.body) {
12
13
  if (node.type !== 'ImportDeclaration')
13
14
  continue;
15
+ // 跳过 import type 和 import typeof 语句
16
+ if (node.importKind === 'type' || node.importKind === 'typeof')
17
+ continue;
14
18
  const source = node.source.value;
15
19
  for (const specifier of node.specifiers) {
16
20
  localNames.add(specifier.local.name);
@@ -27,6 +31,7 @@ export function collectExistingImports(program) {
27
31
  /**
28
32
  * 收集本地变量绑定
29
33
  * 包括导入绑定、变量声明、函数声明、类声明
34
+ * 只收集运行时存在的绑定,跳过 import type
30
35
  * @param program - AST Program 节点
31
36
  * @returns 本地变量名集合
32
37
  */
@@ -34,6 +39,9 @@ export function collectLocalBindings(program) {
34
39
  const bindings = new Set();
35
40
  for (const node of program.body) {
36
41
  if (node.type === 'ImportDeclaration') {
42
+ // 跳过 import type 和 import typeof 语句
43
+ if (node.importKind === 'type' || node.importKind === 'typeof')
44
+ continue;
37
45
  for (const specifier of node.specifiers) {
38
46
  bindings.add(specifier.local.name);
39
47
  }
@@ -29,7 +29,7 @@ export function injectImports(program, ctx) {
29
29
  if (!needsInject(ctx))
30
30
  return;
31
31
  const existingVitarxImport = findExistingVitarxImport(program);
32
- if (existingVitarxImport) {
32
+ if (existingVitarxImport && canAppendSpecifiers(existingVitarxImport)) {
33
33
  appendImportSpecifiers(existingVitarxImport, ctx);
34
34
  }
35
35
  else {
@@ -40,12 +40,29 @@ export function injectImports(program, ctx) {
40
40
  program.body.unshift(importDecl);
41
41
  }
42
42
  }
43
+ /**
44
+ * 检查是否可以向导入语句追加说明符
45
+ * 只有纯命名导入才能追加,命名空间导入和默认导入不能追加
46
+ */
47
+ function canAppendSpecifiers(importDecl) {
48
+ for (const spec of importDecl.specifiers) {
49
+ // 如果存在命名空间导入或默认导入,不能追加
50
+ if (spec.type === 'ImportNamespaceSpecifier' || spec.type === 'ImportDefaultSpecifier') {
51
+ return false;
52
+ }
53
+ }
54
+ return true;
55
+ }
43
56
  /**
44
57
  * 查找已存在的 vitarx 导入语句
58
+ * 只查找普通的 import 语句,跳过 import type 语句
45
59
  */
46
60
  function findExistingVitarxImport(program) {
47
61
  for (const node of program.body) {
48
- if (node.type === 'ImportDeclaration' && node.source.value === VITARX_MODULE) {
62
+ if (node.type === 'ImportDeclaration' &&
63
+ node.source.value === VITARX_MODULE &&
64
+ node.importKind !== 'type' &&
65
+ node.importKind !== 'typeof') {
49
66
  return node;
50
67
  }
51
68
  }
@@ -1,5 +1,5 @@
1
1
  import type { RenderUnit, View } from 'vitarx';
2
- interface IfBlockProps {
2
+ export interface IfBlockProps {
3
3
  /**
4
4
  * 子元素列表
5
5
  *
@@ -7,7 +7,7 @@ interface IfBlockProps {
7
7
  */
8
8
  children: View[];
9
9
  }
10
- interface SwitchProps {
10
+ export interface SwitchProps {
11
11
  /**
12
12
  * 默认渲染内容
13
13
  *
@@ -21,7 +21,7 @@ interface SwitchProps {
21
21
  */
22
22
  children: View | View[];
23
23
  }
24
- interface MatchProps {
24
+ export interface MatchProps {
25
25
  /**
26
26
  * 匹配条件
27
27
  */
@@ -32,6 +32,50 @@ interface MatchProps {
32
32
  children: RenderUnit;
33
33
  }
34
34
  declare global {
35
+ namespace Vitarx {
36
+ interface IntrinsicAttributes {
37
+ /**
38
+ * 条件渲染指令 - v-if
39
+ *
40
+ * 用于条件性地渲染元素。
41
+ * 当表达式的值为真时,元素将被渲染;否则,元素不会被渲染到DOM中。
42
+ *
43
+ * ```tsx
44
+ * const show = ref(true)
45
+ * // v-if指令不支持 ref 自动解包,必须使用.value
46
+ * <div v-if={show.value}></div>
47
+ * ```
48
+ */
49
+ 'v-if'?: unknown;
50
+ /**
51
+ * 条件渲染指令 - v-else
52
+ *
53
+ * 用于表示else块。
54
+ * 必须紧跟在v-if或v-else-if元素后面。
55
+ *
56
+ * ```tsx
57
+ * const show = ref(false)
58
+ * <div v-if={show.value}></div>
59
+ * <div v-else></div>
60
+ * ```
61
+ */
62
+ 'v-else'?: unknown;
63
+ /**
64
+ * 条件渲染指令 - v-else-if
65
+ *
66
+ * 用于表示else-if块。
67
+ * 必须紧跟在v-if或v-else-if元素后面。
68
+ *
69
+ * ```tsx
70
+ * const show = ref(1)
71
+ * <div v-if={show.value === 0}></div>
72
+ * <div v-else-if={show.value === 1}></div>
73
+ * <div v-else></div>
74
+ * ```
75
+ */
76
+ 'v-else-if'?: unknown;
77
+ }
78
+ }
35
79
  /**
36
80
  * IfBlock - 编译宏组件,无需import导入
37
81
  *
@@ -82,5 +126,25 @@ declare global {
82
126
  * 需 `Switch` 组件搭配使用,不允许单独使用。
83
127
  */
84
128
  const Match: (props: MatchProps) => View;
129
+ /**
130
+ * 开发环境
131
+ *
132
+ * @example
133
+ * ```js
134
+ * // 等效于 import.meta.env.DEV
135
+ * if(__VITARX_DEV__){
136
+ * // 仅在开发环境下运行的代码
137
+ * }
138
+ * ```
139
+ */
140
+ const __VITARX_DEV__: boolean;
141
+ /**
142
+ * SSR环境
143
+ *
144
+ * // 等效于 import.meta.env.SSR
145
+ * if(__VITARX_SSR__){
146
+ * // 仅在服务端运行的代码
147
+ * }
148
+ */
149
+ const __VITARX_SSR__: boolean;
85
150
  }
86
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/plugin-vite",
3
- "version": "0.0.1-beta.3",
3
+ "version": "0.0.1-beta.5",
4
4
  "description": "The official plugin for Vitarx support in Vite.",
5
5
  "author": "ZhuChongLin <8210856@qq.com>",
6
6
  "license": "MIT",
File without changes