From 05da2201b6a8e0c723fc945c972bd8f0497d460c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B9=A4=E4=BB=99?= Date: Thu, 30 Mar 2023 12:12:04 +0800 Subject: [PATCH] first version --- CHANGELOG.md | 6 +----- README.md | 17 +++++++++++++---- package.json | 3 ++- src/index.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- tsconfig.json | 10 ++-------- 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6f5fa7..1d619c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,4 @@ ## 1.0.0 - 2023-03-30 -- Added something -- Changed something -- Fixed something -- Removed something -- Deprecated something +- First version, support `client` option and `VITE_ADSENSE_CLIENT` env diff --git a/README.md b/README.md index f184790..bafb130 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,17 @@ npm i vite-plugin-adsense ## Usage -```ts -import { hello } from 'vite-plugin-adsense'; - -hello('world'); +```ini +# .env +VITE_ADSENSE_CLIENT=ca-pub-1234567890123456 +``` + +```ts +// vite.config.ts +import { defineConfig } from 'vite'; +import adsense from 'vite-plugin-adsense'; + +export default defineConfig({ + plugins: [adsense()], +}); ``` diff --git a/package.json b/package.json index 62cc7fe..d936436 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "@tsconfig/node16": "^1.0.0", "@types/jest": "^29.0.0", "@types/node": "^16.0.0", - "typescript": "^4.0.0" + "typescript": "^4.0.0", + "vite": "^4.2.1" } } diff --git a/src/index.ts b/src/index.ts index c8f346c..310be64 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,41 @@ -export function hello(who = 'world'): string { - return `Hello, ${who}!`; +import { writeFile } from 'fs/promises'; +import { join } from 'path'; +import { loadEnv, Plugin } from 'vite'; + +export interface AdSenseOptions { + /** Google AdSense publisher ID, e.g. ca-pub-1234567890123456 */ + client?: string; +} + +export default function adsense({ client }: AdSenseOptions = {}): Plugin { + const env = loadEnv(process.env.NODE_ENV || '', process.cwd()); + const actualClient = client || env.VITE_ADSENSE_CLIENT; + let outDir = 'dist'; + return { + name: 'vite:plugin-adsense', + configResolved(resolvedConfig) { + // store the resolved outDir + outDir = resolvedConfig.build.outDir; + }, + transformIndexHtml: (html) => { + if (!actualClient) { + return html; + } + // insert adsense script to the end of index.html + return html.replace( + /<\/body>/, + ` \n` + ); + }, + buildEnd: async () => { + if (!actualClient) { + return; + } + // create ads.txt file + await writeFile( + join(outDir, 'ads.txt'), + `google.com, ${actualClient.substring(3)}, DIRECT, f08c47fec0942fa0\n` + ); + }, + }; } diff --git a/tsconfig.json b/tsconfig.json index afe540c..2e6f260 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,13 +2,7 @@ "extends": "@tsconfig/node16/tsconfig.json", "compilerOptions": { "resolveJsonModule": true, - "types": [ - "node", - "jest", - "@guoyunhe/node-scripts/global" - ] + "types": ["node", "jest", "@guoyunhe/node-scripts/global"] }, - "include": [ - "src" - ] + "include": ["src"] }