Skip to content

Commit

Permalink
Fix node 16 cjs import (#2112)
Browse files Browse the repository at this point in the history
Fixes #2107

From running `npm pack` locally on PR:

<img width="671" alt="Screenshot 2024-12-06 at 9 24 58 AM"
src="https://github.com/user-attachments/assets/322af457-bcd0-4f66-b355-e07545207ab1">

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.
  • Loading branch information
KyleAMathews authored Dec 9, 2024
1 parent abad10e commit dae3b0d
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 132 deletions.
6 changes: 6 additions & 0 deletions .changeset/hot-beds-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@electric-sql/client": patch
"@electric-sql/react": patch
---

Fix node 16 cjs import
2 changes: 1 addition & 1 deletion examples/nextjs-example/app/shape-proxy/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ export async function GET(request: Request) {
})
}
return resp
}
}
24 changes: 14 additions & 10 deletions examples/yjs/app/y-electric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,13 @@ export class ElectricProvider extends ObservableV2<ObservableProvider> {
) {
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
)
}
}
})
}
Expand All @@ -347,11 +349,13 @@ export class ElectricProvider extends ObservableV2<ObservableProvider> {
}
})

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(
Expand Down
69 changes: 37 additions & 32 deletions packages/react-hooks/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
},
Expand All @@ -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"
}
}
7 changes: 6 additions & 1 deletion packages/react-hooks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": "../../tsconfig.base.json"
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"paths": {
"@electric-sql/client": ["../typescript-client/src"]
}
}
}
46 changes: 27 additions & 19 deletions packages/react-hooks/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -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<Options> = {
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<string, string>
entry: { 'index.legacy-esm': 'src/index.ts' } as Record<string, string>,
},
// 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<string, string>,
},
{
...commonOptions,
format: 'cjs',
outDir: './dist/cjs/',
outExtension: () => ({ js: '.cjs' })
}
]
})
69 changes: 37 additions & 32 deletions packages/typescript-client/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}
1 change: 1 addition & 0 deletions packages/typescript-client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

Expand Down
Loading

0 comments on commit dae3b0d

Please sign in to comment.