Skip to content

Commit

Permalink
feat(vite): disable when dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 27, 2024
1 parent 4350aba commit 54e0521
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/vite-plugin-analytics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import type { Plugin } from 'vite';
import type { Plugin, ResolvedConfig } from 'vite';

import { type AnalyticsOptions, generate } from '@unplugin-analytics/core';

import { renderScriptTag } from './render';

export interface Options {
/**
* Whether inject scripts during development
*/
dev?: boolean;

analytics?: AnalyticsOptions;
}

export default function Analytics(options: Options = {}): Plugin {
let config: ResolvedConfig;
const tags = generate(options.analytics ?? []);

return {
name: 'vite-plugin-analytics',
configResolved(resolvedConfig) {
config = resolvedConfig;
},
transformIndexHtml() {
// Skip transform when dev is true and during serve mode
if (options.dev && config.command === 'serve') {
return;
}
return tags.map(renderScriptTag);
}
};
Expand Down

0 comments on commit 54e0521

Please sign in to comment.