Skip to content

Commit 55faed6

Browse files
committed
feat: support transform script
1 parent cf3dc48 commit 55faed6

19 files changed

+619
-290
lines changed

example/src/App.vue

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
<script lang="ts">
22
import { defineComponent } from 'vue'
3+
import Home from "./views/Home.vue"
4+
import NotFound from "./views/404.vue"
5+
import NoPermission from "./views/401.vue"
36
export default defineComponent({
4-
directives: {
5-
7+
name: 'App',
8+
components: {
9+
Home,
10+
NotFound,
11+
NoPermission
612
},
7-
setup(props, item) {
8-
13+
setup() {
14+
return {}
915
}
1016
})
1117
</script>
1218

1319
<template>
14-
<div>Home</div>
15-
</template>
20+
<div>App</div>
21+
</template>

example/src/views/401.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<script lang="ts">
2+
import { defineComponent, PropType, ref } from "vue"
3+
import Header from "../components/Header.vue"
4+
import Tab from "../components/Tab.vue"
5+
import touchdir from "vtouchdir"
6+
export default defineComponent({
7+
name: 'App',
8+
components: {
9+
Header,
10+
Tab,
11+
},
12+
directives: {
13+
force: {},
14+
touchdir,
15+
},
16+
props: {
17+
items: Array as PropType<number[]>
18+
},
19+
emit: ["click"],
20+
setup(props, { emit, attrs, slots: mySlots, expose }) {
21+
const bar = ref(0)
22+
expose({ bar })
23+
emit("change");
24+
return {
25+
bar
26+
}
27+
}
28+
})
29+
</script>
30+
31+
<template>
32+
<div>App</div>
33+
</template>

example/src/views/Home.vue

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<script lang="ts">
2+
import { PropType, ref, watch } from 'vue'
3+
import Header from "../components/Header.vue"
4+
import Tab from "../components/Tab.vue"
5+
export default ({
6+
name: 'Home',
7+
components: {
8+
Header,
9+
Tab,
10+
},
11+
props: {
12+
source: {
13+
type: String as PropType<"Main" | "visitor">
14+
},
15+
showTable: Boolean,
16+
maxLimit: {
17+
type: Number,
18+
default: 0
19+
},
20+
time: {
21+
type: Date,
22+
required: true
23+
}
24+
},
25+
setup(theProps) {
26+
const foo = ref(0)
27+
28+
watch(() => theProps.maxLimit, (value) => {
29+
foo.value = value
30+
})
31+
return { foo }
32+
}
33+
})
34+
</script>
35+
36+
<template>
37+
<div>App</div>
38+
</template>

src/constants.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@ export const enum FileType {
99
ts,
1010
}
1111

12+
export interface DefaultOption {
13+
propsNotOnlyTs?: boolean;
14+
notUseNewFile?: boolean;
15+
path: {
16+
[key: string]:
17+
| string
18+
| string[]
19+
| {
20+
mode: "*" | "**";
21+
excludes: string | string[];
22+
};
23+
};
24+
}
25+
26+
export type CommandsOption = Omit<DefaultOption, "path">;
1227
export interface Config {
1328
fileType: FileType;
1429
script: string;
1530
offset: number;
16-
setupScript?: string;
1731
fileAbsolutePath: string;
32+
propsNotOnlyTs?: boolean;
33+
setupScript?: string;
1834
}
1935

2036
export const parseOption = {
@@ -24,3 +40,6 @@ export const parseOption = {
2440
} as ParseOptions;
2541

2642
export type SetupAst = ArrowFunctionExpression | MethodProperty;
43+
44+
export const USE_ATTRS = "useAttrs" as const;
45+
export const USE_SLOTS = "useSlots" as const;

src/index.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { findUpSync } from "find-up";
2-
import { getTheFileAbsolutePath, useConfigPath } from "./utils";
3-
import { parseSfc } from "./parse/parser";
2+
import { getTheFileAbsolutePath, output, useConfigPath } from "./utils";
3+
import { parseSfc } from "./transform";
4+
import { CommandsOption } from "./constants";
45

56
const CONFIG_FILE_NAME = "tosetup.config" as const;
67

@@ -9,11 +10,21 @@ async function setup() {
910

1011
let { pathNames, commands } = argv.reduce<{
1112
pathNames: string[];
12-
commands: string[];
13+
commands: CommandsOption;
1314
}>(
1415
(p, c) => {
1516
if (c.startsWith("--")) {
16-
p.commands.push(c);
17+
switch (c.split("--")[1] as keyof typeof p.commands) {
18+
case "propsNotOnlyTs":
19+
p.commands.propsNotOnlyTs = true;
20+
break;
21+
case "notUseNewFile":
22+
p.commands.notUseNewFile = true;
23+
break;
24+
25+
default:
26+
break;
27+
}
1728
return p;
1829
}
1930

@@ -24,22 +35,26 @@ async function setup() {
2435

2536
return p;
2637
},
27-
{ pathNames: [], commands: [] },
38+
{ pathNames: [], commands: {} },
2839
);
2940

3041
if (!pathNames.length) {
3142
const configPath = findUpSync(CONFIG_FILE_NAME);
3243
if (!configPath) {
33-
console.error(
44+
output.error(
3445
`Please enter a file path or use a ${CONFIG_FILE_NAME} file.`,
3546
);
3647
process.exit(1);
3748
}
3849

39-
pathNames = await useConfigPath(CONFIG_FILE_NAME);
50+
const config = await useConfigPath(CONFIG_FILE_NAME);
51+
52+
pathNames = config.pathNames;
53+
54+
commands = { ...commands, ...config.option };
4055
}
4156

42-
parseSfc(pathNames);
57+
parseSfc(pathNames, commands);
4358
}
4459

4560
setup();

src/parse/filter-options.ts

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/parse/parser.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)