diff options
author | 2022-07-08 17:47:01 -0400 | |
---|---|---|
committer | 2022-07-08 17:47:01 -0400 | |
commit | f9ed77bb0d71d1644d524547a24963210f4ecaff (patch) | |
tree | 5c4c27daf7213d8e483b8a0eaaca547804542ef1 /packages/integrations | |
parent | d2f68345f97eb5b55bc323d633017069398727b2 (diff) | |
download | astro-f9ed77bb0d71d1644d524547a24963210f4ecaff.tar.gz astro-f9ed77bb0d71d1644d524547a24963210f4ecaff.tar.zst astro-f9ed77bb0d71d1644d524547a24963210f4ecaff.zip |
Add editor integrations to language integrations (#3864)
Diffstat (limited to 'packages/integrations')
-rw-r--r-- | packages/integrations/svelte/package.json | 6 | ||||
-rw-r--r-- | packages/integrations/svelte/src/editor.cts | 23 | ||||
-rw-r--r-- | packages/integrations/vue/package.json | 5 | ||||
-rw-r--r-- | packages/integrations/vue/src/editor.cts | 55 |
4 files changed, 85 insertions, 4 deletions
diff --git a/packages/integrations/svelte/package.json b/packages/integrations/svelte/package.json index 755fbc8d3..fe0e6976b 100644 --- a/packages/integrations/svelte/package.json +++ b/packages/integrations/svelte/package.json @@ -21,20 +21,22 @@ "homepage": "https://astro.build", "exports": { ".": "./dist/index.js", + "./editor": "./dist/editor.cjs", "./*": "./*", "./client.js": "./client.js", "./server.js": "./server.js", "./package.json": "./package.json" }, "scripts": { - "build": "astro-scripts build \"src/**/*.ts\" && tsc", - "build:ci": "astro-scripts build \"src/**/*.ts\"", + "build": "astro-scripts build \"src/index.ts\" && astro-scripts build \"src/editor.cts\" --force-cjs --no-clean-dist && tsc", + "build:ci": "astro-scripts build \"src/**/*.ts\" && astro-scripts build \"src/editor.cts\" --force-cjs --no-clean-dist", "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { "@sveltejs/vite-plugin-svelte": "^1.0.0-next.48", "postcss-load-config": "^3.1.4", "svelte-preprocess": "^4.10.7", + "svelte2tsx": "^0.5.11", "vite": "^2.9.10" }, "devDependencies": { diff --git a/packages/integrations/svelte/src/editor.cts b/packages/integrations/svelte/src/editor.cts new file mode 100644 index 000000000..202609d33 --- /dev/null +++ b/packages/integrations/svelte/src/editor.cts @@ -0,0 +1,23 @@ +import { svelte2tsx } from 'svelte2tsx'; + +export function toTSX(code: string, className: string): string { + let result = ` + let ${className}__AstroComponent_: Error + export default ${className}__AstroComponent_ + `; + + try { + let tsx = svelte2tsx(code).code; + tsx = 'let Props = render().props;\n' + tsx; + + // Replace Svelte's class export with a function export + result = tsx.replace( + /^export default[\S\s]*/gm, + `export default function ${className}__AstroComponent_(_props: typeof Props): any {}` + ); + } catch (e: any) { + return result; + } + + return result; +} diff --git a/packages/integrations/vue/package.json b/packages/integrations/vue/package.json index 03d7b7b2f..07ed5caba 100644 --- a/packages/integrations/vue/package.json +++ b/packages/integrations/vue/package.json @@ -21,14 +21,15 @@ "homepage": "https://astro.build", "exports": { ".": "./dist/index.js", + "./editor": "./dist/editor.cjs", "./*": "./*", "./client.js": "./client.js", "./server.js": "./server.js", "./package.json": "./package.json" }, "scripts": { - "build": "astro-scripts build \"src/**/*.ts\" && tsc", - "build:ci": "astro-scripts build \"src/**/*.ts\"", + "build": "astro-scripts build \"src/index.ts\" && astro-scripts build \"src/editor.cts\" --force-cjs --no-clean-dist && tsc", + "build:ci": "astro-scripts build \"src/**/*.ts\" && astro-scripts build \"src/editor.cts\" --force-cjs --no-clean-dist", "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { diff --git a/packages/integrations/vue/src/editor.cts b/packages/integrations/vue/src/editor.cts new file mode 100644 index 000000000..29adaa09c --- /dev/null +++ b/packages/integrations/vue/src/editor.cts @@ -0,0 +1,55 @@ +import { parse } from '@vue/compiler-sfc'; + +export function toTSX(code: string, className: string): string { + let result = `export default function ${className}__AstroComponent_(_props: Record<string, any>): any {}`; + + // NOTE: As you can expect, using regexes for this is not exactly the most reliable way of doing things + // However, I couldn't figure out a way to do it using Vue's compiler, I tried looking at how Volar does it, but I + // didn't really understand everything happening there and it seemed to be pretty Volar-specific. I do believe + // someone more knowledgable on Vue's internals could figure it out, but since this solution is good enough for most + // Vue components (and it's an improvement over, well, nothing), it's alright, I think + try { + const parsedResult = parse(code); + + if (parsedResult.errors.length > 0) { + return ` + let ${className}__AstroComponent_: Error + export default ${className}__AstroComponent_ + `; + } + + if (parsedResult.descriptor.scriptSetup) { + const definePropsType = + parsedResult.descriptor.scriptSetup.content.match(/defineProps<([\s\S]+)>/m); + + if (definePropsType) { + result = ` + ${parsedResult.descriptor.scriptSetup.content} + + export default function ${className}__AstroComponent_(_props: ${definePropsType[1]}): any { + <div></div> + } + `; + } else { + const defineProps = + parsedResult.descriptor.scriptSetup.content.match(/defineProps\([\s\S]+\)/m); + + if (defineProps) { + result = ` + import { defineProps } from '@vue/runtime-core'; + + const Props = ${defineProps[0]} + + export default function ${className}__AstroComponent_(_props: typeof Props): any { + <div></div> + } + `; + } + } + } + } catch (e: any) { + return result; + } + + return result; +} |