-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathastro.config.mjs
More file actions
61 lines (59 loc) · 1.84 KB
/
astro.config.mjs
File metadata and controls
61 lines (59 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import vercel from '@astrojs/vercel'
import { defineConfig } from 'astro/config'
import { transform } from 'esbuild'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const readEmbedFile = async () => await fs.readFile(path.join(__dirname, 'src/scripts/embed.ts'), 'utf-8')
// https://astro.build/config
export default defineConfig({
site: 'https://caniuse.pengzhanbo.cn',
devToolbar: {
enabled: false,
},
output: 'server',
adapter: vercel(),
integrations: [
{
name: 'ciu-embed-scripts',
hooks: {
'astro:server:setup': ({ server }) => {
server.middlewares.use(async (req, res, next) => {
if (req.url === '/embed.js') {
const source = await readEmbedFile()
res.setHeader('Content-Type', 'text/javascript;charset=utf-8')
const transformed = await transform(source, { loader: 'ts' })
res.end(transformed.code)
}
else {
next()
}
})
},
'astro:build:setup': ({ vite }) => {
vite.plugins ??= []
vite.plugins.push({
name: 'ciu-home-script-bundle',
async buildEnd() {
const source = await readEmbedFile()
const transformed = await transform(source, {
loader: 'ts',
format: 'esm',
target: 'ES2020',
platform: 'browser',
minify: true,
})
this.emitFile({
fileName: 'embed.js',
type: 'asset',
source: transformed.code,
})
console.log('Embed script bundle generated')
},
})
},
},
},
],
})