Skip to content

Commit

Permalink
Add editor integrations to language integrations (#3864)
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Jul 8, 2022
1 parent d2f6834 commit f9ed77b
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-students-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/svelte': patch
'@astrojs/vue': patch
---

Add entrypoints for editor support for Vue and Svelte (destined to be used by our language server)
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
},
"packageManager": "[email protected]",
"pnpm": {
"packageExtensions": {
"svelte2tsx": {
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
}
}
},
"peerDependencyRules": {
"ignoreMissing": [
"rollup",
Expand Down
6 changes: 4 additions & 2 deletions packages/integrations/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
23 changes: 23 additions & 0 deletions packages/integrations/svelte/src/editor.cts
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 3 additions & 2 deletions packages/integrations/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
55 changes: 55 additions & 0 deletions packages/integrations/vue/src/editor.cts
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions scripts/cmd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,31 @@ export default async function build(...args) {
))
);

const noClean = args.includes('--no-clean-dist');
const forceCJS = args.includes('--force-cjs');

const {
type = 'module',
version,
dependencies = {},
} = await fs.readFile('./package.json').then((res) => JSON.parse(res.toString()));
// expose PACKAGE_VERSION on process.env for CLI utils
config.define = { 'process.env.PACKAGE_VERSION': JSON.stringify(version) };
const format = type === 'module' ? 'esm' : 'cjs';
const format = type === 'module' && !forceCJS ? 'esm' : 'cjs';

const outdir = 'dist';
await clean(outdir);

if (!noClean) {
await clean(outdir);
}

if (!isDev) {
await esbuild.build({
...config,
bundle: false,
entryPoints,
outdir,
outExtension: forceCJS ? { '.js': '.cjs' } : {},
format,
});
return;
Expand Down

0 comments on commit f9ed77b

Please sign in to comment.