From dae3b0d90da48ab4fb420a64f45be908168c4458 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Mon, 9 Dec 2024 09:20:20 -0700 Subject: [PATCH] Fix node 16 cjs import (#2112) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/electric-sql/electric/issues/2107 From running `npm pack` locally on PR: Screenshot 2024-12-06 at 9 24 58 AM The change is making exports explicit for esm or cjs: ```json "exports": { ".": { "import": { "types": "./dist/index.d.ts", "default": "./dist/index.mjs" }, "require": { "types": "./dist/cjs/index.d.cts", "default": "./dist/cjs/index.cjs" } } } ``` And generating separate type declarations. --- .changeset/hot-beds-pump.md | 6 ++ .../nextjs-example/app/shape-proxy/route.ts | 2 +- examples/yjs/app/y-electric.ts | 24 ++++--- packages/react-hooks/package.json | 69 ++++++++++--------- packages/react-hooks/tsconfig.json | 7 +- packages/react-hooks/tsup.config.ts | 46 ++++++++----- packages/typescript-client/package.json | 69 ++++++++++--------- packages/typescript-client/tsconfig.json | 1 + packages/typescript-client/tsup.config.ts | 51 +++++++------- tsconfig.base.json | 7 +- tsconfig.build.json | 10 +-- 11 files changed, 160 insertions(+), 132 deletions(-) create mode 100644 .changeset/hot-beds-pump.md diff --git a/.changeset/hot-beds-pump.md b/.changeset/hot-beds-pump.md new file mode 100644 index 0000000000..8e2f295542 --- /dev/null +++ b/.changeset/hot-beds-pump.md @@ -0,0 +1,6 @@ +--- +"@electric-sql/client": patch +"@electric-sql/react": patch +--- + +Fix node 16 cjs import diff --git a/examples/nextjs-example/app/shape-proxy/route.ts b/examples/nextjs-example/app/shape-proxy/route.ts index 0e66e0506f..aadfae4d3a 100644 --- a/examples/nextjs-example/app/shape-proxy/route.ts +++ b/examples/nextjs-example/app/shape-proxy/route.ts @@ -41,4 +41,4 @@ export async function GET(request: Request) { }) } return resp -} \ No newline at end of file +} diff --git a/examples/yjs/app/y-electric.ts b/examples/yjs/app/y-electric.ts index 50ec608ad4..f6245cf035 100644 --- a/examples/yjs/app/y-electric.ts +++ b/examples/yjs/app/y-electric.ts @@ -316,11 +316,13 @@ export class ElectricProvider extends ObservableV2 { ) { this.synced = true - updateShapeState( - `operations`, - this.operationsStream!.lastOffset, - this.operationsStream!.shapeHandle - ) + if (this.operationsStream?.lastOffset && this.operationsStream?.shapeHandle) { + updateShapeState( + `operations`, + this.operationsStream.lastOffset, + this.operationsStream.shapeHandle + ) + } } }) } @@ -347,11 +349,13 @@ export class ElectricProvider extends ObservableV2 { } }) - updateShapeState( - `awareness`, - this.awarenessStream!.lastOffset, - this.awarenessStream!.shapeHandle - ) + if (this.awarenessStream?.lastOffset && this.awarenessStream?.shapeHandle) { + updateShapeState( + `awareness`, + this.awarenessStream.lastOffset, + this.awarenessStream.shapeHandle + ) + } } const unsubscribeAwarenessHandler = this.awarenessStream.subscribe( diff --git a/packages/react-hooks/package.json b/packages/react-hooks/package.json index c5a74aebdc..c845c37fec 100644 --- a/packages/react-hooks/package.json +++ b/packages/react-hooks/package.json @@ -1,34 +1,11 @@ { "name": "@electric-sql/react", - "version": "0.6.1", "description": "React hooks for ElectricSQL", - "type": "module", - "main": "dist/cjs/index.cjs", - "module": "dist/index.legacy-esm.js", - "types": "dist/index.d.ts", - "exports": { - "./package.json": "./package.json", - ".": { - "types": "./dist/index.d.ts", - "import": "./dist/index.mjs", - "default": "./dist/cjs/index.cjs" - } - }, - "files": [ - "dist", - "src" - ], - "sideEffects": false, - "repository": { - "type": "git", - "url": "git+https://github.com/electric-sql/electric.git" - }, + "version": "0.6.1", "author": "ElectricSQL team and contributors.", - "license": "Apache-2", "bugs": { "url": "https://github.com/electric-sql/electric/issues" }, - "homepage": "https://electric-sql.com", "dependencies": { "@electric-sql/client": "workspace:*", "use-sync-external-store": "^1.2.2" @@ -57,6 +34,27 @@ "uuid": "^10.0.0", "vitest": "^2.0.2" }, + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.mjs" + }, + "require": { + "types": "./dist/cjs/index.d.cts", + "default": "./dist/cjs/index.cjs" + } + } + }, + "files": [ + "dist", + "src" + ], + "homepage": "https://electric-sql.com", + "license": "Apache-2", + "main": "dist/cjs/index.cjs", + "module": "dist/index.legacy-esm.js", "peerDependencies": { "react": "^18.3.1" }, @@ -65,19 +63,26 @@ "optional": true } }, + "repository": { + "type": "git", + "url": "git+https://github.com/electric-sql/electric.git" + }, + "scripts": { + "build": "shx rm -rf dist && tsup && tsc -p tsconfig.build.json", + "format": "eslint . --fix", + "prepack": "pnpm build", + "stylecheck": "eslint . --quiet", + "test": "pnpm exec vitest", + "typecheck": "tsc -p tsconfig.json" + }, + "sideEffects": false, + "type": "module", + "types": "dist/index.d.ts", "typesVersions": { "*": { "*": [ "./dist/index.d.ts" ] } - }, - "scripts": { - "test": "pnpm exec vitest", - "typecheck": "tsc -p tsconfig.json", - "build": "shx rm -rf dist && concurrently \"tsup\" \"tsc -p tsconfig.build.json\"", - "prepack": "pnpm build", - "stylecheck": "eslint . --quiet", - "format": "eslint . --fix" } } diff --git a/packages/react-hooks/tsconfig.json b/packages/react-hooks/tsconfig.json index 9536a0f413..b5c3207611 100644 --- a/packages/react-hooks/tsconfig.json +++ b/packages/react-hooks/tsconfig.json @@ -1,3 +1,8 @@ { - "extends": "../../tsconfig.base.json" + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "paths": { + "@electric-sql/client": ["../typescript-client/src"] + } + } } diff --git a/packages/react-hooks/tsup.config.ts b/packages/react-hooks/tsup.config.ts index 155957c2dd..855d7d5288 100644 --- a/packages/react-hooks/tsup.config.ts +++ b/packages/react-hooks/tsup.config.ts @@ -1,53 +1,61 @@ import type { Options } from 'tsup' import { defineConfig } from 'tsup' -export default defineConfig(options => { +export default defineConfig((options: Options) => { const commonOptions: Partial = { entry: { - index: 'src/index.ts' + index: 'src/index.ts', }, tsconfig: `./tsconfig.build.json`, - // esbuildPlugins: [mangleErrorsTransform], sourcemap: true, - ...options + ...options, } return [ - // Standard ESM, embedded `process.env.NODE_ENV` checks + // ESM build with .d.ts { ...commonOptions, format: ['esm'], - outExtension: () => ({ js: '.mjs' }), // Add dts: '.d.ts' when egoist/tsup#1053 lands - dts: true, - clean: true + outExtension: () => ({ js: '.mjs' }), + dts: { + entry: commonOptions.entry, + resolve: true, + }, + clean: true, }, - // Support Webpack 4 by pointing `"module"` to a file with a `.js` extension + // CJS build with .d.cts + { + ...commonOptions, + format: ['cjs'], + outDir: './dist/cjs/', + outExtension: () => ({ js: '.cjs' }), + dts: { + entry: commonOptions.entry, + resolve: true, + }, + clean: false, + }, + // Support Webpack 4 by pointing "module" to a file with a .js extension { ...commonOptions, format: ['esm'], target: 'es2017', dts: false, outExtension: () => ({ js: '.js' }), - entry: { 'index.legacy-esm': 'src/index.ts' } as Record + entry: { 'index.legacy-esm': 'src/index.ts' } as Record, }, // Browser-ready ESM, production + minified { ...commonOptions, define: { - 'process.env.NODE_ENV': JSON.stringify('production') + 'process.env.NODE_ENV': JSON.stringify('production'), }, format: ['esm'], outExtension: () => ({ js: '.mjs' }), minify: true, entry: { - 'index.browser': 'src/index.ts' - }, + 'index.browser': 'src/index.ts', + } as Record, }, - { - ...commonOptions, - format: 'cjs', - outDir: './dist/cjs/', - outExtension: () => ({ js: '.cjs' }) - } ] }) diff --git a/packages/typescript-client/package.json b/packages/typescript-client/package.json index 342b885513..bc4294f216 100644 --- a/packages/typescript-client/package.json +++ b/packages/typescript-client/package.json @@ -1,34 +1,11 @@ { "name": "@electric-sql/client", - "version": "0.9.0", "description": "Postgres everywhere - your data, in sync, wherever you need it.", - "type": "module", - "main": "dist/cjs/index.cjs", - "module": "dist/index.legacy-esm.js", - "types": "dist/index.d.ts", - "exports": { - "./package.json": "./package.json", - ".": { - "types": "./dist/index.d.ts", - "import": "./dist/index.mjs", - "default": "./dist/cjs/index.cjs" - } - }, - "files": [ - "dist", - "src" - ], - "sideEffects": false, - "repository": { - "type": "git", - "url": "git+https://github.com/electric-sql/electric.git" - }, + "version": "0.9.0", "author": "ElectricSQL team and contributors.", - "license": "Apache-2", "bugs": { "url": "https://github.com/electric-sql/electric/issues" }, - "homepage": "https://electric-sql.com", "dependencies": {}, "devDependencies": { "@types/pg": "^8.11.6", @@ -49,22 +26,50 @@ "uuid": "^10.0.0", "vitest": "^2.0.2" }, + "type": "module", + "exports": { + "./package.json": "./package.json", + ".": { + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.mjs" + }, + "require": { + "types": "./dist/cjs/index.d.cts", + "default": "./dist/cjs/index.cjs" + } + } + }, + "files": [ + "dist", + "src" + ], + "homepage": "https://electric-sql.com", + "license": "Apache-2", + "main": "./dist/cjs/index.cjs", + "module": "./dist/index.legacy-esm.js", + "types": "./dist/index.d.ts", "optionalDependencies": { "@rollup/rollup-darwin-arm64": "^4.18.1" }, + "repository": { + "type": "git", + "url": "git+https://github.com/electric-sql/electric.git" + }, + "scripts": { + "build": "shx rm -rf dist && tsup && tsc -p tsconfig.build.json", + "format": "eslint . --fix", + "prepack": "pnpm build", + "stylecheck": "eslint . --quiet", + "test": "pnpm exec vitest", + "typecheck": "tsc -p tsconfig.json" + }, + "sideEffects": false, "typesVersions": { "*": { "*": [ "./dist/index.d.ts" ] } - }, - "scripts": { - "test": "pnpm exec vitest", - "typecheck": "tsc -p tsconfig.json", - "build": "shx rm -rf dist && concurrently \"tsup\" \"tsc -p tsconfig.build.json\"", - "prepack": "pnpm build", - "stylecheck": "eslint . --quiet", - "format": "eslint . --fix" } } diff --git a/packages/typescript-client/tsconfig.json b/packages/typescript-client/tsconfig.json index 5bd3117ad9..87d0bec14c 100644 --- a/packages/typescript-client/tsconfig.json +++ b/packages/typescript-client/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": "../../tsconfig.base.json", "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ diff --git a/packages/typescript-client/tsup.config.ts b/packages/typescript-client/tsup.config.ts index 075298b6c3..ad899720ed 100644 --- a/packages/typescript-client/tsup.config.ts +++ b/packages/typescript-client/tsup.config.ts @@ -2,59 +2,60 @@ import type { Options } from 'tsup' import { defineConfig } from 'tsup' export default defineConfig((options) => { - const entry = { - index: 'src/index.ts', - } const commonOptions: Partial = { - entry, + entry: { + index: 'src/index.ts', + }, tsconfig: `./tsconfig.build.json`, sourcemap: true, ...options, } return [ - // Standard ESM, embedded `process.env.NODE_ENV` checks + // ESM build with .d.ts { ...commonOptions, format: ['esm'], - outExtension: () => ({ js: '.mjs' }), // Add dts: '.d.ts' when egoist/tsup#1053 lands - dts: true, + outExtension: () => ({ js: '.mjs' }), + dts: { + entry: commonOptions.entry, + resolve: true, + }, clean: true, }, - // Support Webpack 4 by pointing `"module"` to a file with a `.js` extension + // CJS build with .d.cts + { + ...commonOptions, + format: ['cjs'], + outDir: './dist/cjs/', + outExtension: () => ({ js: '.cjs' }), + dts: { + entry: commonOptions.entry, + resolve: true, + }, + clean: false, + }, + // Support Webpack 4 by pointing "module" to a file with a .js extension { ...commonOptions, format: ['esm'], target: 'es2017', dts: false, outExtension: () => ({ js: '.js' }), - entry: Object.fromEntries( - Object.entries(entry).map(([key, value]) => [ - `${key}.legacy-esm`, - value, - ]) - ), + entry: { 'index.legacy-esm': 'src/index.ts' }, }, // Browser-ready ESM, production + minified { ...commonOptions, - - entry: Object.fromEntries( - Object.entries(entry).map(([key, value]) => [`${key}.browser`, value]) - ), - define: { 'process.env.NODE_ENV': JSON.stringify('production'), }, format: ['esm'], outExtension: () => ({ js: '.mjs' }), minify: true, - }, - { - ...commonOptions, - format: 'cjs', - outDir: './dist/cjs/', - outExtension: () => ({ js: '.cjs' }), + entry: { + 'index.browser': 'src/index.ts', + }, }, ] }) diff --git a/tsconfig.base.json b/tsconfig.base.json index d272c3e097..445b21cff9 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -8,11 +8,12 @@ ], "jsx": "preserve", "noEmit": true, - "module": "preserve", + "module": "esnext", + "moduleResolution": "node", "esModuleInterop": true, + "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": true, "strict": true, - "skipLibCheck": true, - "moduleResolution": "bundler" + "skipLibCheck": true } } diff --git a/tsconfig.build.json b/tsconfig.build.json index f73b101227..162b53870c 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,11 +1,3 @@ { - "compilerOptions": { - "incremental": false, - "baseUrl": "./", - "target": "es2016", - "noEmit": true, - "jsx": "preserve", - "module": "preserve", - "moduleResolution": "bundler" - } + "extends": "./tsconfig.base.json", }