Compare commits

...

1 commit

Author SHA1 Message Date
Matthew Phillips
c825d1e374 Saving this experiment 2021-11-16 11:29:00 -05:00
10 changed files with 140 additions and 1 deletions

View file

@ -10,6 +10,7 @@ import configAliasVitePlugin from '../vite-plugin-config-alias/index.js';
import markdownVitePlugin from '../vite-plugin-markdown/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js';
import jsxVitePlugin from '../vite-plugin-jsx/index.js'; import jsxVitePlugin from '../vite-plugin-jsx/index.js';
import fetchVitePlugin from '../vite-plugin-fetch/index.js'; import fetchVitePlugin from '../vite-plugin-fetch/index.js';
import rendererVitePlugin from '../vite-plugin-renderer/index.js';
import { resolveDependency } from './util.js'; import { resolveDependency } from './util.js';
// Some packages are just external, and thats the way it goes. // Some packages are just external, and thats the way it goes.
@ -54,6 +55,7 @@ export async function createVite(inlineConfig: ViteConfigWithSSR, { astroConfig,
jsxVitePlugin({ config: astroConfig, logging }), jsxVitePlugin({ config: astroConfig, logging }),
astroPostprocessVitePlugin({ config: astroConfig, devServer }), astroPostprocessVitePlugin({ config: astroConfig, devServer }),
fetchVitePlugin(), fetchVitePlugin(),
rendererVitePlugin({}),
], ],
publicDir: fileURLToPath(astroConfig.public), publicDir: fileURLToPath(astroConfig.public),
root: fileURLToPath(astroConfig.projectRoot), root: fileURLToPath(astroConfig.projectRoot),

View file

@ -38,7 +38,7 @@ interface ExtractedProps {
componentUrl: string; componentUrl: string;
componentExport: { value: string }; componentExport: { value: string };
} | null; } | null;
props: Record<string | number, any>; props: Record<string | number | symbol, any>;
} }
// Used to extract the directives, aka `client:load` information about a component. // Used to extract the directives, aka `client:load` information about a component.

View file

@ -139,6 +139,7 @@ export async function renderComponent(result: SSRResult, displayName: string, Co
if (typeof Component === 'string') { if (typeof Component === 'string') {
html = await renderAstroComponent(await render`<${Component}${spreadAttributes(props)}>${children}</${Component}>`); html = await renderAstroComponent(await render`<${Component}${spreadAttributes(props)}>${children}</${Component}>`);
} else { } else {
console.log('could not render this');
throw new Error(`Astro is unable to render ${metadata.displayName}!\nIs there a renderer to handle this type of component defined in your Astro config?`); throw new Error(`Astro is unable to render ${metadata.displayName}!\nIs there a renderer to handle this type of component defined in your Astro config?`);
} }
} else { } else {

View file

@ -0,0 +1,76 @@
import type { TransformResult } from '@astrojs/compiler';
import type { SourceMapInput } from 'rollup';
import type vite from '../core/vite';
import { pathToFileURL } from 'url';
import type { AstroConfig } from '../@types/astro-core';
import esbuild from 'esbuild';
import fs from 'fs';
import { fileURLToPath } from 'url';
import os from 'os';
import { transform } from '@astrojs/compiler';
import { decode } from 'sourcemap-codec';
import { AstroDevServer } from '../core/dev/index.js';
function needsAstroLoader(id: string): URL | null {
try {
const url = new URL(`file://${id}`);
if(url.searchParams.has('astro')) {
return url;
}
} catch {}
return null;
}
/** Transform .astro files for Vite */
export default function renderer({ }: any): vite.Plugin {
return {
name: '@astrojs/vite-plugin-renderer',
enforce: 'pre', // run transforms before other plugins can
/*configResolved(resolvedConfig) {
},*/
resolveId(id, importer) {
if(id.startsWith('.')) {
const url = new URL(id, new URL(`file://${importer}`));
if(url.searchParams.has('astro')) {
url.pathname += '.noop';
// Remove file:// because Vite doesn't like it
return url.toString().substr(5);
}
}
return null;
},
async load(id, opts) {
const url = needsAstroLoader(id);
if(url === null) {
return null;
}
const renderer = url.searchParams.get('renderer');
const rendererSpecifier = `@astrojs/renderer-${renderer}`;
console.log(rendererSpecifier)
const source = `
import RealComponent from '/@fs${url.pathname.substr(0, url.pathname.length - 5)}';
import { renderComponent } from 'astro/internal';
import { h } from 'preact';
import d from '@astrojs/renderer-vue/server.js';
export default function FrameworkComponent(props) {
//return h('div', null, 'works');
debugger;
return d.renderToStaticMarkup(RealComponent, props, null);
}
`;
return {
code: source
}
}
};
}

View file

@ -0,0 +1,11 @@
import { h, Fragment } from 'preact';
import VueComponent from './VueComponent.vue?astro&renderer=preact';
export default function() {
return (
<Fragment>
<div>i am preact</div>
<VueComponent />
</Fragment>
);
}

View file

@ -0,0 +1,15 @@
<template>
<span id="vue">i am vue</span>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
setup() {
return {
}
}
})
</script>

View file

@ -0,0 +1,16 @@
---
//import Test from '../components/AstroComponent.astro';
//import JsxComponent from '../components/JsxComponent.jsx';
//import SvelteComponent from '../components/SvelteComponent.svelte';
//import VueComponent from '../components/VueComponent.vue';
import PreactComponent from '../components/PreactComponent.jsx';
---
<html lang="en">
<head>
<title>Framework agnostic component usage</title>
</head>
<body>
<PreactComponent />
</body>
</html>

View file

@ -0,0 +1,17 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Framework Agnostic components', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/framework-agnostic/' });
await fixture.build();
});
it('works', async () => {
const html = await fixture.readFile('/index.html');
console.log(html);
});
});

View file

@ -20,6 +20,7 @@ function check(Component, props, children) {
return !/\<undefined\>/.test(html); return !/\<undefined\>/.test(html);
} catch (err) { } catch (err) {
debugger;
return false; return false;
} }
} }