Skip to content

Commit e07a615

Browse files
committed
Now grew into an Astro intergration!
1 parent 99f7b20 commit e07a615

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

.changeset/blue-carrots-grin.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"astro-vtbot": minor
3+
---
4+
5+
Enables installation as an Astro integration with `npx astro add astro-vtbot`. This automatically adds the `<Linter>` component to all your `<ViewTransition>` pages!
6+

integration/env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference types="astro/client" />
2+
/// <reference path="../.astro/types.d.ts" />

integration/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import type { AstroConfig, AstroIntegration } from "astro";
3+
import vitePluginVtbotExtend from "./vite-plugin-extend";
4+
5+
type VtBotOptions = {
6+
autoLint: boolean;
7+
};
8+
9+
export default function createIntegration(options?: VtBotOptions): AstroIntegration {
10+
let config: AstroConfig;
11+
return {
12+
name: 'astro-vtbot',
13+
hooks: {
14+
'astro:config:setup': ({ updateConfig }) => {
15+
(options?.autoLint ?? true) && updateConfig({
16+
vite: {
17+
plugins: [vitePluginVtbotExtend()],
18+
},
19+
});
20+
},
21+
22+
'astro:config:done': async ({ config: cfg }) => {
23+
config = cfg;
24+
},
25+
},
26+
};
27+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import VTPL from './vtpl.astro';
2+
export * from 'astro:transitions';
3+
export { VTPL as ViewTransitions };

integration/vite-plugin-extend.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { parse } from "acorn";
2+
import { walk, type Node } from 'estree-walker';
3+
import type { Plugin } from "vite";
4+
5+
export default function vitePluginVtbotExtend(): Plugin {
6+
return {
7+
name: "vtbot:linter",
8+
enforce: "pre",
9+
transform(code: string, id: string) {
10+
if (!import.meta.env.DEV || id.endsWith("vtpl.astro") || !id.endsWith(".astro")) return;
11+
12+
const match = code.match(/from\s*['"]astro:transitions["']/ms);
13+
if (match) {
14+
console.log('id :>> ', id);
15+
const ast = parse(code, {
16+
ecmaVersion: 'latest',
17+
sourceType: 'module',
18+
}) as Node;
19+
20+
walk(ast, {
21+
enter(node: any) {
22+
if (node.type === 'ImportDeclaration' && node.source.value === 'astro:transitions') {
23+
code = code.substring(0, node.source.start) + '"astro-vtbot/vtext"' + code.substring(node.source.end);
24+
}
25+
}
26+
});
27+
};
28+
return code;
29+
}
30+
};
31+
};
32+

integration/vtpl.astro

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
import { ViewTransitions } from 'astro:transitions';
3+
import Linter from '../components/Linter.astro';
4+
---
5+
6+
<ViewTransitions />
7+
<Linter />

0 commit comments

Comments
 (0)