Skip to content

Commit

Permalink
feat(unplugin): init vite plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 27, 2024
1 parent 54e0521 commit 77ecf27
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 12 deletions.
12 changes: 5 additions & 7 deletions packages/unplugin-analytics/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { defineBuildConfig } from 'unbuild'
import { defineBuildConfig } from 'unbuild';

export default defineBuildConfig({
entries: [
'src/index',
],
entries: ['src/index', 'src/vite', 'src/nuxt'],
declaration: true,
clean: true,
rollup: {
emitCJS: true,
},
});
emitCJS: true
}
});
11 changes: 11 additions & 0 deletions packages/unplugin-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./vite": {
"types": "./dist/vite.d.ts",
"import": "./dist/vite.mjs",
"require": "./dist/vite.cjs"
},
"./nuxt": {
"types": "./dist/nuxt.d.ts",
"import": "./dist/nuxt.mjs",
"require": "./dist/nuxt.cjs"
}
},
"main": "dist/index.cjs",
Expand All @@ -45,6 +55,7 @@
},
"dependencies": {
"@unplugin-analytics/core": "workspace:*",
"scule": "^1.3.0",
"unplugin": "^1.7.1"
},
"peerDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions packages/unplugin-analytics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export function hello() {
return 'world';
}
export * from './plugin';
47 changes: 47 additions & 0 deletions packages/unplugin-analytics/src/nuxt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { NuxtModule } from '@nuxt/schema';

import { defineNuxtModule } from '@nuxt/kit';

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

const nuxtModule = defineNuxtModule<Options>({
meta: {
// Usually the npm package name of your module
name: 'unplugin-analytics/nuxt',
// The key in `nuxt.config` that holds your module options
configKey: 'analytics',
// Compatibility constraints
compatibility: {
// Semver version of supported nuxt versions
nuxt: '^3.0.0'
}
},
// Default configuration options for your module, can also be a function returning those
defaults: {},
// Shorthand sugar to register Nuxt hooks
hooks: {},
setup(options: Options = {}, nuxt) {
// // install webpack plugin
// nuxt.hook('webpack:config', async (config: any) => {
// config.plugins = config.plugins || [];
// config.plugins.unshift(UnpluginAnalytics.webpack(options));
// });
// // install vite plugin
// nuxt.hook('vite:extendConfig', async (config: any) => {
// config.plugins = config.plugins || [];
// config.plugins.push(UnpluginAnalytics.vite(options));
// });
}
}) as NuxtModule<Options>;

export default nuxtModule;

declare module '@nuxt/schema' {
interface NuxtConfig {
analytics?: Options;
}

interface NuxtOptions {
analytics?: Options;
}
}
30 changes: 30 additions & 0 deletions packages/unplugin-analytics/src/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createUnplugin } from 'unplugin';

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

import { renderScriptTag } from '../../../vite-plugin-analytics/src/render';

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

analytics?: AnalyticsOptions;
}

export const UnpluginAnalytics = createUnplugin<Options | undefined>((options = {}) => {
const tags = generate(options.analytics ?? []);

return {
name: 'unplugin-analytics',
vite: {
transformIndexHtml(_html, ctx) {
if (options.dev && ctx.server) {
return;
}
return tags.map(renderScriptTag).filter(Boolean);
}
}
};
});
3 changes: 3 additions & 0 deletions packages/unplugin-analytics/src/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { UnpluginAnalytics } from './plugin';

export default UnpluginAnalytics.vite;
4 changes: 2 additions & 2 deletions packages/vite-plugin-analytics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type AnalyticsOptions, generate } from '@unplugin-analytics/core';

import { renderScriptTag } from './render';

export interface Options {
interface Options {
/**
* Whether inject scripts during development
*/
Expand All @@ -27,7 +27,7 @@ export default function Analytics(options: Options = {}): Plugin {
if (options.dev && config.command === 'serve') {
return;
}
return tags.map(renderScriptTag);
return tags.map(renderScriptTag).filter(Boolean);
}
};
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77ecf27

Please sign in to comment.