|
1 | 1 | import fs from 'fs' |
2 | 2 | import globby from 'globby' |
3 | 3 |
|
4 | | -// Append "use client" to client side components |
| 4 | +// Append "use client" to all path chunks that contain "use" hooks |
5 | 5 | // So these packages can be directly used in Next.js directly |
| 6 | +// This allows us to see support file splitting with easy next import |
6 | 7 | ;(async () => { |
7 | | - const clientPaths = await globby([ |
8 | | - 'comments/Disqus.js', |
9 | | - 'comments/Giscus.js', |
10 | | - 'comments/Utterances.js', |
11 | | - 'search/Algolia.js', |
12 | | - 'search/KBar.js', |
13 | | - 'search/KBarModal.js', |
14 | | - 'search/KBarPortal.js', |
15 | | - 'ui/NewsletterForm.js', |
16 | | - 'ui/Pre.js', |
17 | | - ]) |
18 | | - for (const path of clientPaths) { |
19 | | - const data = fs.readFileSync(path) |
20 | | - const fd = fs.openSync(path, 'w+') |
21 | | - const insert = Buffer.from('"use client"\n') |
22 | | - fs.writeSync(fd, insert, 0, insert.length, 0) |
23 | | - fs.writeSync(fd, data, 0, data.length, insert.length) |
24 | | - fs.close(fd, (err) => { |
25 | | - if (err) throw err |
26 | | - }) |
| 8 | + console.log('Added use client directive to the following files:') |
| 9 | + const chunkPaths = await globby('chunk*') |
| 10 | + for (const path of chunkPaths) { |
| 11 | + const data = fs.readFileSync(path, 'utf8') |
| 12 | + if (/useState|useEffect|useRef|useCallback|useMemo|useTheme|useRouter/.test(data)) { |
| 13 | + console.log(path) |
| 14 | + const insert = Buffer.from('"use client"\n') |
| 15 | + fs.writeFileSync(path, insert + data) |
| 16 | + } |
27 | 17 | } |
28 | 18 | })() |
0 commit comments