From b5fcfdfeabeab6aaf4d5e576b5244eaa0e4933e0 Mon Sep 17 00:00:00 2001 From: Nathan Vasse Date: Tue, 23 May 2023 15:09:21 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(react)=20add=20support=20for=20CJS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At first we wanted to only support ESM. But as we want to share types and export design tokens from the react package to the outside world in order to allow local cunningham.ts files to import them, we are forced to also build the library for CJS too. Why? Because local cunningham.ts files are loaded as CJS, so they can only import CJS module. Why loading cunningham.ts as CJS? Because the tokens package's binary is built for CJS, then it's seamless to make it load CJS, where making it load ESM was buggy. And why not migrate the tokens package to ESM so? Because it implies to rewrite every imports with .js extensions, makes us loose the possibility to use __dirname kind of variables. And also Jest use for testing is not compliant at all when it comes to execute ESM code. Well, the ecosystem is not ready for that at the moment, sadly. --- .changeset/dry-maps-speak.md | 5 +++++ packages/react/package.json | 7 ++++--- packages/react/vite.config.ts | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 .changeset/dry-maps-speak.md diff --git a/.changeset/dry-maps-speak.md b/.changeset/dry-maps-speak.md new file mode 100644 index 0000000..f5d5a22 --- /dev/null +++ b/.changeset/dry-maps-speak.md @@ -0,0 +1,5 @@ +--- +"@openfun/cunningham-react": minor +--- + +add support for CJS diff --git a/packages/react/package.json b/packages/react/package.json index 80ad243..a2cd239 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -5,14 +5,15 @@ "publishConfig": { "access": "public" }, - "type": "module", "license": "MIT", - "module": "./dist/index.js", + "main": "./dist/index.js", + "module": "./dist/index.mjs", "types": "./dist/index.d.ts", "exports": { ".": { "types": "./dist/index.d.ts", - "default": "./dist/index.js" + "require": "./dist/index.js", + "default": "./dist/index.mjs" }, "./style": "./dist/style.css", "./fonts": "./dist/fonts.css", diff --git a/packages/react/vite.config.ts b/packages/react/vite.config.ts index 3e04c08..a450061 100644 --- a/packages/react/vite.config.ts +++ b/packages/react/vite.config.ts @@ -14,7 +14,7 @@ export default defineConfig({ entry: { index: "./src/index.ts", }, - formats: ["es"], + formats: ["es", "cjs"], }, rollupOptions: { external: ["react", "react-dom"],