create-react-docs-ui 0.6.3 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,75 @@
1
1
  // @ts-nocheck
2
2
  import path from "path"
3
+ import { spawn } from "child_process"
3
4
  import react from "@vitejs/plugin-react"
4
5
  import { defineConfig } from "vite"
5
6
  import { mdxComponentsPlugin } from "./vite-plugin-mdx-components"
6
7
 
8
+ function searchIndexPlugin() {
9
+ return {
10
+ name: 'search-index-plugin',
11
+ configureServer(server) {
12
+ const publicDir = path.resolve(__dirname, 'public')
13
+ const docsDir = path.resolve(publicDir, 'docs')
14
+
15
+ server.watcher.add(docsDir)
16
+
17
+ let generating = false
18
+ let pending = false
19
+
20
+ const generateIndex = () => {
21
+ if (generating) {
22
+ pending = true
23
+ return
24
+ }
25
+ generating = true
26
+
27
+ const child = spawn('node', ['scripts/generate-search-index.js'], {
28
+ cwd: path.resolve(__dirname),
29
+ stdio: 'inherit'
30
+ })
31
+
32
+ child.on('close', () => {
33
+ generating = false
34
+ if (pending) {
35
+ pending = false
36
+ generateIndex()
37
+ }
38
+ })
39
+ }
40
+
41
+ generateIndex()
42
+
43
+ const isDocFile = (file) => {
44
+ return file.includes(path.sep + 'docs' + path.sep) &&
45
+ (file.endsWith('.md') || file.endsWith('.mdx'))
46
+ }
47
+
48
+ const debounce = (fn, delay) => {
49
+ let timer = null
50
+ return (...args) => {
51
+ if (timer) clearTimeout(timer)
52
+ timer = setTimeout(() => fn(...args), delay)
53
+ }
54
+ }
55
+
56
+ const debouncedGenerate = debounce(generateIndex, 500)
57
+
58
+ server.watcher.on('change', (file) => {
59
+ if (isDocFile(file)) debouncedGenerate()
60
+ })
61
+
62
+ server.watcher.on('add', (file) => {
63
+ if (isDocFile(file)) debouncedGenerate()
64
+ })
65
+
66
+ server.watcher.on('unlink', (file) => {
67
+ if (isDocFile(file)) debouncedGenerate()
68
+ })
69
+ }
70
+ }
71
+ }
72
+
7
73
  function publicHmrPlugin() {
8
74
  return {
9
75
  name: 'public-hmr',
@@ -43,20 +109,23 @@ export default defineConfig({
43
109
  componentsPath: './src/components',
44
110
  outputPath: './src/generated/mdx-components.ts'
45
111
  }),
112
+ searchIndexPlugin(),
46
113
  publicHmrPlugin()
47
114
  ],
48
115
  resolve: {
49
116
  alias: {
50
117
  "@": "src",
51
- buffer: "buffer/",
118
+ buffer: "buffer",
52
119
  },
53
120
  },
121
+ define: {
122
+ global: 'globalThis',
123
+ },
54
124
  server: {
55
125
  host: "0.0.0.0",
56
126
  port: 5173,
57
127
  },
58
128
  build: {
59
- // 提高 chunk 大小警告的阈值到 2000 kB
60
129
  chunkSizeWarningLimit: 2000
61
130
  }
62
131
  })