Skip to content

Commit

Permalink
add back code splitting and add use client at chunk level
Browse files Browse the repository at this point in the history
  • Loading branch information
timlrx committed Jul 17, 2023
1 parent 70bafdd commit 1fe37b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-ligers-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pliny': patch
---

Add back code splitting and add use client at the chunk level
32 changes: 11 additions & 21 deletions packages/pliny/add-use-client.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import fs from 'fs'
import globby from 'globby'

// Append "use client" to client side components
// Append "use client" to all path chunks that contain "use" hooks
// So these packages can be directly used in Next.js directly
// This allows us to see support file splitting with easy next import
;(async () => {
const clientPaths = await globby([
'comments/Disqus.js',
'comments/Giscus.js',
'comments/Utterances.js',
'search/Algolia.js',
'search/KBar.js',
'search/KBarModal.js',
'search/KBarPortal.js',
'ui/NewsletterForm.js',
'ui/Pre.js',
])
for (const path of clientPaths) {
const data = fs.readFileSync(path)
const fd = fs.openSync(path, 'w+')
const insert = Buffer.from('"use client"\n')
fs.writeSync(fd, insert, 0, insert.length, 0)
fs.writeSync(fd, data, 0, data.length, insert.length)
fs.close(fd, (err) => {
if (err) throw err
})
console.log('Added use client directive to the following files:')
const chunkPaths = await globby('chunk*')
for (const path of chunkPaths) {
const data = fs.readFileSync(path, 'utf8')
if (/useState|useEffect|useRef|useCallback|useMemo|useTheme|useRouter/.test(data)) {
console.log(path)
const insert = Buffer.from('"use client"\n')
fs.writeFileSync(path, insert + data)
}
}
})()
2 changes: 1 addition & 1 deletion packages/pliny/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineConfig } from 'tsup'
export default defineConfig({
entry: ['src/**/*'],
format: 'esm',
splitting: false,
splitting: true,
treeshake: true,
dts: true,
silent: true,
Expand Down

0 comments on commit 1fe37b1

Please sign in to comment.