-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathwithTheme.mjs
41 lines (34 loc) · 1.14 KB
/
withTheme.mjs
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
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { camelCase, kebabCase } from 'case-anything'
export function withTheme(nextConfig = {}) {
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const themesDir = path.join(__dirname, 'themes')
const dir = fs.readdirSync(themesDir).filter((f) => {
const fullPath = path.join(themesDir, f)
return fs.statSync(fullPath).isDirectory()
})
const themesText = `export const themes = [${dir.map((t) => '"' + t + '"').join(', ')}]`
const importList = dir
.map((t) => `import * as ${camelCase(t)} from './${t}';`)
.join('\n')
const mapText = `const map: Record<string, any> = {${dir.map((t) => `"${t}": ${camelCase(t)}`).join(', ')}}`
return {
...nextConfig,
webpack(config) {
const themeFilePath = path.join(__dirname, 'themes', 'theme-loader.ts')
const themeContent = `
${importList}
${themesText}
${mapText}
export function loadTheme(name = 'garden'): any {
return map[name] || garden;
}
`
fs.writeFileSync(themeFilePath, themeContent.trim())
return config
},
}
}