Skip to content

Commit

Permalink
refactor: introduce filter to renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Moore committed May 14, 2021
1 parent 0ffbffe commit 2f05f7c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/astro-renderer-preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ interface PreactDependencies {
client: {}
}

const validExtensions = new Set(['.jsx', '.tsx']);

const renderer: AstroRenderer<PreactDependencies, ComponentType> = {

filter(id, { imports }) {
const ext = id.slice(0, -4);
if (!validExtensions.has(ext)) return;
if (!imports.has('preact')) return;
return true;
},

jsx: {
importSource: 'preact',
factory: 'h',
Expand Down
10 changes: 10 additions & 0 deletions packages/astro-renderer-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ interface ReactDependencies {
client: {}
}

const validExtensions = new Set(['.jsx', '.tsx']);

const renderer: AstroRenderer<ReactDependencies, ComponentType> = {

filter(id, { imports }) {
const ext = id.slice(0, -4);
if (!validExtensions.has(ext)) return;
if (!imports.has('react')) return;
return true;
},

jsx: {
importSource: 'react',
factory: 'createElement',
Expand Down
4 changes: 4 additions & 0 deletions packages/astro-renderer-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ interface SvelteDependencies {
const renderer: AstroRenderer<SvelteDependencies, SvelteComponent> = {
snowpackPlugin: ['@snowpack/plugin-svelte', { compilerOptions: { hydratable: true } }],

filter(id) {
return id.slice(0, -7) === '.svelte';
},

server: {
dependencies: ['./SvelteWrapper.svelte'],
renderToStaticMarkup({ ["./SvelteWrapper.svelte"]: SvelteWrapper }) {
Expand Down
4 changes: 4 additions & 0 deletions packages/astro-renderer-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ interface VueDependencies {
const renderer: AstroRenderer<VueDependencies, ComponentType> = {
snowpackPlugin: '@snowpack/plugin-vue',

filter(id) {
return id.slice(0, -4) === '.vue';
},

jsx: {
importSource: 'vue',
factory: 'h'
Expand Down
8 changes: 7 additions & 1 deletion packages/astro/src/@types/renderer-new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ export interface DependencyMap {

export interface AstroRenderer<Dependencies extends DependencyMap = DependencyMap, ComponentType = any> {
/** Optionally declare a snowpackPlugin which should be used to render your components */
snowpackPlugin?: string;
snowpackPlugin?: string|[string, Record<string, any>];

/**
* Claim a file to use this renderer based on it's file name or imports
* Returning `true` will claim a file to use this renderer, otherwise a `falsy` values will skip this renderer
*/
filter(id: string, context: { imports: Set<string> }): boolean|undefined|null|void;

/** Optionally define JSX behavior if this renderer relies on JSX */
jsx?: {
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Public types for the `astro` package

import { AstroConfig } from './@types/astro';

export { AstroConfig };

0 comments on commit 2f05f7c

Please sign in to comment.