@vitarx/plugin-vite 0.0.1-beta.2 → 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 +1 -1
- package/dist/index.js +12 -12
- package/dist/passes/imports/collectImports.d.ts +2 -0
- package/dist/passes/imports/collectImports.js +8 -0
- package/dist/passes/imports/injectImports.js +19 -2
- package/dist/{components.d.ts → types.d.ts} +24 -4
- package/package.json +1 -3
- /package/dist/{components.js → types.js} +0 -0
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
|
-
import {
|
|
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 (
|
|
8
|
+
if (IS_V8) {
|
|
9
|
+
// @ts-ignore
|
|
8
10
|
viteTransform = (await import('vite')).transformWithOxc;
|
|
9
11
|
}
|
|
10
12
|
else {
|
|
@@ -33,15 +35,11 @@ export default function vitarx(_options) {
|
|
|
33
35
|
isDEV = env.command === 'serve' && !env.isPreview;
|
|
34
36
|
const configSSR = !!config.build?.ssr;
|
|
35
37
|
isSSR = env.isSsrBuild === true || configSSR;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
esbuild: {
|
|
42
|
-
jsx: 'preserve',
|
|
43
|
-
exclude: /\.[jt]sx$/
|
|
44
|
-
},
|
|
38
|
+
const buildConfig = {
|
|
39
|
+
jsx: 'preserve',
|
|
40
|
+
exclude: /\.[jt]sx$/
|
|
41
|
+
};
|
|
42
|
+
const _config = {
|
|
45
43
|
define: {
|
|
46
44
|
__VITARX_DEV__: JSON.stringify(isDEV),
|
|
47
45
|
__VITARX_SSR__: JSON.stringify(isSSR)
|
|
@@ -51,7 +49,9 @@ export default function vitarx(_options) {
|
|
|
51
49
|
'@vitarx/vite-plugin/hmr-client': path.join(__dirname, 'hmr-client/index.js')
|
|
52
50
|
}
|
|
53
51
|
}
|
|
54
|
-
}
|
|
52
|
+
};
|
|
53
|
+
_config[IS_V8 ? 'oxc' : 'esbuild'] = buildConfig;
|
|
54
|
+
return _config;
|
|
55
55
|
},
|
|
56
56
|
configResolved(config) {
|
|
57
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' &&
|
|
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
|
+
"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",
|
|
@@ -52,8 +52,6 @@
|
|
|
52
52
|
"node": "^25.7.0",
|
|
53
53
|
"prettier": "^3.8.1",
|
|
54
54
|
"rimraf": "^6.1.3",
|
|
55
|
-
"vitarx": "^4.0.0-beta.0",
|
|
56
|
-
"vite": "^8.0.0-beta.16",
|
|
57
55
|
"vitest": "^4.0.18"
|
|
58
56
|
},
|
|
59
57
|
"scripts": {
|
|
File without changes
|