Add editor integrations to language integrations (#3864)
This commit is contained in:
parent
d2f68345f9
commit
f9ed77bb0d
8 changed files with 131 additions and 6 deletions
6
.changeset/nasty-students-run.md
Normal file
6
.changeset/nasty-students-run.md
Normal file
|
@ -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)
|
|
@ -47,6 +47,15 @@
|
||||||
},
|
},
|
||||||
"packageManager": "pnpm@7.5.0",
|
"packageManager": "pnpm@7.5.0",
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
|
"packageExtensions": {
|
||||||
|
"svelte2tsx": {
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"typescript": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"peerDependencyRules": {
|
"peerDependencyRules": {
|
||||||
"ignoreMissing": [
|
"ignoreMissing": [
|
||||||
"rollup",
|
"rollup",
|
||||||
|
|
|
@ -21,20 +21,22 @@
|
||||||
"homepage": "https://astro.build",
|
"homepage": "https://astro.build",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./dist/index.js",
|
".": "./dist/index.js",
|
||||||
|
"./editor": "./dist/editor.cjs",
|
||||||
"./*": "./*",
|
"./*": "./*",
|
||||||
"./client.js": "./client.js",
|
"./client.js": "./client.js",
|
||||||
"./server.js": "./server.js",
|
"./server.js": "./server.js",
|
||||||
"./package.json": "./package.json"
|
"./package.json": "./package.json"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
"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\"",
|
"build:ci": "astro-scripts build \"src/**/*.ts\" && astro-scripts build \"src/editor.cts\" --force-cjs --no-clean-dist",
|
||||||
"dev": "astro-scripts dev \"src/**/*.ts\""
|
"dev": "astro-scripts dev \"src/**/*.ts\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.48",
|
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.48",
|
||||||
"postcss-load-config": "^3.1.4",
|
"postcss-load-config": "^3.1.4",
|
||||||
"svelte-preprocess": "^4.10.7",
|
"svelte-preprocess": "^4.10.7",
|
||||||
|
"svelte2tsx": "^0.5.11",
|
||||||
"vite": "^2.9.10"
|
"vite": "^2.9.10"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
23
packages/integrations/svelte/src/editor.cts
Normal file
23
packages/integrations/svelte/src/editor.cts
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -21,14 +21,15 @@
|
||||||
"homepage": "https://astro.build",
|
"homepage": "https://astro.build",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./dist/index.js",
|
".": "./dist/index.js",
|
||||||
|
"./editor": "./dist/editor.cjs",
|
||||||
"./*": "./*",
|
"./*": "./*",
|
||||||
"./client.js": "./client.js",
|
"./client.js": "./client.js",
|
||||||
"./server.js": "./server.js",
|
"./server.js": "./server.js",
|
||||||
"./package.json": "./package.json"
|
"./package.json": "./package.json"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
"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\"",
|
"build:ci": "astro-scripts build \"src/**/*.ts\" && astro-scripts build \"src/editor.cts\" --force-cjs --no-clean-dist",
|
||||||
"dev": "astro-scripts dev \"src/**/*.ts\""
|
"dev": "astro-scripts dev \"src/**/*.ts\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
55
packages/integrations/vue/src/editor.cts
Normal file
55
packages/integrations/vue/src/editor.cts
Normal file
|
@ -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
pnpm-lock.yaml
generated
21
pnpm-lock.yaml
generated
|
@ -1,5 +1,7 @@
|
||||||
lockfileVersion: 5.4
|
lockfileVersion: 5.4
|
||||||
|
|
||||||
|
packageExtensionsChecksum: 01871422d489547c532184effb134b35
|
||||||
|
|
||||||
patchedDependencies:
|
patchedDependencies:
|
||||||
'@changesets/cli@2.23.0':
|
'@changesets/cli@2.23.0':
|
||||||
hash: kcozqtpxuwjzskw6zg5royevn4
|
hash: kcozqtpxuwjzskw6zg5royevn4
|
||||||
|
@ -2184,11 +2186,13 @@ importers:
|
||||||
postcss-load-config: ^3.1.4
|
postcss-load-config: ^3.1.4
|
||||||
svelte: ^3.48.0
|
svelte: ^3.48.0
|
||||||
svelte-preprocess: ^4.10.7
|
svelte-preprocess: ^4.10.7
|
||||||
|
svelte2tsx: ^0.5.11
|
||||||
vite: ^2.9.10
|
vite: ^2.9.10
|
||||||
dependencies:
|
dependencies:
|
||||||
'@sveltejs/vite-plugin-svelte': 1.0.0-next.49_svelte@3.49.0+vite@2.9.14
|
'@sveltejs/vite-plugin-svelte': 1.0.0-next.49_svelte@3.49.0+vite@2.9.14
|
||||||
postcss-load-config: 3.1.4
|
postcss-load-config: 3.1.4
|
||||||
svelte-preprocess: 4.10.7_k3vaqsyrx2lfvza3vdeafxime4
|
svelte-preprocess: 4.10.7_k3vaqsyrx2lfvza3vdeafxime4
|
||||||
|
svelte2tsx: 0.5.11_svelte@3.49.0
|
||||||
vite: 2.9.14
|
vite: 2.9.14
|
||||||
devDependencies:
|
devDependencies:
|
||||||
astro: link:../../astro
|
astro: link:../../astro
|
||||||
|
@ -14277,11 +14281,28 @@ packages:
|
||||||
resolution: {integrity: sha512-+lmjic1pApJWDfPCpUUTc1m8azDqYCG1JN9YEngrx/hUyIcFJo6VZhj0A1Ai0wqoHcEIuQy+e9tk+4uDgdtsFA==}
|
resolution: {integrity: sha512-+lmjic1pApJWDfPCpUUTc1m8azDqYCG1JN9YEngrx/hUyIcFJo6VZhj0A1Ai0wqoHcEIuQy+e9tk+4uDgdtsFA==}
|
||||||
engines: {node: '>= 8'}
|
engines: {node: '>= 8'}
|
||||||
|
|
||||||
|
/svelte2tsx/0.5.11_svelte@3.49.0:
|
||||||
|
resolution: {integrity: sha512-Is95G1wqNvEUJZ9DITRS2zfMwVJRZztMduPs1vJJ0cm65WUg/avBl5vBXjHycQL/qmFpaqa3NG4qWnf7bCHPag==}
|
||||||
|
peerDependencies:
|
||||||
|
svelte: ^3.24
|
||||||
|
typescript: ^4.1.2
|
||||||
|
peerDependenciesMeta:
|
||||||
|
typescript:
|
||||||
|
optional: true
|
||||||
|
dependencies:
|
||||||
|
dedent-js: 1.0.1
|
||||||
|
pascal-case: 3.1.2
|
||||||
|
svelte: 3.49.0
|
||||||
|
dev: false
|
||||||
|
|
||||||
/svelte2tsx/0.5.11_ueozcsexptisi2awlbuwt6eqmq:
|
/svelte2tsx/0.5.11_ueozcsexptisi2awlbuwt6eqmq:
|
||||||
resolution: {integrity: sha512-Is95G1wqNvEUJZ9DITRS2zfMwVJRZztMduPs1vJJ0cm65WUg/avBl5vBXjHycQL/qmFpaqa3NG4qWnf7bCHPag==}
|
resolution: {integrity: sha512-Is95G1wqNvEUJZ9DITRS2zfMwVJRZztMduPs1vJJ0cm65WUg/avBl5vBXjHycQL/qmFpaqa3NG4qWnf7bCHPag==}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
svelte: ^3.24
|
svelte: ^3.24
|
||||||
typescript: ^4.1.2
|
typescript: ^4.1.2
|
||||||
|
peerDependenciesMeta:
|
||||||
|
typescript:
|
||||||
|
optional: true
|
||||||
dependencies:
|
dependencies:
|
||||||
dedent-js: 1.0.1
|
dedent-js: 1.0.1
|
||||||
pascal-case: 3.1.2
|
pascal-case: 3.1.2
|
||||||
|
|
|
@ -47,6 +47,9 @@ export default async function build(...args) {
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const noClean = args.includes('--no-clean-dist');
|
||||||
|
const forceCJS = args.includes('--force-cjs');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
type = 'module',
|
type = 'module',
|
||||||
version,
|
version,
|
||||||
|
@ -54,9 +57,13 @@ export default async function build(...args) {
|
||||||
} = await fs.readFile('./package.json').then((res) => JSON.parse(res.toString()));
|
} = await fs.readFile('./package.json').then((res) => JSON.parse(res.toString()));
|
||||||
// expose PACKAGE_VERSION on process.env for CLI utils
|
// expose PACKAGE_VERSION on process.env for CLI utils
|
||||||
config.define = { 'process.env.PACKAGE_VERSION': JSON.stringify(version) };
|
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';
|
const outdir = 'dist';
|
||||||
await clean(outdir);
|
|
||||||
|
if (!noClean) {
|
||||||
|
await clean(outdir);
|
||||||
|
}
|
||||||
|
|
||||||
if (!isDev) {
|
if (!isDev) {
|
||||||
await esbuild.build({
|
await esbuild.build({
|
||||||
|
@ -64,6 +71,7 @@ export default async function build(...args) {
|
||||||
bundle: false,
|
bundle: false,
|
||||||
entryPoints,
|
entryPoints,
|
||||||
outdir,
|
outdir,
|
||||||
|
outExtension: forceCJS ? { '.js': '.cjs' } : {},
|
||||||
format,
|
format,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Add table
Reference in a new issue