@vue.ts/complex-types 0.6.0 → 1.0.0-beta.10
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/LICENSE +21 -21
- package/README.md +20 -5
- package/dist/astro.d.ts +8 -10
- package/dist/astro.js +14 -0
- package/dist/esbuild.d.ts +7 -8
- package/dist/esbuild.js +8 -0
- package/dist/farm.d.ts +8 -0
- package/dist/farm.js +8 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.js +3 -0
- package/dist/nuxt.d.ts +7 -8
- package/dist/nuxt.js +20 -0
- package/dist/rolldown.d.ts +8 -0
- package/dist/rolldown.js +8 -0
- package/dist/rollup.d.ts +7 -8
- package/dist/rollup.js +8 -0
- package/dist/rspack.d.ts +5 -6
- package/dist/rspack.js +8 -0
- package/dist/src-Ci8DJn1t.js +228 -0
- package/dist/types-B0cfnWIB.d.ts +37 -0
- package/dist/types-u_Hr5uXA.d.ts +1 -0
- package/dist/types.d.ts +3 -30
- package/dist/types.js +1 -0
- package/dist/vite-D-Xfh8NL.js +8 -0
- package/dist/vite.d.ts +7 -8
- package/dist/vite.js +4 -0
- package/dist/webpack-By_BEclB.js +8 -0
- package/dist/webpack.d.ts +7 -8
- package/dist/webpack.js +4 -0
- package/package.json +71 -54
- package/dist/astro.cjs +0 -22
- package/dist/astro.d.cts +0 -13
- package/dist/astro.d.mts +0 -13
- package/dist/astro.mjs +0 -20
- package/dist/esbuild.cjs +0 -13
- package/dist/esbuild.d.cts +0 -9
- package/dist/esbuild.d.mts +0 -9
- package/dist/esbuild.mjs +0 -11
- package/dist/index.cjs +0 -238
- package/dist/index.d.cts +0 -9
- package/dist/index.d.mts +0 -9
- package/dist/index.mjs +0 -231
- package/dist/nuxt.cjs +0 -35
- package/dist/nuxt.d.cts +0 -9
- package/dist/nuxt.d.mts +0 -9
- package/dist/nuxt.mjs +0 -33
- package/dist/rollup.cjs +0 -13
- package/dist/rollup.d.cts +0 -9
- package/dist/rollup.d.mts +0 -9
- package/dist/rollup.mjs +0 -11
- package/dist/rspack.cjs +0 -13
- package/dist/rspack.d.cts +0 -8
- package/dist/rspack.d.mts +0 -8
- package/dist/rspack.mjs +0 -11
- package/dist/types.cjs +0 -2
- package/dist/types.d.cts +0 -30
- package/dist/types.d.mts +0 -30
- package/dist/types.mjs +0 -1
- package/dist/vite.cjs +0 -13
- package/dist/vite.d.cts +0 -9
- package/dist/vite.d.mts +0 -9
- package/dist/vite.mjs +0 -11
- package/dist/webpack.cjs +0 -13
- package/dist/webpack.d.cts +0 -9
- package/dist/webpack.d.mts +0 -9
- package/dist/webpack.mjs +0 -11
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Ray <https://github.com/so1ve>
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Ray <https://github.com/so1ve>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@vue.ts/complex-types)
|
|
4
4
|
|
|
5
|
-
Use [`@vue/language-core`](https://github.com/vuejs/language-tools/tree/master/packages/language-core) to support complex types for Vue Macros.
|
|
5
|
+
Use [`@vue/language-core`](https://github.com/vuejs/language-tools/tree/master/packages/language-core) to support complex types for Vue's Macros.
|
|
6
6
|
|
|
7
7
|
For example: fixes https://github.com/vuejs/core/issues/8286.
|
|
8
8
|
|
|
@@ -14,10 +14,6 @@ $ yarn add -D @vue.ts/complex-types
|
|
|
14
14
|
$ pnpm add -D @vue.ts/complex-types
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
## TODOs
|
|
18
|
-
|
|
19
|
-
- [ ] Add more tests
|
|
20
|
-
|
|
21
17
|
## 🚀 Usage
|
|
22
18
|
|
|
23
19
|
<details>
|
|
@@ -38,6 +34,25 @@ export default defineConfig({
|
|
|
38
34
|
|
|
39
35
|
<br></details>
|
|
40
36
|
|
|
37
|
+
<details>
|
|
38
|
+
<summary>Rolldown</summary><br>
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// rolldown.config.js
|
|
42
|
+
import VueComplexTypes from "@vue.ts/complex-types/rolldown";
|
|
43
|
+
|
|
44
|
+
export default {
|
|
45
|
+
plugins: [
|
|
46
|
+
VueComplexTypes({
|
|
47
|
+
/* options */
|
|
48
|
+
}),
|
|
49
|
+
// other plugins
|
|
50
|
+
],
|
|
51
|
+
};
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
<br></details>
|
|
55
|
+
|
|
41
56
|
<details>
|
|
42
57
|
<summary>Rollup</summary><br>
|
|
43
58
|
|
package/dist/astro.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { Options } from
|
|
2
|
-
import '@vue.ts/common';
|
|
3
|
-
import 'magic-string';
|
|
4
|
-
import 'typescript';
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
5
2
|
|
|
3
|
+
//#region src/astro.d.ts
|
|
6
4
|
declare const _default: (options: Options) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
name: string;
|
|
6
|
+
hooks: {
|
|
7
|
+
"astro:config:setup": (astro: any) => Promise<void>;
|
|
8
|
+
};
|
|
11
9
|
};
|
|
12
|
-
|
|
13
|
-
export { _default as default };
|
|
10
|
+
//#endregion
|
|
11
|
+
export { _default as default };
|
package/dist/astro.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { r as unpluginFactory } from "./src-Ci8DJn1t.js";
|
|
2
|
+
import { createVitePlugin } from "unplugin";
|
|
3
|
+
|
|
4
|
+
//#region src/astro.ts
|
|
5
|
+
var astro_default = (options) => ({
|
|
6
|
+
name: "@vue.ts/complex-types",
|
|
7
|
+
hooks: { "astro:config:setup": async (astro) => {
|
|
8
|
+
astro.config.vite.plugins ??= [];
|
|
9
|
+
astro.config.vite.plugins.push(createVitePlugin(unpluginFactory)(options));
|
|
10
|
+
} }
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { astro_default as default };
|
package/dist/esbuild.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import 'magic-string';
|
|
5
|
-
import 'typescript';
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import "./types-u_Hr5uXA.js";
|
|
3
|
+
import * as esbuild0 from "esbuild";
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
//#region src/esbuild.d.ts
|
|
6
|
+
declare const _default: (options?: Options | undefined) => esbuild0.Plugin;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { _default as default };
|
package/dist/esbuild.js
ADDED
package/dist/farm.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import "./types-u_Hr5uXA.js";
|
|
3
|
+
import * as _farmfe_core0 from "@farmfe/core";
|
|
4
|
+
|
|
5
|
+
//#region src/farm.d.ts
|
|
6
|
+
declare const _default: (options?: Options | undefined) => _farmfe_core0.JsPlugin;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { _default as default };
|
package/dist/farm.js
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import 'magic-string';
|
|
5
|
-
import 'typescript';
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import * as unplugin0 from "unplugin";
|
|
3
|
+
import { UnpluginFactory } from "unplugin";
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
declare const unpluginFactory: UnpluginFactory<Options | undefined>;
|
|
7
|
+
declare const unplugin: unplugin0.UnpluginInstance<Options | undefined, boolean>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { unplugin as default, unplugin, unpluginFactory };
|
package/dist/index.js
ADDED
package/dist/nuxt.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import 'magic-string';
|
|
5
|
-
import 'typescript';
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import "./types-u_Hr5uXA.js";
|
|
3
|
+
import * as _nuxt_schema0 from "@nuxt/schema";
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
//#region src/nuxt.d.ts
|
|
6
|
+
declare const _default: _nuxt_schema0.NuxtModule<Options, Options, false>;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { _default as default };
|
package/dist/nuxt.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import "./src-Ci8DJn1t.js";
|
|
2
|
+
import { t as vite_default } from "./vite-D-Xfh8NL.js";
|
|
3
|
+
import { t as webpack_default } from "./webpack-By_BEclB.js";
|
|
4
|
+
import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from "@nuxt/kit";
|
|
5
|
+
|
|
6
|
+
//#region src/nuxt.ts
|
|
7
|
+
var nuxt_default = defineNuxtModule({
|
|
8
|
+
meta: {
|
|
9
|
+
name: "@vue.ts/complex-types",
|
|
10
|
+
configKey: "complexTypes"
|
|
11
|
+
},
|
|
12
|
+
defaults: {},
|
|
13
|
+
setup(options, _nuxt) {
|
|
14
|
+
addVitePlugin(() => vite_default(options));
|
|
15
|
+
addWebpackPlugin(() => webpack_default(options));
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { nuxt_default as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import "./types-u_Hr5uXA.js";
|
|
3
|
+
import * as rolldown0 from "rolldown";
|
|
4
|
+
|
|
5
|
+
//#region src/rolldown.d.ts
|
|
6
|
+
declare const _default: (options?: Options | undefined) => rolldown0.Plugin<any> | rolldown0.Plugin<any>[];
|
|
7
|
+
//#endregion
|
|
8
|
+
export { _default as default };
|
package/dist/rolldown.js
ADDED
package/dist/rollup.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import 'magic-string';
|
|
5
|
-
import 'typescript';
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import "./types-u_Hr5uXA.js";
|
|
3
|
+
import * as rollup0 from "rollup";
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
//#region src/rollup.d.ts
|
|
6
|
+
declare const _default: (options?: Options | undefined) => rollup0.Plugin<any> | rollup0.Plugin<any>[];
|
|
7
|
+
//#endregion
|
|
8
|
+
export { _default as default };
|
package/dist/rollup.js
ADDED
package/dist/rspack.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Options } from
|
|
2
|
-
import
|
|
3
|
-
import 'magic-string';
|
|
4
|
-
import 'typescript';
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import "./types-u_Hr5uXA.js";
|
|
5
3
|
|
|
4
|
+
//#region src/rspack.d.ts
|
|
6
5
|
declare const _default: (options?: Options | undefined) => RspackPluginInstance;
|
|
7
|
-
|
|
8
|
-
export { _default as default };
|
|
6
|
+
//#endregion
|
|
7
|
+
export { _default as default };
|
package/dist/rspack.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { createUnplugin } from "unplugin";
|
|
2
|
+
import { ensureLanguage, getLanguage } from "@vue.ts/language";
|
|
3
|
+
import { createFilter } from "unplugin-utils";
|
|
4
|
+
import MagicString from "magic-string";
|
|
5
|
+
import ts from "typescript";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { defu } from "defu";
|
|
8
|
+
import { normalizePath } from "@vue.ts/shared";
|
|
9
|
+
import { isReservedProp } from "@vue/shared";
|
|
10
|
+
|
|
11
|
+
//#region src/core/utils.ts
|
|
12
|
+
const defaultOptions = {
|
|
13
|
+
include: ["**/*.vue"],
|
|
14
|
+
exclude: ["node_modules/**"],
|
|
15
|
+
tsconfigPath: join(process.cwd(), "tsconfig.json"),
|
|
16
|
+
defineEmits: true,
|
|
17
|
+
defineProps: true
|
|
18
|
+
};
|
|
19
|
+
const resolveOptions = (rawOptions) => defu(rawOptions, defaultOptions);
|
|
20
|
+
const quotesReg = /"/g;
|
|
21
|
+
const escapeQuotes = (s) => s.replace(quotesReg, "\\\"");
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/core/printer.ts
|
|
25
|
+
var Printer = class {
|
|
26
|
+
constructor(checker) {
|
|
27
|
+
this.checker = checker;
|
|
28
|
+
}
|
|
29
|
+
typeToString(type) {
|
|
30
|
+
return this.checker.typeToString(type, void 0, ts.TypeFormatFlags.NoTruncation);
|
|
31
|
+
}
|
|
32
|
+
printUnionOrIntersection(type, separator, isPropertyBlacklisted, inner) {
|
|
33
|
+
return [...new Set(type.types.map((t) => this.printType(t, isPropertyBlacklisted, inner)).filter(Boolean))].join(separator);
|
|
34
|
+
}
|
|
35
|
+
isSymbolOptional(symbol) {
|
|
36
|
+
return !!(symbol.flags & ts.SymbolFlags.Optional);
|
|
37
|
+
}
|
|
38
|
+
printConditionType(type, isPropertyBlacklisted, inner) {
|
|
39
|
+
const { trueType, falseType } = type.root.node;
|
|
40
|
+
const trueTypeNode = this.checker.getTypeAtLocation(trueType);
|
|
41
|
+
const falseTypeNode = this.checker.getTypeAtLocation(falseType);
|
|
42
|
+
return `${this.printType(trueTypeNode, isPropertyBlacklisted, inner)} | ${this.printType(falseTypeNode, isPropertyBlacklisted, inner)}`;
|
|
43
|
+
}
|
|
44
|
+
printPrimitiveType(type) {
|
|
45
|
+
if (type.flags & ts.TypeFlags.BooleanLiteral) return "boolean";
|
|
46
|
+
else if (type.flags & ts.TypeFlags.String || type.isStringLiteral()) return "string";
|
|
47
|
+
else if (type.flags & ts.TypeFlags.Number || type.isNumberLiteral()) return "number";
|
|
48
|
+
else if (type.flags & ts.TypeFlags.BigInt) return "bigint";
|
|
49
|
+
else if (type.flags & ts.TypeFlags.Any) return "any";
|
|
50
|
+
else if (type.flags & ts.TypeFlags.Unknown) return "unknown";
|
|
51
|
+
else if (type.flags & ts.TypeFlags.Null) return "null";
|
|
52
|
+
return "";
|
|
53
|
+
}
|
|
54
|
+
printType(type, isPropertyBlacklisted, inner = false) {
|
|
55
|
+
if (type.isUnion()) return this.printUnionOrIntersection(type, " | ", isPropertyBlacklisted, inner);
|
|
56
|
+
else if (type.isIntersection()) return this.printUnionOrIntersection(type, " & ", isPropertyBlacklisted, inner);
|
|
57
|
+
if (this.checker.isArrayType(type)) return "Array";
|
|
58
|
+
else if (type.flags & ts.TypeFlags.Object) {
|
|
59
|
+
const decl = type.getSymbol()?.declarations?.[0];
|
|
60
|
+
if (decl && ts.isFunctionTypeNode(decl)) return "Function";
|
|
61
|
+
if (inner) return "object";
|
|
62
|
+
const properties = type.getProperties();
|
|
63
|
+
const props = {};
|
|
64
|
+
for (const prop of properties) {
|
|
65
|
+
const propType = this.checker.getTypeOfSymbol(prop);
|
|
66
|
+
const name = prop.getName();
|
|
67
|
+
if (isPropertyBlacklisted(name)) continue;
|
|
68
|
+
props[name] = {
|
|
69
|
+
value: this.printType(propType, isPropertyBlacklisted, true),
|
|
70
|
+
isOptional: this.isSymbolOptional(prop)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const parts = [];
|
|
74
|
+
for (const [propName, { isOptional, value }] of Object.entries(props)) {
|
|
75
|
+
const questionMark = isOptional ? "?" : "";
|
|
76
|
+
parts.push(`"${escapeQuotes(propName)}"${questionMark}: ${value},`);
|
|
77
|
+
}
|
|
78
|
+
return Object.keys(props).length > 0 ? `{\n${parts.join("\n")}\n}` : "";
|
|
79
|
+
} else if (type.isLiteral() || type.flags & ts.TypeFlags.BooleanLiteral || type.flags & ts.TypeFlags.String || type.flags & ts.TypeFlags.Number || type.flags & ts.TypeFlags.BigInt || type.flags & ts.TypeFlags.Any || type.flags & ts.TypeFlags.Unknown || type.flags & ts.TypeFlags.Null) return this.printPrimitiveType(type);
|
|
80
|
+
else if (type.flags & ts.TypeFlags.Undefined) return "";
|
|
81
|
+
else if (type.flags & ts.TypeFlags.Conditional) return this.printConditionType(type, isPropertyBlacklisted, inner);
|
|
82
|
+
else if (type.isTypeParameter()) {
|
|
83
|
+
const decl = type.getSymbol()?.declarations?.[0];
|
|
84
|
+
if (!decl || !ts.isTypeParameterDeclaration(decl)) return "";
|
|
85
|
+
const ref = ts.getEffectiveConstraintOfTypeParameter(decl);
|
|
86
|
+
if (!ref) return "";
|
|
87
|
+
const refType = this.checker.getTypeAtLocation(ref);
|
|
88
|
+
return this.printType(refType, isPropertyBlacklisted, inner);
|
|
89
|
+
}
|
|
90
|
+
return this.typeToString(type);
|
|
91
|
+
}
|
|
92
|
+
printPropsTypeArg(node, isPropertyBlacklisted) {
|
|
93
|
+
const type = this.checker.getTypeAtLocation(node);
|
|
94
|
+
return this.printType(type, isPropertyBlacklisted);
|
|
95
|
+
}
|
|
96
|
+
printEventsByCallSignatures(callSignatures) {
|
|
97
|
+
return callSignatures.map((c) => {
|
|
98
|
+
const event = c.getParameters()[0];
|
|
99
|
+
return this.typeToString(this.checker.getTypeOfSymbol(event));
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
printEventsByMembers(members) {
|
|
103
|
+
return members.map((m) => `"${escapeQuotes(m.getName())}"`);
|
|
104
|
+
}
|
|
105
|
+
printEventsRuntimeArg(node) {
|
|
106
|
+
const parts = [];
|
|
107
|
+
const type = this.checker.getTypeAtLocation(node);
|
|
108
|
+
const callSignatures = type.getCallSignatures();
|
|
109
|
+
const members = type.getProperties();
|
|
110
|
+
if (callSignatures.length > 0 && members.length > 0) throw new Error("[@vue.ts/complex-types] You may not use old style `defineEmits` and `defineEmits` shorthand together.");
|
|
111
|
+
if (members.length > 0) parts.push(...this.printEventsByMembers(members));
|
|
112
|
+
else parts.push(...this.printEventsByCallSignatures(callSignatures));
|
|
113
|
+
return `[${parts.join(", ")}]`;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/core/transformers/defineEmits.ts
|
|
119
|
+
const transformDefineEmits = (printer, s, id) => {
|
|
120
|
+
const normalizedFilepath = normalizePath(id);
|
|
121
|
+
const language = getLanguage();
|
|
122
|
+
const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
|
|
123
|
+
const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
|
|
124
|
+
const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
|
|
125
|
+
if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
|
|
126
|
+
const scriptSetupDefineEmitsNode = language.findNodeByRange(scriptSetupAst, (scriptSetupRanges) => scriptSetupRanges.defineEmits?.callExp);
|
|
127
|
+
const virtualFileDefineEmitsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Emit");
|
|
128
|
+
if (!scriptSetupDefineEmitsNode || !virtualFileDefineEmitsTypeNode) return;
|
|
129
|
+
const offset = scriptSetupBlock.startTagEnd;
|
|
130
|
+
if (ts.isCallExpression(scriptSetupDefineEmitsNode) && scriptSetupDefineEmitsNode.arguments[0]) throw new Error("[@vue.ts/complex-types] `defineEmits` cannot accept both runtime argument and type argument.");
|
|
131
|
+
const tokens = scriptSetupDefineEmitsNode.getChildren(scriptSetupAst);
|
|
132
|
+
const lessThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.LessThanToken);
|
|
133
|
+
const greaterThanToken = tokens.find((t) => t.kind === ts.SyntaxKind.GreaterThanToken);
|
|
134
|
+
const openParenToken = tokens.find((t) => t.kind === ts.SyntaxKind.OpenParenToken);
|
|
135
|
+
if (!lessThanToken || !greaterThanToken || !openParenToken) return;
|
|
136
|
+
const defineEmitsTypeArgRange = [offset + lessThanToken.pos, offset + greaterThanToken.end];
|
|
137
|
+
const runtimeArgPos = offset + openParenToken.end;
|
|
138
|
+
const printedRuntimeArg = printer.printEventsRuntimeArg(virtualFileDefineEmitsTypeNode);
|
|
139
|
+
s.remove(...defineEmitsTypeArgRange);
|
|
140
|
+
s.appendRight(runtimeArgPos, printedRuntimeArg);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/core/transformers/defineProps.ts
|
|
145
|
+
const transformDefineProps = (printer, s, id) => {
|
|
146
|
+
const normalizedFilepath = normalizePath(id);
|
|
147
|
+
const language = getLanguage();
|
|
148
|
+
const scriptSetupBlock = language.getScriptSetupBlock(normalizedFilepath);
|
|
149
|
+
const scriptSetupAst = language.getScriptSetupAst(normalizedFilepath);
|
|
150
|
+
const virtualFileAst = language.getVirtualFileOrTsAst(normalizedFilepath);
|
|
151
|
+
if (!scriptSetupBlock || !scriptSetupAst || !virtualFileAst) return;
|
|
152
|
+
const scriptSetupDefinePropsTypeRange = language.parseScriptSetupRanges(scriptSetupAst).defineProps?.typeArg;
|
|
153
|
+
const virtualFileDefinePropsTypeNode = language.findNodeByName(virtualFileAst, "__VLS_Props");
|
|
154
|
+
if (!scriptSetupDefinePropsTypeRange || !virtualFileDefinePropsTypeNode) return;
|
|
155
|
+
const printedType = printer.printPropsTypeArg(virtualFileDefinePropsTypeNode, isReservedProp);
|
|
156
|
+
const offset = scriptSetupBlock.startTagEnd;
|
|
157
|
+
s.overwrite(offset + scriptSetupDefinePropsTypeRange.start, offset + scriptSetupDefinePropsTypeRange.end, printedType);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/core/transformers/index.ts
|
|
162
|
+
const transformers = [["defineEmits", transformDefineEmits], ["defineProps", transformDefineProps]];
|
|
163
|
+
const getTransformers = (options) => transformers.filter(([key]) => !!options[key]);
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/core/transform.ts
|
|
167
|
+
function transform(code, id, options) {
|
|
168
|
+
const s = new MagicString(code);
|
|
169
|
+
const printer = new Printer(getLanguage().tsLs.getProgram().getTypeChecker());
|
|
170
|
+
const transformers$1 = getTransformers(options);
|
|
171
|
+
for (const [, transform$1] of transformers$1) transform$1(printer, s, id);
|
|
172
|
+
return {
|
|
173
|
+
code: s.toString(),
|
|
174
|
+
map: s.generateMap({ hires: true })
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
//#endregion
|
|
179
|
+
//#region src/index.ts
|
|
180
|
+
const languageExtRegexp = /\.((?:c|m)?(?:j|t)sx?|d\.ts|vue)$/i;
|
|
181
|
+
const unpluginFactory = (options = {}) => {
|
|
182
|
+
const resolvedOptions = resolveOptions(options);
|
|
183
|
+
const filter = createFilter(resolvedOptions.include, resolvedOptions.exclude);
|
|
184
|
+
return {
|
|
185
|
+
name: "@vue.ts/complex-types",
|
|
186
|
+
enforce: "pre",
|
|
187
|
+
vite: { async handleHotUpdate(ctx) {
|
|
188
|
+
const { file } = ctx;
|
|
189
|
+
if (!languageExtRegexp.test(file)) return;
|
|
190
|
+
const language = getLanguage();
|
|
191
|
+
const { read } = ctx;
|
|
192
|
+
async function readAndUpdateLanguage() {
|
|
193
|
+
const content = await read();
|
|
194
|
+
language.updateFile(file, content);
|
|
195
|
+
return content;
|
|
196
|
+
}
|
|
197
|
+
if (!filter(file)) {
|
|
198
|
+
await readAndUpdateLanguage();
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
let cached;
|
|
202
|
+
ctx.read = async () => {
|
|
203
|
+
cached ??= (async () => {
|
|
204
|
+
const code = await readAndUpdateLanguage();
|
|
205
|
+
return transform(code, file, resolvedOptions)?.code ?? code;
|
|
206
|
+
})();
|
|
207
|
+
return cached;
|
|
208
|
+
};
|
|
209
|
+
for (const mod of ctx.modules) ctx.server.moduleGraph.invalidateModule(mod);
|
|
210
|
+
return ctx.modules;
|
|
211
|
+
} },
|
|
212
|
+
buildStart() {
|
|
213
|
+
ensureLanguage(resolveOptions(options).tsconfigPath);
|
|
214
|
+
},
|
|
215
|
+
transform: {
|
|
216
|
+
filter: { id: {
|
|
217
|
+
include: resolvedOptions.include,
|
|
218
|
+
exclude: resolvedOptions.exclude
|
|
219
|
+
} },
|
|
220
|
+
handler: (code, id) => transform(code, id, resolvedOptions)
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
const unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
|
|
225
|
+
var src_default = unplugin;
|
|
226
|
+
|
|
227
|
+
//#endregion
|
|
228
|
+
export { unplugin as n, unpluginFactory as r, src_default as t };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { FilterPattern } from "unplugin";
|
|
2
|
+
import MagicString from "magic-string";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
|
|
5
|
+
//#region ../shared/src/types.d.ts
|
|
6
|
+
interface BaseOptions {
|
|
7
|
+
include?: FilterPattern;
|
|
8
|
+
exclude?: FilterPattern;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/core/printer.d.ts
|
|
12
|
+
declare class Printer {
|
|
13
|
+
private checker;
|
|
14
|
+
constructor(checker: ts.TypeChecker);
|
|
15
|
+
private typeToString;
|
|
16
|
+
private printUnionOrIntersection;
|
|
17
|
+
private isSymbolOptional;
|
|
18
|
+
private printConditionType;
|
|
19
|
+
private printPrimitiveType;
|
|
20
|
+
private printType;
|
|
21
|
+
printPropsTypeArg(node: ts.Node, isPropertyBlacklisted: (prop: string) => boolean): string;
|
|
22
|
+
private printEventsByCallSignatures;
|
|
23
|
+
private printEventsByMembers;
|
|
24
|
+
printEventsRuntimeArg(node: ts.Node): string;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/core/types.d.ts
|
|
28
|
+
type ValidTransforms = "defineEmits" | "defineProps";
|
|
29
|
+
type Options = {
|
|
30
|
+
tsconfigPath?: string;
|
|
31
|
+
} & Partial<Record<ValidTransforms, boolean>> & BaseOptions;
|
|
32
|
+
type ResolvedOptions = Required<Options>;
|
|
33
|
+
type TransformOptions = Pick<ResolvedOptions, ValidTransforms>;
|
|
34
|
+
type Transformer = (printer: Printer, s: MagicString, id: string) => void;
|
|
35
|
+
type Transformers = [ValidTransforms, Transformer][];
|
|
36
|
+
//#endregion
|
|
37
|
+
export { Transformers as a, Transformer as i, ResolvedOptions as n, ValidTransforms as o, TransformOptions as r, Options as t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,30 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
declare class Printer {
|
|
6
|
-
private checker;
|
|
7
|
-
constructor(checker: ts.TypeChecker);
|
|
8
|
-
private getBaseType;
|
|
9
|
-
private printIntersectionTypeNode;
|
|
10
|
-
private printUnionTypeNode;
|
|
11
|
-
private printTypeLiteralNode;
|
|
12
|
-
private printByType;
|
|
13
|
-
printTypeArg(node: ts.Node): string;
|
|
14
|
-
private printEventsByCallSignatures;
|
|
15
|
-
private printEventsByMembers;
|
|
16
|
-
printEventsRuntimeArg(node: ts.Node): string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
type ValidTransforms = "defineEmits" | "defineProps";
|
|
20
|
-
type Options = {
|
|
21
|
-
tsconfigPath?: string;
|
|
22
|
-
} & {
|
|
23
|
-
[Transform in ValidTransforms]?: boolean;
|
|
24
|
-
} & BaseOptions;
|
|
25
|
-
type ResolvedOptions = Required<Options>;
|
|
26
|
-
type TransformOptions = Pick<ResolvedOptions, ValidTransforms>;
|
|
27
|
-
type Transformer = (printer: Printer, s: MagicString, id: string) => void;
|
|
28
|
-
type Transformers = [ValidTransforms, Transformer][];
|
|
29
|
-
|
|
30
|
-
export type { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms };
|
|
1
|
+
import { a as Transformers, i as Transformer, n as ResolvedOptions, o as ValidTransforms, r as TransformOptions, t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import "./types-u_Hr5uXA.js";
|
|
3
|
+
export { Options, ResolvedOptions, TransformOptions, Transformer, Transformers, ValidTransforms };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/vite.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import 'magic-string';
|
|
5
|
-
import 'typescript';
|
|
1
|
+
import { t as Options } from "./types-B0cfnWIB.js";
|
|
2
|
+
import "./types-u_Hr5uXA.js";
|
|
3
|
+
import * as vite0 from "vite";
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
//#region src/vite.d.ts
|
|
6
|
+
declare const _default: (options?: Options | undefined) => vite0.Plugin<any> | vite0.Plugin<any>[];
|
|
7
|
+
//#endregion
|
|
8
|
+
export { _default as default };
|
package/dist/vite.js
ADDED