diff --git a/AGENTS.md b/AGENTS.md index 9b7f61037..8aa82803a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -8,7 +8,7 @@ Working notes for agents (and humans) contributing to this repository. - **Stay thin.** Anything Chart.js already does belongs to Chart.js. New props and options are added only when they cannot be expressed through Chart.js `options`, `plugins` or a ref to the chart instance. - **Bundle size matters.** The package is tree-shakeable and its size is a tested contract (`.size-limit.json`): a consumer pays only for the components they import. Follow the `tree-shaking` and `size-limit` skills. -- **Compatibility is part of the API.** The package supports every React version from the `peerDependencies` range and ships both ESM and CommonJS builds: CommonJS support was removed once and restored in v5.1 after users broke. Changing either is a breaking change. +- **Compatibility is part of the API.** The package supports every React version from the `peerDependencies` range. Since v6 it ships only the ES module build, which Node.js 20.19+ and 22.12+ can also `require()`. Narrowing either is a breaking change. ## Repository layout diff --git a/README.md b/README.md index 39187c18e..ca8f8bd6a 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ import { Doughnut } from 'react-chartjs-2'; ## Docs +- [Migration to v6](https://react-chartjs-2.js.org/docs/migration-to-v6) - [Migration to v4](https://react-chartjs-2.js.org/docs/migration-to-v4) - [Working with datasets](https://react-chartjs-2.js.org/docs/working-with-datasets) - [Working with events](https://react-chartjs-2.js.org/docs/working-with-events) diff --git a/package.json b/package.json index d54565f57..37dd94eef 100644 --- a/package.json +++ b/package.json @@ -24,14 +24,17 @@ ], "sideEffects": false, "types": "./dist/index.d.ts", - "exports": "./src/index.ts", + "exports": { + ".": "./src/index.ts", + "./package.json": "./package.json" + }, "publishConfig": { - "main": "./dist/index.cjs", - "module": "./dist/index.js", "exports": { - "types": "./dist/index.d.ts", - "import": "./dist/index.js", - "require": "./dist/index.cjs" + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "./package.json": "./package.json" }, "directory": "package", "linkDirectory": false @@ -45,7 +48,9 @@ "prepublishOnly": "run test build clear:package clean-publish", "postpublish": "pnpm clear:package", "emitDeclarations": "tsc --emitDeclarationOnly -p tsconfig.build.json", - "build": "run -p [ vite build ] emitDeclarations", + "clear:dist": "del ./dist", + "build:dist": "run -p [ vite build ] emitDeclarations", + "build": "run clear:dist build:dist", "storybook": "storybook dev -p 6006 --no-open", "storybook:build": "storybook build", "sandboxes:build": "pnpm -r --filter \"./sandboxes/**\" build", diff --git a/vite.config.js b/vite.config.js index fa53c89fe..589cbacbf 100644 --- a/vite.config.js +++ b/vite.config.js @@ -7,8 +7,7 @@ export default defineConfig({ build: { target: 'esnext', lib: { - // CommonJS stays: it was dropped once and restored in v5.1 - formats: ['es', 'cjs'], + formats: ['es'], entry: { index: './src/index.ts' } @@ -16,9 +15,7 @@ export default defineConfig({ rolldownOptions: { external: id => /^(react|chart\.js)(\/|$)/.test(id), output: { - topLevelVar: false, - // rolldown adds the directive only when the sources have it, and CommonJS of the package always ran strict - strict: true + topLevelVar: false } }, sourcemap: true, diff --git a/website/docs/docs.js b/website/docs/docs.js index 1c6e2ebf5..dcd4fd0bc 100644 --- a/website/docs/docs.js +++ b/website/docs/docs.js @@ -1,4 +1,5 @@ exports.docs = [ + { title: 'Migration to v6', slug: '/docs/migration-to-v6' }, { title: 'Migration to v5', slug: '/docs/migration-to-v5' }, { title: 'Migration to v4', slug: '/docs/migration-to-v4' }, { title: 'Working with datasets', slug: '/docs/working-with-datasets' }, diff --git a/website/docs/faq/esm-only.md b/website/docs/faq/esm-only.md index 17581a9bc..5955c4d5b 100644 --- a/website/docs/faq/esm-only.md +++ b/website/docs/faq/esm-only.md @@ -1,7 +1,13 @@ --- slug: /faq/esm-only +description: "Fix the \"Cannot find module 'react-chartjs-2'\" error: react-chartjs-2 v6 is ESM only, so load it as an ES module or stay on v5." --- # How to fix "Cannot find module 'react-chartjs-2'" error? -Upgrade to Chart.js v4.1 and react-chartjs-2 v5.1, where the CommonJS support was restored. +Since v6, react-chartjs-2 is an ESM-only package, and the error means that something tries to load it as CommonJS: + +- Node.js before 20.19 and 22.12 cannot `require()` an ES module: upgrade Node.js, or make your code ESM. +- Jest runs tests as CommonJS: let it transform the package with `transformIgnorePatterns`, run it with ES modules support, or use Vitest. + +See the [migration guide to v6](/docs/migration-to-v6) for the details. If you cannot load ES modules yet, use react-chartjs-2 v5, which ships the CommonJS build. diff --git a/website/docs/migration-to-v6.md b/website/docs/migration-to-v6.md new file mode 100644 index 000000000..70a33d64d --- /dev/null +++ b/website/docs/migration-to-v6.md @@ -0,0 +1,30 @@ +--- +slug: /docs/migration-to-v6 +description: "react-chartjs-2 migration guide to v6: the package is ESM only, the components and their props are the same." +--- + +# Migration to v6 + +v6 ships only the ES module build: the CommonJS build is gone. The components, their props and the event helpers did not change. + +## ESM only + +The package has a single entry, `dist/index.js`, which is an ES module. How your project loads it depends on the tool: + +- **Bundlers** (Vite, webpack 5, Rollup, esbuild, Parcel) and frameworks built on them, such as Next.js, import ES modules as they are: nothing to change. +- **Node.js** 20.19, 22.12 and later can `require()` an ES module, so server-side rendering from CommonJS keeps working there. Older Node.js versions need your code to be ESM, `"type": "module"` in `package.json`, or a dynamic `import()`. +- **Jest** runs tests as CommonJS: either let it transform the package, + + ```js title="jest.config.js" + export default { + transformIgnorePatterns: ['node_modules/(?!(react-chartjs-2)/)'] + } + ``` + + or run Jest with [ES modules support](https://jestjs.io/docs/ecmascript-modules), or move the tests to [Vitest](https://vitest.dev), which runs ES modules natively. + +If none of that is possible yet, keep using react-chartjs-2 v5, which ships the CommonJS build. + +## TypeScript + +The types are published for ES modules only. With `"moduleResolution": "node16"` or `"nodenext"` in a CommonJS project, TypeScript asks to import the package with `import()`: use `"moduleResolution": "bundler"` when a bundler builds the project, or make the project ESM.