forked from anycable/anycable-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
105 lines (96 loc) · 3.02 KB
/
vite.config.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// vite.config.(js|ts)
import { defineConfig } from "vite"
import pugPlugin from "vite-plugin-pug"
import handlebars from 'vite-plugin-handlebars'
import { default as MarkdownIt } from 'markdown-it'
import { default as MarkdownItClassy } from 'markdown-it-classy'
import { default as MarkdownItHighlightJS } from 'markdown-it-highlightjs'
import { default as Handlebars } from 'handlebars'
import "process"
import { resolve } from "path"
import { readFileSync } from "fs"
import { default as fg } from "fast-glob"
const config = {
sponsorUrl: "https://github.com/sponsors/anycable",
formURL: "https://form.typeform.com/to/wAHm0sRP",
proFormUrl: "https://form.typeform.com/to/BwBcZmdQ",
tracking: process.env.NODE_ENV == 'production'
}
const root = resolve(__dirname, 'src')
const pages = fg.sync(resolve(root, '**/*/index.html')).map(path => {
let url = path.slice(root.length).replace(/\/index\.html$/, '')
return url
}).sort((a, b) => a < b ? 1 : -1) // Sort pages to match by the longest url prefix in the dev middleware
const pugOptions = {basedir: "src", pretty: true, localImports: true}
const markdownIt = new MarkdownIt({html: true})
markdownIt.use(MarkdownItClassy)
markdownIt.use(MarkdownItHighlightJS)
const handlebarsHelpers = {
'inline': (path) => {
return new Handlebars.SafeString(readFileSync(resolve(root, path)))
},
'md': (path, options) => {
if (path.startsWith('.')) {
path = resolve(root, options.data.root.__file__.replace(/[^\/]+\.html$/, path))
} else {
path = resolve(root, path)
}
return new Handlebars.SafeString(markdownIt.render(readFileSync(path).toString()))
},
'coalesce': (...vars) => {
return new Handlebars.SafeString(vars.find(v => v))
},
'youtube': (videoId, linkType) => {
switch(linkType) {
case 'video': {
return new Handlebars.SafeString(`https://youtube.com/watch?v=${videoId}`)
}
case 'thumbnail': {
return new Handlebars.SafeString(`https://i.ytimg.com/vi/${videoId}/maxresdefault.jpg`)
}
}
},
'currentYear': () => {
return (new Date()).getFullYear()
}
}
export default defineConfig({
appType: 'mpa',
root: root,
plugins: [
pugPlugin(pugOptions, config),
handlebars({
partialDirectory: resolve(root, 'partials'),
context: (path) => {
return { ...config, __file__: path.replace(/^\//, '') }
},
helpers: handlebarsHelpers
}),
{
name: 'rewrite-middleware',
configureServer(serve) {
serve.middlewares.use((req, res, next) => {
for(let page of pages) {
if (req.url.startsWith(page)) {
req.url = `${page}/index.html`;
break
}
}
next()
})
}
}
],
build: {
outDir: resolve(__dirname, 'dist'),
rollupOptions: {
input: {
...pages.reduce((acc, url) => {
acc[url.replace("/", "")] = resolve(root, url, "index.html")
return acc
}, {}),
main: resolve(root, 'index.html'),
},
},
},
})