first version

This commit is contained in:
鹤仙
2023-03-30 12:12:04 +08:00
parent bce20c7247
commit 05da2201b6
5 changed files with 58 additions and 20 deletions
+1 -5
View File
@@ -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
+13 -4
View File
@@ -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()],
});
```
+2 -1
View File
@@ -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"
}
}
+40 -2
View File
@@ -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>/,
` <script src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=${actualClient}" crossorigin="anonymous" defer></script>\n</body>`
);
},
buildEnd: async () => {
if (!actualClient) {
return;
}
// create ads.txt file
await writeFile(
join(outDir, 'ads.txt'),
`google.com, ${actualClient.substring(3)}, DIRECT, f08c47fec0942fa0\n`
);
},
};
}
+2 -8
View File
@@ -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"]
}