From a7e0975075d4ecba81fe77c88909095c9e4abbb2 Mon Sep 17 00:00:00 2001 From: strickczq Date: Sat, 19 Nov 2022 16:52:47 +0800 Subject: [PATCH] react: Fix build with next/webpack + react@<18.0.0 Build only CommonJS to be compatible with `react@<18.0.0`, and use a wrapper to support ESM. --- react/package.json | 8 ++++---- react/vite.config.ts | 22 ++++++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/react/package.json b/react/package.json index 55b155e9..a433d07d 100644 --- a/react/package.json +++ b/react/package.json @@ -1,14 +1,14 @@ { "name": "@giscus/react", "version": "2.2.3", - "main": "dist/index.cjs", - "module": "dist/index.mjs", + "main": "dist/index.js", + "module": "dist/wrapper.mjs", "types": "dist/index.d.ts", "exports": { ".": { "types": "./dist/index.d.ts", - "require": "./dist/index.cjs", - "import": "./dist/index.mjs" + "require": "./dist/index.js", + "import": "./dist/wrapper.mjs" } }, "files": [ diff --git a/react/vite.config.ts b/react/vite.config.ts index 0cf4952c..c5bd964e 100644 --- a/react/vite.config.ts +++ b/react/vite.config.ts @@ -7,15 +7,12 @@ const resolvePath = (str: string) => resolve(__dirname, str); // https://vitejs.dev/config/ export default defineConfig({ - plugins: [react()], + plugins: [react(), wrapper()], build: { lib: { entry: resolve(__dirname, 'src/lib/index.ts'), - formats: ['cjs', 'es'], - fileName: (format) => ({ - cjs: 'index.cjs', - es: 'index.mjs', - }[format]), + formats: ['cjs'], + fileName: 'index', }, rollupOptions: { external: ['react', 'react-dom', 'react/jsx-runtime'], @@ -37,3 +34,16 @@ export default defineConfig({ }, }, }); + +function wrapper() { + return { + name: 'wrapper', + generateBundle() { + this.emitFile({ + type: 'asset', + fileName: 'wrapper.mjs', + source: `import module from './index.js';\n\nexport default module;`, + }); + }, + }; +}