-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: convert to defineAsyncComponent (#56)
- Loading branch information
Showing
11 changed files
with
249 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/vue-script-setup-converter/src/lib/converter/componentsConverter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { expect, test } from "vitest"; | ||
import type { CallExpression } from "ts-morph"; | ||
import { ScriptTarget, SyntaxKind, Project } from "ts-morph"; | ||
import { parse } from "@vue/compiler-sfc"; | ||
import { getNodeByKind } from "../helpers/node"; | ||
import { convertComponents } from "./componentsConverter"; | ||
|
||
const parseScript = (input: string, lang: "js" | "ts" = "js") => { | ||
const { | ||
descriptor: { script }, | ||
} = parse(input); | ||
|
||
const project = new Project({ | ||
tsConfigFilePath: "tsconfig.json", | ||
compilerOptions: { | ||
target: ScriptTarget.Latest, | ||
}, | ||
}); | ||
|
||
const sourceFile = project.createSourceFile("s.tsx", script?.content ?? ""); | ||
|
||
const callexpression = getNodeByKind(sourceFile, SyntaxKind.CallExpression); | ||
|
||
const components = convertComponents(callexpression as CallExpression); | ||
|
||
return components; | ||
}; | ||
|
||
test("should be converted to defineAsyncComponent", () => { | ||
const source = `<script> | ||
import { defineComponent } from 'vue'; | ||
import HelloWorld from './HelloWorld.vue'; | ||
export default defineComponent({ | ||
components: { | ||
HelloWorld, | ||
MyComp: () => import('./MyComp.vue'), | ||
Foo: () => import('./Foo.vue'), | ||
} | ||
}) | ||
`; | ||
const output = parseScript(source); | ||
|
||
expect(output).toMatchInlineSnapshot( | ||
` | ||
"const MyComp = defineAsyncComponent(() => import('./MyComp.vue')) | ||
const Foo = defineAsyncComponent(() => import('./Foo.vue'))" | ||
` | ||
); | ||
}); | ||
|
||
test("should be output as is", () => { | ||
const source = `<script> | ||
import { defineComponent, defineAsyncComponent } from 'vue'; | ||
import HelloWorld from './HelloWorld.vue'; | ||
export default defineComponent({ | ||
components: { | ||
HelloWorld, | ||
MyComp: defineAsyncComponent(() => import('./MyComp.vue')), | ||
Foo: defineAsyncComponent(() => import('./Foo.vue')), | ||
} | ||
}) | ||
`; | ||
const output = parseScript(source); | ||
|
||
expect(output).toMatchInlineSnapshot(` | ||
"const MyComp = defineAsyncComponent(() => import('./MyComp.vue')) | ||
const Foo = defineAsyncComponent(() => import('./Foo.vue'))" | ||
`); | ||
}); |
48 changes: 48 additions & 0 deletions
48
packages/vue-script-setup-converter/src/lib/converter/componentsConverter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { CallExpression, PropertyAssignment } from "ts-morph"; | ||
import { Node, SyntaxKind } from "ts-morph"; | ||
import { getOptionsNode } from "../helpers/node"; | ||
import { filterDynamicImport } from "../helpers/module"; | ||
|
||
export const convertComponents = (node: CallExpression) => { | ||
const componentsNode = getOptionsNode(node, "components"); | ||
|
||
if (!componentsNode) { | ||
return ""; | ||
} | ||
|
||
return convertToDefineAsyncComponent(componentsNode); | ||
}; | ||
|
||
const convertToDefineAsyncComponent = (node: PropertyAssignment) => { | ||
const child = node.getInitializer(); | ||
|
||
if (!child) { | ||
throw new Error("components is empty."); | ||
} | ||
|
||
if (!Node.isObjectLiteralExpression(child)) return ""; | ||
|
||
const properties = child.getProperties(); | ||
|
||
const filterdProperties = properties.filter(filterDynamicImport); | ||
|
||
if (filterdProperties.length === 0) return ""; | ||
|
||
const value = filterdProperties | ||
.map((x) => { | ||
const propertyName = x.getName(); | ||
const initializer = x.getInitializer(); | ||
|
||
if (!initializer) return ""; | ||
|
||
if (initializer.getText().includes("defineAsyncComponent")) { | ||
return `const ${propertyName} = ${initializer.getText()}`; | ||
} | ||
|
||
const arrowFunc = x.getFirstChildByKind(SyntaxKind.ArrowFunction); | ||
return `const ${propertyName} = defineAsyncComponent(${arrowFunc!.getText()})`; | ||
}) | ||
.join("\n"); | ||
|
||
return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.