first version
This commit is contained in:
+1
-5
@@ -2,8 +2,4 @@
|
|||||||
|
|
||||||
## 1.0.0 - 2023-03-30
|
## 1.0.0 - 2023-03-30
|
||||||
|
|
||||||
- Added something
|
- First version, support `client` option and `VITE_ADSENSE_CLIENT` env
|
||||||
- Changed something
|
|
||||||
- Fixed something
|
|
||||||
- Removed something
|
|
||||||
- Deprecated something
|
|
||||||
|
|||||||
@@ -8,8 +8,17 @@ npm i vite-plugin-adsense
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```ts
|
```ini
|
||||||
import { hello } from 'vite-plugin-adsense';
|
# .env
|
||||||
|
VITE_ADSENSE_CLIENT=ca-pub-1234567890123456
|
||||||
hello('world');
|
```
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// vite.config.ts
|
||||||
|
import { defineConfig } from 'vite';
|
||||||
|
import adsense from 'vite-plugin-adsense';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [adsense()],
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|||||||
+2
-1
@@ -55,6 +55,7 @@
|
|||||||
"@tsconfig/node16": "^1.0.0",
|
"@tsconfig/node16": "^1.0.0",
|
||||||
"@types/jest": "^29.0.0",
|
"@types/jest": "^29.0.0",
|
||||||
"@types/node": "^16.0.0",
|
"@types/node": "^16.0.0",
|
||||||
"typescript": "^4.0.0"
|
"typescript": "^4.0.0",
|
||||||
|
"vite": "^4.2.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-2
@@ -1,3 +1,41 @@
|
|||||||
export function hello(who = 'world'): string {
|
import { writeFile } from 'fs/promises';
|
||||||
return `Hello, ${who}!`;
|
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
@@ -2,13 +2,7 @@
|
|||||||
"extends": "@tsconfig/node16/tsconfig.json",
|
"extends": "@tsconfig/node16/tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"types": [
|
"types": ["node", "jest", "@guoyunhe/node-scripts/global"]
|
||||||
"node",
|
|
||||||
"jest",
|
|
||||||
"@guoyunhe/node-scripts/global"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"include": [
|
"include": ["src"]
|
||||||
"src"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user