Skip to content

Commit

Permalink
feat(unplugin): support astro
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 27, 2024
1 parent edbf145 commit 5dda26a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
20 changes: 19 additions & 1 deletion examples/astro/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,23 @@ import { defineConfig } from 'astro/config';
import Analytics from '../../packages/unplugin-analytics/src/astro';

export default defineConfig({
integrations: [Analytics()]
integrations: [
Analytics({
analytics: {
umami: {
src: `https://umami.onekuma.cn/script.js`,
id: `a8602a4a-8d41-4df7-9797-5bd074785f2c`
},
plausible: {
domain: `garden.onekuma.cn`
},
cloudflare: {
beacon: `aa68fa3bf166467082bc79ba029b057f`
},
clarity: {
id: `kwj19d7z4j`
}
}
})
]
});
3 changes: 3 additions & 0 deletions examples/astro/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
import Analytics from '~analytics/component.astro'
interface Props {
title: string;
}
Expand All @@ -13,6 +15,7 @@ const { title } = Astro.props;
<meta name="description" content="Astro description" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<Analytics></Analytics>
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
</head>
Expand Down
48 changes: 45 additions & 3 deletions packages/unplugin-analytics/src/astro.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
import { kebabCase } from 'scule';
import { generate } from '@unplugin-analytics/core';

import type { Options } from './plugin';

export default (options: Options = {}) => ({
name: 'unplugin-analytics',
hooks: {
'astro:config:setup': async (astro: any) => {
astro.config.vite.plugins ||= [];
astro.config.vite.plugins.push({});
astro.config.vite.plugins.push(VitePlugin(options));
}
}
});

function VitePlugin() {
function VitePlugin(options: Options) {
const Component = `~analytics/component.astro`;

return {
name: 'unplugin-analytics:astro'
name: 'unplugin-analytics:astro',
resolveId(id: string) {
if (id === Component) return id;
},
async load(id: string) {
if (id === Component) {
const tags = generate(options.analytics ?? {});
const rendered = tags.map((tag) => {
const pieces: string[] = [];
if (tag.defer) {
pieces.push(`defer`);
}
if (tag.async) {
pieces.push(`async`);
}
for (const [_key, _value] of Object.entries(tag.dataset ?? {})) {
const key = kebabCase(`data-${_key}`);
const value = escape(_value);
pieces.push(`${key}="${value}"`);
}

if ('src' in tag) {
return `<script src="${tag.src}" ${pieces.join(' ')}></script>`;
} else {
if (tag.type) {
pieces.unshift(`type="${tag.type}"`);
}
return `<script ${pieces.join(' ')}>${tag.children}</script>`;
}
});

return [...rendered].join('');
}
}
};
}

function escape(text: string) {
return text.replace(/"/g, `&quot;`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineNuxtPlugin({
if ('src' in tag) {
desc.src = tag.src;
} else {
desc.type = tag.type;
desc.children = tag.children;
}

Expand Down

0 comments on commit 5dda26a

Please sign in to comment.