|
1 | | -import { dirname, join } from 'node:path'; |
2 | | -import { fileURLToPath } from 'node:url'; |
| 1 | +import path from 'node:path'; |
| 2 | +import url from 'node:url'; |
3 | 3 | import { ViteEjsPlugin as viteEjsPlugin } from 'vite-plugin-ejs'; |
4 | | -import { getTheme, getTitle } from '../lib/utils.js'; |
| 4 | +import { |
| 5 | + getRevealJsTheme, |
| 6 | + getTheme, |
| 7 | + getTitle, |
| 8 | + readJsonSync, |
| 9 | +} from '../lib/utils.js'; |
5 | 10 |
|
6 | | -const __dirname = fileURLToPath(new URL('.', import.meta.url)); |
| 11 | +const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); |
7 | 12 | const cwd = process.cwd(); |
8 | | -const outDir = join(process.cwd(), 'dist'); |
| 13 | +const outDir = path.join(process.cwd(), 'dist'); |
| 14 | +const pkg = readJsonSync(path.join(cwd, 'package.json')); |
| 15 | +const configFromPackage = pkg['auto-reveal'] || { |
| 16 | + theme: undefined, |
| 17 | + config: {}, |
| 18 | +}; |
| 19 | + |
| 20 | +export default function themeConfigPlugin() { |
| 21 | + const theme = getTheme(); |
| 22 | + const revealJsTheme = getRevealJsTheme(); |
| 23 | + |
| 24 | + return { |
| 25 | + name: 'auto-reveal', |
| 26 | + resolveId(id) { |
| 27 | + if (id === 'virtual:auto-reveal/config') { |
| 28 | + return '\0virtual:auto-reveal/config'; |
| 29 | + } |
| 30 | + }, |
| 31 | + load(id) { |
| 32 | + if (id === '\0virtual:auto-reveal/config') { |
| 33 | + let config = configFromPackage.config; |
| 34 | + |
| 35 | + config = { |
| 36 | + ...config, |
| 37 | + ...(theme?.config ?? {}), |
| 38 | + }; |
| 39 | + |
| 40 | + return `const config = ${JSON.stringify(config)};export default config;`; |
| 41 | + } |
| 42 | + }, |
| 43 | + config() { |
| 44 | + return { |
| 45 | + server: { |
| 46 | + fs: { |
| 47 | + allow: [theme?.dir, revealJsTheme.dir, '.'].filter(Boolean), |
| 48 | + }, |
| 49 | + }, |
| 50 | + resolve: { |
| 51 | + alias: { |
| 52 | + '@theme': theme?.main ? theme.main : revealJsTheme.main, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }; |
| 56 | + }, |
| 57 | + }; |
| 58 | +} |
9 | 59 |
|
10 | | -const themeFolder = dirname(getTheme()); |
11 | 60 | const config = { |
12 | 61 | configFile: false, |
13 | | - root: join(__dirname, '..', 'src'), |
14 | | - publicDir: join(cwd, 'public'), |
| 62 | + root: path.join(__dirname, '..', 'src'), |
| 63 | + publicDir: path.join(cwd, 'public'), |
15 | 64 | base: './', |
16 | 65 | server: { |
17 | 66 | port: 1337, |
18 | | - fs: { |
19 | | - allow: [themeFolder, '.'], |
20 | | - }, |
21 | 67 | }, |
22 | 68 | plugins: [ |
23 | 69 | viteEjsPlugin({ |
24 | 70 | title: getTitle(), |
25 | 71 | }), |
| 72 | + themeConfigPlugin(), |
26 | 73 | ], |
27 | 74 | resolve: { |
28 | 75 | alias: { |
29 | | - slides: join(cwd, 'slides'), |
30 | | - '@theme': themeFolder, |
| 76 | + slides: path.join(cwd, 'slides'), |
31 | 77 | }, |
32 | 78 | }, |
33 | 79 | build: { |
34 | | - outDir: join(cwd, 'dist'), |
| 80 | + outDir: path.join(cwd, 'dist'), |
35 | 81 | }, |
36 | 82 | }; |
37 | 83 |
|
|
0 commit comments