From 1fe37b11bc334d8a8f852174c48cf79266a50a80 Mon Sep 17 00:00:00 2001 From: Timothy Lin Date: Mon, 17 Jul 2023 19:28:18 +0800 Subject: [PATCH 1/2] add back code splitting and add use client at chunk level --- .changeset/yellow-ligers-sort.md | 5 +++++ packages/pliny/add-use-client.mjs | 32 +++++++++++-------------------- packages/pliny/tsup.config.ts | 2 +- 3 files changed, 17 insertions(+), 22 deletions(-) create mode 100644 .changeset/yellow-ligers-sort.md diff --git a/.changeset/yellow-ligers-sort.md b/.changeset/yellow-ligers-sort.md new file mode 100644 index 0000000..8a39af4 --- /dev/null +++ b/.changeset/yellow-ligers-sort.md @@ -0,0 +1,5 @@ +--- +'pliny': patch +--- + +Add back code splitting and add use client at the chunk level diff --git a/packages/pliny/add-use-client.mjs b/packages/pliny/add-use-client.mjs index 26d62f9..98046a2 100644 --- a/packages/pliny/add-use-client.mjs +++ b/packages/pliny/add-use-client.mjs @@ -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) + } } })() diff --git a/packages/pliny/tsup.config.ts b/packages/pliny/tsup.config.ts index eb56b6e..c9ae36e 100644 --- a/packages/pliny/tsup.config.ts +++ b/packages/pliny/tsup.config.ts @@ -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, From 37ddbcda15f62ebb91ec3da882bf772def74d6ba Mon Sep 17 00:00:00 2001 From: Timothy Lin Date: Mon, 17 Jul 2023 19:28:37 +0800 Subject: [PATCH 2/2] bump version --- .changeset/pre.json | 3 ++- packages/pliny/CHANGELOG.md | 6 ++++++ packages/pliny/package.json | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.changeset/pre.json b/.changeset/pre.json index 3392213..ba91570 100644 --- a/.changeset/pre.json +++ b/.changeset/pre.json @@ -19,6 +19,7 @@ "tall-rocks-hear", "tricky-carpets-fly", "unlucky-keys-join", - "weak-swans-heal" + "weak-swans-heal", + "yellow-ligers-sort" ] } diff --git a/packages/pliny/CHANGELOG.md b/packages/pliny/CHANGELOG.md index cc13055..83b7416 100644 --- a/packages/pliny/CHANGELOG.md +++ b/packages/pliny/CHANGELOG.md @@ -1,5 +1,11 @@ # pliny +## 0.1.0-beta.8 + +### Patch Changes + +- 1fe37b1: Add back code splitting and add use client at the chunk level + ## 0.1.0-beta.7 ### Patch Changes diff --git a/packages/pliny/package.json b/packages/pliny/package.json index 27542dc..72e2bb5 100644 --- a/packages/pliny/package.json +++ b/packages/pliny/package.json @@ -2,7 +2,7 @@ "name": "pliny", "description": "Main entry point for pliny components", "homepage": "https://github.com/timlrx/pliny", - "version": "0.1.0-beta.7", + "version": "0.1.0-beta.8", "type": "module", "exports": { "./*": "./*",