@vitarx/plugin-vite 0.0.1-beta.3 → 0.0.1-beta.4

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/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
  */
@@ -82,5 +82,25 @@ declare global {
82
82
  * 需 `Switch` 组件搭配使用,不允许单独使用。
83
83
  */
84
84
  const Match: (props: MatchProps) => View;
85
+ /**
86
+ * 开发环境
87
+ *
88
+ * @example
89
+ * ```js
90
+ * // 等效于 import.meta.env.DEV
91
+ * if(__VITARX_DEV__){
92
+ * // 仅在开发环境下运行的代码
93
+ * }
94
+ * ```
95
+ */
96
+ const __VITARX_DEV__: boolean;
97
+ /**
98
+ * SSR环境
99
+ *
100
+ * // 等效于 import.meta.env.SSR
101
+ * if(__VITARX_SSR__){
102
+ * // 仅在服务端运行的代码
103
+ * }
104
+ */
105
+ const __VITARX_SSR__: boolean;
85
106
  }
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.4",
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