Skip to content

Instantly share code, notes, and snippets.

View huozhi's full-sized avatar
🎈

Jiachi Liu huozhi

🎈
View GitHub Profile
@huozhi
huozhi / page.tsx
Created August 21, 2025 09:21
use-swr re-use item cache from list example
'use client'
import React from 'react'
import useSWR, { mutate } from 'swr'
interface Book {
id: string
title: string
author: string
publishedYear: number
@huozhi
huozhi / rspack.config.js
Created July 22, 2025 13:30
output esm modules with rspack
{
mode: dev ? 'development' : 'production',
output: {
path: path.join(__dirname, 'dist/compiled/next-devtools'),
filename: `index.js`,
iife: false,
chunkFormat: 'module',
chunkLoading: 'import',
library: {
type: 'module',
@huozhi
huozhi / rolldown-config-mapping.ts
Last active May 26, 2025 07:44
map rollup config to rolldown config
// rolldown version: 1.0.0-beta.9
//
// Error output example:
// ⨯ Error: Failed validate input options.
// - For the "input.external". Invalid type: Expected string but received Function.
// at validateOption (.../node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/src-DUvB0HDu.cjs:2116:28)
// at rolldown (.../node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/src-DUvB0HDu.cjs:4298:2)
// at build (.../node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/src-DUvB0HDu.cjs:4309:25)
// Issue: input.external is not supported
@huozhi
huozhi / lazy.ts
Created January 12, 2025 16:58
lazy load
// Lazy load a value and cache it from the `getter`
function lazy<T>(getter: () => T): { value: T } {
return {
get value() {
const evaluated = getter()
Object.defineProperty(this, 'value', { value: evaluated })
return evaluated
}
}
@huozhi
huozhi / .zshrc
Created January 12, 2025 14:22
Enable tmux in iterm2 only and not in other terminals
# Check if running in iTerm2 and `tmux` is available
if [[ "$TERM_PROGRAM" == "iTerm.app" ]] && command -v tmux &>/dev/null; then
# Start a new tmux session or attach to an existing one
if [[ -z "$TMUX" ]]; then
tmux attach || tmux new
fi
fi
@huozhi
huozhi / checkout-react-compiler-with-nextjs.sh
Created May 16, 2024 23:47
checkout-react-compiler-with-nextjs.sh
pnpm add next@canary react@beta react-dom@beta
pnpm add -D babel-plugin-react-compiler
echo "module.exports = {
experimental: {
reactCompiler: true
},
}" > next.config.js
@huozhi
huozhi / ts-check.mjs
Created December 3, 2023 16:04
validate types of package
#!/usr/bin/env node
import * as core from '@arethetypeswrong/core'
import { groupProblemsByKind } from '@arethetypeswrong/core/utils'
import { execSync } from 'child_process'
import { readFile, stat, unlink } from 'fs/promises'
import path from 'path'
const problemFlags = {
NoResolution: 'no-resolution',
UntypedResolution: 'untyped-resolution',
@huozhi
huozhi / linear-graident-console-log.ts
Created October 23, 2023 00:47
create linear gradient color
const linearGradientText = (
text: string,
startColor: number[],
endColor: number[]
) => {
let gradient = ''
for (let i = 0; i < text.length; i++) {
const r = Math.floor(
startColor[0] + (i / (text.length - 1)) * (endColor[0] - startColor[0])
@huozhi
huozhi / get-keys.ts
Last active January 16, 2023 17:37
typescript cheat sheet
type KeysOfUnion<T> = T extends T ? keyof T : never
@huozhi
huozhi / insert-text-to-file.sh
Last active September 19, 2022 20:58
inesert text to files
# https://stackoverflow.com/questions/61546603/bash-find-file-and-insert-text-in-beginning-of-empty-and-non-empty-files
# insert `'client'\n` to all .client.tsx files
find . -type f -name '*.client.tsx' -print0 | while IFS= read -rd '' file; do ed -s "$file" <<< $'0a\n\'client\'\n\n.\nw\nq'; done
# rename files
ls | grep \.png$ | sed 'p;s/\.png/\.jpg/' | xargs -n2 mv