Skip to content

Commit

Permalink
react: Fix build with next/webpack + react@<18.0.0
Browse files Browse the repository at this point in the history
build only cjs to be compatible with `react@<18.0.0`, and use a wrapper to support esm.
  • Loading branch information
strickczq committed Nov 19, 2022
1 parent 906d1a6 commit 471058c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
8 changes: 4 additions & 4 deletions react/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
22 changes: 16 additions & 6 deletions react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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'; export default module;`,
});
},
};
}

0 comments on commit 471058c

Please sign in to comment.