Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 #810

Merged
merged 46 commits into from
May 15, 2024
Merged

v2 #810

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
cdc9c30
prepare for the next major version
dai-shi Nov 6, 2023
2c33752
[v2] breaking: do not throw promises (#813)
dai-shi Nov 12, 2023
e91b0cb
[v2] breaking: do not copy initial objects (#815)
dai-shi Nov 13, 2023
9101ee5
breaking: require TS 4.5 at least (#817)
dai-shi Nov 13, 2023
3aabd0c
[v2] breaking: drop "module" condition (#818)
dai-shi Nov 13, 2023
b662708
Merge branch 'main' into v2
dai-shi Nov 13, 2023
204e073
run prettier
dai-shi Nov 13, 2023
615df9c
[v2] breaking: require react 18 and drop use-sync-external-store (#819)
dai-shi Nov 13, 2023
580dcde
[v2] breaking: remove deprecated features (#820)
dai-shi Nov 14, 2023
8902c4c
[v2] breaking: remove derive-valtio dependency (#821)
dai-shi Nov 14, 2023
5e40034
[v2] breaking: drop UMD/SystemJS builds and simplify babel config (#822)
dai-shi Nov 14, 2023
2e01d72
2.0.0-alpha.0
dai-shi Nov 14, 2023
757a064
Merge branch 'main' into v2
dai-shi Nov 28, 2023
01cdc29
run prettier
dai-shi Nov 28, 2023
f8216c5
2.0.0-alpha.1
dai-shi Nov 28, 2023
83aebbb
simplify ts test script
dai-shi Dec 17, 2023
2b115ca
Merge branch 'main' into v2
dai-shi Dec 17, 2023
1765841
Merge branch 'main' into v2
dai-shi Jan 6, 2024
fe44861
update react canary version
dai-shi Jan 6, 2024
9bffaf6
remove depreacated proxyWithHistory
dai-shi Jan 6, 2024
9e8bcff
2.0.0-alpha.2
dai-shi Jan 6, 2024
90b4c9e
Merge branch 'main' into v2
dai-shi Feb 9, 2024
4162033
update react canary
dai-shi Feb 9, 2024
fae36c9
[v2] export Snapshot type (#856)
dai-shi Feb 9, 2024
990fb1a
2.0.0-beta.0
dai-shi Feb 9, 2024
36b998b
Merge branch 'main' into v2
dai-shi Mar 2, 2024
9d0dc69
[v2] drop es5 (#865)
dai-shi Mar 2, 2024
2fc4b82
breaking: compatibility with memo (#866)
dai-shi Mar 2, 2024
c91a6bf
update yarn lock
dai-shi Mar 2, 2024
a8e4452
2.0.0-beta.1
dai-shi Mar 2, 2024
c04346d
[v2] fix: make affected per proxy (#868)
dai-shi Mar 15, 2024
d7d4a14
2.0.0-beta.2
dai-shi Mar 15, 2024
69505ca
[v2] fix rollup config for cjs (#873)
dai-shi Mar 23, 2024
9431e8a
2.0.0-beta.3
dai-shi Mar 23, 2024
182702f
[v2] migration guide (#878)
dai-shi Apr 5, 2024
0bd9648
Merge branch 'main' into v2
dai-shi Apr 6, 2024
32a63be
fix workflow file
dai-shi Apr 6, 2024
110b4c8
Merge branch 'main' into v2
dai-shi Apr 7, 2024
db2f40d
fix: explicit package.json type field (#882)
dai-shi Apr 8, 2024
648129d
merge main and update proxy-compare
dai-shi Apr 29, 2024
05af9f6
2.0.0-beta.4
dai-shi Apr 29, 2024
7c71869
fix(react): Change to useLayoutEffect in useSnapshot (#891)
endash May 3, 2024
4fe352b
Merge branch 'main' into v2
dai-shi May 4, 2024
07a4fe7
2.0.0-beta.5
dai-shi May 4, 2024
44e2761
Merge branch 'main' into v2
dai-shi May 12, 2024
a93605f
chore package.json
dai-shi May 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: false
matrix:
build: [cjs, esm]
env: [development, production]
env: [development] # [development, production]
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
Expand All @@ -26,10 +26,6 @@ jobs:
cache-dependency-path: '**/pnpm-lock.yaml'
- run: pnpm install --frozen-lockfile
- run: pnpm build
- name: Use React 17 for production test
if: ${{ matrix.env == 'production' }}
run: |
pnpm add -D [email protected] [email protected] @testing-library/[email protected]
- name: Patch for DEV-ONLY
if: ${{ matrix.env == 'development' }}
run: |
Expand All @@ -44,27 +40,13 @@ jobs:
if: ${{ matrix.build == 'cjs' }}
run: |
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\1.js')/" vitest.config.ts
sed -i~ "s/\"valtio\/vanilla\"/\"..\/..\/..\/..\/..\/..\/dist\/vanilla.js\"/" node_modules/derive-valtio/dist/index.umd.js
- name: Patch for ESM
if: ${{ matrix.build == 'esm' }}
run: |
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\/esm\1.mjs')/" vitest.config.ts
sed -i~ "1s/^/import.meta.env=import.meta.env||{};import.meta.env.MODE='${NODE_ENV}';/" tests/*.tsx
env:
NODE_ENV: ${{ matrix.env }}
- name: Patch for UMD
if: ${{ matrix.build == 'umd' }}
run: |
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\/umd\1.${NODE_ENV}.js')/" vitest.config.ts
sed -i~ "s/\"valtio\/vanilla\"/\"..\/..\/..\/..\/..\/..\/dist\/umd\/vanilla.${NODE_ENV}.js\"/" node_modules/derive-valtio/dist/index.umd.js
env:
NODE_ENV: ${{ matrix.env }}
- name: Patch for SystemJS
if: ${{ matrix.build == 'system' }}
run: |
sed -i~ "s/resolve('\.\/src\(.*\)\.ts')/resolve('\.\/dist\/system\1.${NODE_ENV}.js')/" vitest.config.ts
env:
NODE_ENV: ${{ matrix.env }}
- name: Test ${{ matrix.build }} ${{ matrix.env }}
run: |
pnpm test:spec
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/test-multiple-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ jobs:
fail-fast: false
matrix:
react:
- 16.8.0
- 16.9.0
- 17.0.0
- 18.0.0
- 18.1.0
- 18.2.0
Expand Down
43 changes: 4 additions & 39 deletions .github/workflows/test-old-typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ jobs:
- 4.7.4
- 4.6.4
- 4.5.5
- 4.4.4
- 4.3.5
- 4.2.3
- 4.1.5
- 4.0.5
- 3.9.7
- 3.8.3
- 3.7.5
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
Expand All @@ -45,39 +37,12 @@ jobs:
- run: pnpm build
- name: Patch for Old TS
run: |
sed -i~ 's/\/\/ @ts-expect-error.*\[LATEST-TS-ONLY\]//' tests/*.tsx
sed -i~ 's/"target":/"skipLibCheck":true,"target":/' tsconfig.json
sed -i~ 's/"exactOptionalPropertyTypes": true,//' tsconfig.json
sed -i~ 's/"moduleResolution": "bundler",/"moduleResolution": "node",/' tsconfig.json
sed -i~ 's/"allowImportingTsExtensions": true,//' tsconfig.json
sed -i~ 's/"valtio": \["\.\/src\/index\.ts"\],/"valtio": [".\/dist\/ts3.4\/index.d.ts"],/' tsconfig.json
sed -i~ 's/"valtio\/\*": \["\.\/src\/\*\.ts"\]/"valtio\/*": [".\/dist\/ts3.4\/*.d.ts"]/' tsconfig.json
sed -i~ 's/"valtio": \["\.\/src\/index\.ts"\],/"valtio": [".\/dist\/index.d.ts"],/' tsconfig.json
sed -i~ 's/"valtio\/\*": \["\.\/src\/\*\.ts"\]/"valtio\/*": [".\/dist\/*.d.ts"]/' tsconfig.json
sed -i~ 's/"include": .*/"include": ["src\/types.d.ts", "dist\/**\/*", "tests\/**\/*"],/' tsconfig.json
- name: Patch for Older TS
if: ${{ matrix.typescript == '4.4.4' ||matrix.typescript == '4.3.5' || matrix.typescript == '4.2.3' || matrix.typescript == '4.1.5' || matrix.typescript == '4.0.5' || startsWith(matrix.typescript, '3.') }}
run: |
sed -i~ 's/import\.meta\.env/(import.meta.env as any)/' tests/*.tsx
sed -i~ '1s/^/import React from "react";/' tests/*.tsx
sed -i~ 's/"jsx": "react-jsx"/"jsx": "react"/' tsconfig.json
sed -i~ 's/"noUncheckedIndexedAccess": true,//' tsconfig.json
pnpm json -I -f package.json -e "this.resolutions={}; this.resolutions['pretty-format']='25.5.0'; this.resolutions['@types/prettier']='2.4.2'; this.resolutions['@types/yargs']='17.0.13'; this.resolutions['@types/node']='18.11.18';"
pnpm add -D [email protected] @types/[email protected] @types/[email protected] @types/[email protected] @types/[email protected]
rm -r tests/macro-vite.*
- name: Install old TypeScript
run: |
pnpm add -D typescript@${{ matrix.typescript }}
rm node_modules/.pnpm/parse5@*/node_modules/parse5/dist/*.d.ts
- name: Patch testing setup for Old TS
if: ${{ matrix.typescript == '4.4.4' || matrix.typescript == '4.3.5' || matrix.typescript == '4.2.3' || matrix.typescript == '4.1.5' || matrix.typescript == '4.0.5' || startsWith(matrix.typescript, '3.') }}
run: |
pnpm add -D [email protected] @vitest/[email protected] @vitest/[email protected]
- name: Patch testing setup for older TS
if: ${{ matrix.typescript == '4.0.5' || startsWith(matrix.typescript, '3.') }}
run: |
pnpm add -D @testing-library/[email protected] @testing-library/[email protected]
rm node_modules/vitest/dist/*.d.ts
rm node_modules/.pnpm/@types+babel__traverse@*/node_modules/@types/babel__traverse/*.d.ts
echo "declare module 'vitest'" >> ./src/types.d.ts
run: pnpm add -D typescript@${{ matrix.typescript }}
- name: Test ${{ matrix.typescript }}
run: |
pnpm test:types
run: pnpm test:types
28 changes: 0 additions & 28 deletions babel.config.js

This file was deleted.

83 changes: 83 additions & 0 deletions docs/guides/migrating-to-v2.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
title: 'How to Migrate to v2 from v1'
---

# How to Migrate to v2 from v1

## Changes in v2

React 19 officially introduces the `use` hook to handle promises.
Valtio v1 internally handled promises, which is no longer recommended.
In Valtio v2, promises are not handled internally,
and developers should explicitly use the `use` hook to manage promises.

Valtio v2 also introduces several changes in its design choices:

First, the behavior of `proxy(obj)` has changed. In v1, it was a pure function and deeply copied `obj`. In v2, it is an impure function and deeply modifies `obj`. Generally, reusing `obj` is not recommended, and if you have followed this convention, nothing will break.

Second, the behavior of `useSnapshot()` has been altered. Although it is a subtle change, it is less optimized to ensure compatibility with `useMemo` and the upcoming React compiler. The change may lead to extra re-renders in some edge cases, but it might not be noticeable.

Other notable changes to keep things updated and fresh include:

- Removal of all deprecated features
- Requirement of React version 18 and above
- Requirement of TypeScript version 4.5 and above
- The build target updated to ES2018

## Migration for breaking changes

### Resolving promises

```js
// v1
import { proxy, useSnapshot } from 'valtio'

const state = proxy({ data: fetch(...).then((res) => res.json()) })

const Component = () => {
const snap = useSnapshot(state)
return <>{JSON.stringify(snap.data)}</>
}
```

```js
// v2
import { use } from 'react'
import { proxy, useSnapshot } from 'valtio'

const state = proxy({ data: fetch(...).then((res) => res.json()) })

const Component = () => {
const snap = useSnapshot(state)
return <>{JSON.stringify(use(snap.data))}</>
// If `data` is not an object, you can directly embed it in JSX.
// return <>{snap.data}</>
}
```

### Impure `proxy(obj)`

```js
// v1
import { proxy } from 'valtio'

const state = proxy({ count: 1, obj: { text: 'hi' } })
state.obj = { text: 'hello' }
```

```js
// v2
import { proxy } from 'valtio'
import { deepClone } from 'valtio/utils'

const state = proxy(deepClone({ count: 1, obj: { text: 'hi' } }))
state.obj = deepClone({ text: 'hello' })
```

Note that `deepClone` is unnecessary unless you are reusing the object.
It is generally recommended to avoid reusing the object.

## Links

- https://github.com/pmndrs/valtio/discussions/703
- https://github.com/pmndrs/valtio/pull/810
70 changes: 25 additions & 45 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@
"description": "💊 Valtio makes proxy-state simple for React and Vanilla",
"private": true,
"type": "commonjs",
"version": "1.13.2",
"version": "2.0.0-beta.5",
"publishConfig": {
"tag": "next"
},
"main": "./index.js",
"types": "./index.d.ts",
"typesVersions": {
"<4.5": {
">=4.5": {
"esm/*": [
"esm/*"
],
"*": [
"*"
]
},
"*": {
"esm/*": [
"ts3.4/*"
"ts_version_4.5_and_above_is_required.d.ts"
],
"*": [
"ts3.4/*"
"ts_version_4.5_and_above_is_required.d.ts"
]
}
},
Expand All @@ -23,10 +34,6 @@
"types": "./esm/index.d.mts",
"default": "./esm/index.mjs"
},
"module": {
"types": "./esm/index.d.ts",
"default": "./esm/index.js"
},
"default": {
"types": "./index.d.ts",
"default": "./index.js"
Expand All @@ -37,10 +44,6 @@
"types": "./esm/*.d.mts",
"default": "./esm/*.mjs"
},
"module": {
"types": "./esm/*.d.ts",
"default": "./esm/*.js"
},
"default": {
"types": "./*.d.ts",
"default": "./*.js"
Expand All @@ -61,9 +64,7 @@
"build:vanilla_utils": "rollup -c --config-vanilla_utils",
"build:react": "rollup -c --config-react",
"build:react_utils": "rollup -c --config-react_utils",
"build:macro": "rollup -c --config-macro",
"build:macro_vite": "rollup -c --config-macro_vite",
"postbuild": "pnpm patch-d-ts && pnpm copy && pnpm patch-macro-vite && pnpm patch-ts3.4 && pnpm patch-esm-ts",
"postbuild": "pnpm patch-d-ts && pnpm copy && pnpm patch-old-ts && pnpm patch-esm-ts",
"prettier": "prettier '*.{js,json,md}' '{src,tests,docs}/**/*.{ts,tsx,md,mdx}' --write",
"eslint": "eslint --no-eslintrc --c .eslintrc.json --fix '*.{js,json,ts}' '{src,tests}/**/*.{ts,tsx}'",
"test": "pnpm run '/^test:.*/'",
Expand All @@ -72,10 +73,9 @@
"test:lint": "eslint --no-eslintrc --c .eslintrc.json '*.{js,json,ts}' '{src,tests}/**/*.{ts,tsx}'",
"test:spec": "vitest",
"patch-d-ts": "node -e \"var {entries}=require('./rollup.config.js');require('shelljs').find('dist/**/*.d.ts').forEach(f=>{entries.forEach(({find,replacement})=>require('shelljs').sed('-i',new RegExp(' from \\''+find.source.slice(0,-1)+'\\';$'),' from \\''+replacement+'\\';',f));require('shelljs').sed('-i',/ from '(\\.[^']+)\\.ts';$/,' from \\'\\$1\\';',f)})\"",
"copy": "shx cp -r dist/src/* dist/esm && shx cp -r dist/src/* dist && shx rm -rf dist/{src,tests} && downlevel-dts dist dist/ts3.4 && shx cp package.json readme.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined;\"",
"patch-macro-vite": "shx cp dist/esm/macro/vite.d.ts dist/macro/ && shx cp dist/ts3.4/esm/macro/vite.d.ts dist/ts3.4/macro/",
"patch-ts3.4": "node -e \"require('shelljs').find('dist/ts3.4/**/*.d.ts').forEach(f=>{require('fs').appendFileSync(f,'declare type Awaited<T> = T extends Promise<infer V> ? V : T;');require('shelljs').sed('-i',/^declare type Snapshot<T> =/,'declare type Snapshot<T> = T extends SnapshotIgnore ? T : T extends Promise<unknown> ? Awaited<T> : T extends object ? { readonly [K in keyof T]: Snapshot2<T[K]> } : T; declare type Snapshot2<T> = T extends SnapshotIgnore ? T : T extends Promise<unknown> ? Awaited<T> : T extends object ? { readonly [K in keyof T]: T[K] } : T;;declare type _Snapshot<T> =',f)})\"",
"patch-esm-ts": "node -e \"require('shelljs').find('dist/esm/**/*.d.ts').forEach(f=>{var f2=f.replace(/\\.ts$/,'.mts');require('fs').copyFileSync(f,f2);require('shelljs').sed('-i',/ from '(\\.[^']+)';$/,' from \\'\\$1.mjs\\';',f2);require('shelljs').sed('-i',/^declare module '(\\.[^']+)'/,'declare module \\'\\$1.mjs\\'',f2)})\""
"copy": "shx cp -r dist/src/* dist/esm && shx cp -r dist/src/* dist && shx rm -rf dist/{src,tests} && shx cp package.json readme.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined;\"",
"patch-old-ts": "shx touch dist/ts_version_4.5_and_above_is_required.d.ts",
"patch-esm-ts": "node -e \"require('shelljs').find('dist/esm/**/*.d.ts').forEach(f=>{var f2=f.replace(/\\.ts$/,'.mts');require('fs').renameSync(f,f2);require('shelljs').sed('-i',/ from '(\\.[^']+)';$/,' from \\'\\$1.mjs\\';',f2);require('shelljs').sed('-i',/^declare module '(\\.[^']+)'/,'declare module \\'\\$1.mjs\\'',f2)})\""
},
"engines": {
"node": ">=12.20.0"
Expand Down Expand Up @@ -106,42 +106,22 @@
"homepage": "https://github.com/pmndrs/valtio",
"packageManager": "[email protected]",
"dependencies": {
"derive-valtio": "0.1.0",
"proxy-compare": "2.6.0",
"use-sync-external-store": "1.2.2"
"proxy-compare": "^3.0.0"
},
"devDependencies": {
"@babel/core": "^7.24.5",
"@babel/helper-module-imports": "^7.24.3",
"@babel/plugin-transform-react-jsx": "^7.23.4",
"@babel/plugin-transform-typescript": "^7.24.5",
"@babel/preset-env": "^7.24.5",
"@babel/types": "^7.24.5",
"@redux-devtools/extension": "^3.3.0",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"@testing-library/react": "^15.0.6",
"@typed-macro/core": "1.0.0-alpha",
"@typed-macro/runtime": "1.0.0-alpha",
"@types/babel-plugin-macros": "^3.1.3",
"@types/babel-plugin-tester": "^9.0.10",
"@types/babel__traverse": "^7.20.5",
"@types/jsdom": "^21.1.6",
"@types/react": "18.2.56",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"@types/use-sync-external-store": "^0.0.6",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@vitest/coverage-v8": "^1.6.0",
"@vitest/ui": "^1.6.0",
"aslemammad-vite-plugin-macro": "^1.0.0",
"babel-plugin-macros": "^3.1.0",
"babel-plugin-tester": "10.1.0",
"downlevel-dts": "^0.11.0",
"esbuild": "^0.20.2",
"eslint": "8.57.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -157,8 +137,8 @@
"postinstall-postinstall": "^2.1.0",
"prettier": "^3.2.5",
"proxy-memoize": "^3.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react": "19.0.0-beta-1beb73de0f-20240503",
"react-dom": "19.0.0-beta-1beb73de0f-20240503",
"redux": "^5.0.1",
"rollup": "^4.17.2",
"rollup-plugin-esbuild": "^6.1.1",
Expand All @@ -174,8 +154,8 @@
"@types/react": "18.2.56"
},
"peerDependencies": {
"@types/react": ">=16.8",
"react": ">=16.8"
"@types/react": ">=18.0.0",
"react": ">=18.0.0"
},
"peerDependenciesMeta": {
"@types/react": {
Expand Down
Loading
Loading