Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't merge: Demonstrate Node exports fix #1149

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions node-exports-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bundler-dist
73 changes: 73 additions & 0 deletions node-exports-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
Node Export Example
===================================

This example tests the dual package hazard for `@connectrpc/connect` v1.1.3 and verifies
that the Node exports change does not cause regressions.

`intermediary` is a CommonJS-only package that re-exports from `@connectrpc/connect`.

`consumer-*` are ESM packages that directly import from `@connectrpc/connect`, but
also from the intermediary.

In previous versions of Connect, this caused duplicated identities: `instanceof` did not work as expected, and
bundle size would unnecessarily increase in a web project.

This was fixed in v1.1.2 of Connect ([PR #842](https://github.com/connectrpc/connect-es/pull/842)), which
means the above failures should no longer occur.

### How to run

We have 5 test cases, from a to e. They simply do a strict comparison (`===`)
between imports from `@connectrpc/connect` and imports from `itermediary`.

To run the tests in several versions of Node.js, and also with various bundlers,
run:

```bash
$ npm ci
$ bash test.bash
```

Expected output for test is:

```
a OK
b OK
c OK
d OK
e OK
```

### Testing the Node exports change

Clone branch `sayers/node_exports` from https://github.com/connectrpc/connect-es.

```bash
cd packages/connect
npm run build
npm pack
```

Install the fix in this example:

```bash
tar -xvf <PATH_TO_CONNECT_ES>/packages/connect/connectrpc-connect-1.1.3.tgz
rm -rf node_modules/@connectrpc/connect
mv package node_modules/@connectrpc/connect
```

Run the tests again:

```bash
$ bash test.bash
```

Expected output for every test should still be:

```
a OK
b OK
c OK
d OK
e OK
```
16 changes: 16 additions & 0 deletions node-exports-example/consumer-esbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "consumer-esbuild",
"type": "module",
"dependencies": {
"intermediary": "0.0.1",
"@connectrpc/connect": "^1.0.0"
},
"devDependencies": {
"esbuild": "0.19.4"
},
"sideEffects": false,
"scripts": {
"start": "node dist/index.js",
"prestart": "rm -rf dist && esbuild --log-level=error ./src/index.js --bundle --outfile=dist/index.js"
}
}
14 changes: 14 additions & 0 deletions node-exports-example/consumer-esbuild/src/direct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
console.log('direct load')
import { ConnectError as a } from "@connectrpc/connect";
import { createFetchClient as b } from "@connectrpc/connect/protocol";
import { createTransport as c } from "@connectrpc/connect/protocol-grpc";
import { createTransport as d } from "@connectrpc/connect/protocol-grpc-web";
import { createTransport as e } from "@connectrpc/connect/protocol-connect";

export const direct = {
a,
b,
c,
d,
e,
};
26 changes: 26 additions & 0 deletions node-exports-example/consumer-esbuild/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
console.log('getting direct')
import { direct } from "./direct.js";
console.log('getting intermediary')
import { intermediary } from "intermediary";

let failed = false;
for (const name of "abcde".split("")) {
const directSymbol = direct[name];
const interSymbol = intermediary[name];
if (!directSymbol) {
throw `missing direct ${name}`;
}
if (!interSymbol) {
throw `missing intermediary ${name}`;
}
if (directSymbol === interSymbol) {
console.log(`${name} OK`)
} else {
failed = true;
console.error(`${name} FAIL`)
}
}

// if (failed) {
// process.exit(1);
// }
13 changes: 13 additions & 0 deletions node-exports-example/consumer-node/direct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ConnectError as a } from "@connectrpc/connect";
import { createFetchClient as b } from "@connectrpc/connect/protocol";
import { createTransport as c } from "@connectrpc/connect/protocol-grpc";
import { createTransport as d } from "@connectrpc/connect/protocol-grpc-web";
import { createTransport as e } from "@connectrpc/connect/protocol-connect";

export const direct = {
a,
b,
c,
d,
e,
};
24 changes: 24 additions & 0 deletions node-exports-example/consumer-node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { direct } from "./direct.js";
import { intermediary } from "intermediary";

let failed = false;
for (const name of "abcde".split("")) {
const directSymbol = direct[name];
const interSymbol = intermediary[name];
if (!directSymbol) {
throw `missing direct ${name}`;
}
if (!interSymbol) {
throw `missing intermediary ${name}`;
}
if (directSymbol === interSymbol) {
console.log(`${name} OK`)
} else {
failed = true;
console.error(`${name} FAIL`)
}
}

if (failed) {
process.exit(1);
}
11 changes: 11 additions & 0 deletions node-exports-example/consumer-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "consumer-node",
"type": "module",
"dependencies": {
"intermediary": "0.0.1",
"@connectrpc/connect": "^1.0.0"
},
"scripts": {
"start": "bash start.bash"
}
}
11 changes: 11 additions & 0 deletions node-exports-example/consumer-node/start.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set -x

# you'll need https://github.com/Schniz/fnm

fnm exec --using=v20 node ./index.js
fnm exec --using=v18 node ./index.js
fnm exec --using=v16 node ./index.js
fnm exec --using=v14 node ./index.js
fnm exec --using=v12 node ./index.js

exit 0
19 changes: 19 additions & 0 deletions node-exports-example/consumer-parcel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "consumer-parcel",
"type": "module",
"source": "src/index.js",
"main": "dist/main.js",
"module": "dist/module.js",
"dependencies": {
"intermediary": "0.0.1",
"@connectrpc/connect": "^1.0.0"
},
"devDependencies": {
"parcel": "^2.9.3"
},
"sideEffects": false,
"scripts": {
"start": "node dist/main.js",
"prestart": "rm -rf dist && parcel build --log-level error --no-cache"
}
}
13 changes: 13 additions & 0 deletions node-exports-example/consumer-parcel/src/direct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ConnectError as a } from "@connectrpc/connect";
import { createFetchClient as b } from "@connectrpc/connect/protocol";
import { createTransport as c } from "@connectrpc/connect/protocol-grpc";
import { createTransport as d } from "@connectrpc/connect/protocol-grpc-web";
import { createTransport as e } from "@connectrpc/connect/protocol-connect";

export const direct = {
a,
b,
c,
d,
e,
};
24 changes: 24 additions & 0 deletions node-exports-example/consumer-parcel/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { direct } from "./direct.js";
import { intermediary } from "intermediary";

let failed = false;
for (const name of "abcde".split("")) {
const directSymbol = direct[name];
const interSymbol = intermediary[name];
if (!directSymbol) {
throw `missing direct ${name}`;
}
if (!interSymbol) {
throw `missing intermediary ${name}`;
}
if (directSymbol === interSymbol) {
console.log(`${name} OK`)
} else {
failed = true;
console.error(`${name} FAIL`)
}
}

// if (failed) {
// process.exit(1);
// }
18 changes: 18 additions & 0 deletions node-exports-example/consumer-rollup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "consumer-rollup",
"type": "module",
"dependencies": {
"intermediary": "0.0.1",
"@connectrpc/connect": "^1.0.0"
},
"devDependencies": {
"rollup": "^3.29.4",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-node-resolve": "^15.2.1"
},
"sideEffects": false,
"scripts": {
"start": "node dist/index.js",
"prestart": "rm -rf dist && rollup --silent -c rollup.config.js ./src/index.js --file dist/index.js --format umd"
}
}
6 changes: 6 additions & 0 deletions node-exports-example/consumer-rollup/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
plugins: [nodeResolve(), commonjs()]
};
13 changes: 13 additions & 0 deletions node-exports-example/consumer-rollup/src/direct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ConnectError as a } from "@connectrpc/connect";
import { createFetchClient as b } from "@connectrpc/connect/protocol";
import { createTransport as c } from "@connectrpc/connect/protocol-grpc";
import { createTransport as d } from "@connectrpc/connect/protocol-grpc-web";
import { createTransport as e } from "@connectrpc/connect/protocol-connect";

export const direct = {
a,
b,
c,
d,
e,
};
24 changes: 24 additions & 0 deletions node-exports-example/consumer-rollup/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { direct } from "./direct.js";
import { intermediary } from "intermediary";

let failed = false;
for (const name of "abcde".split("")) {
const directSymbol = direct[name];
const interSymbol = intermediary[name];
if (!directSymbol) {
throw `missing direct ${name}`;
}
if (!interSymbol) {
throw `missing intermediary ${name}`;
}
if (directSymbol === interSymbol) {
console.log(`${name} OK`)
} else {
failed = true;
console.error(`${name} FAIL`)
}
}

// if (failed) {
// process.exit(1);
// }
16 changes: 16 additions & 0 deletions node-exports-example/consumer-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "consumer-ts",
"type": "module",
"dependencies": {
"intermediary": "0.0.1",
"@connectrpc/connect": "^1.0.0"
},
"devDependencies": {
"typescript": "^5.2.2"
},
"sideEffects": false,
"scripts": {
"start": "node dist/index.js",
"prestart": "rm -rf dist && tsc"
}
}
13 changes: 13 additions & 0 deletions node-exports-example/consumer-ts/src/direct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ConnectError as a } from "@connectrpc/connect";
import { createFetchClient as b } from "@connectrpc/connect/protocol";
import { createTransport as c } from "@connectrpc/connect/protocol-grpc";
import { createTransport as d } from "@connectrpc/connect/protocol-grpc-web";
import { createTransport as e } from "@connectrpc/connect/protocol-connect";

export const direct = {
a,
b,
c,
d,
e,
};
18 changes: 18 additions & 0 deletions node-exports-example/consumer-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { direct } from "./direct.js";
import { intermediary } from "intermediary";

for (const name of "abcde".split("") as (keyof typeof direct)[]) {
const directSymbol = direct[name];
const interSymbol = intermediary[name];
if (!directSymbol) {
throw `missing direct ${name}`;
}
if (!interSymbol) {
throw `missing intermediary ${name}`;
}
if (directSymbol === interSymbol) {
console.log(`${name} OK`)
} else {
console.error(`${name} FAIL`)
}
}
14 changes: 14 additions & 0 deletions node-exports-example/consumer-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"files": ["src/index.ts"],
"compilerOptions": {
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// this
"module": "Node16", /* Specify what module code is generated. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": false, /* Skip type checking all .d.ts files. */
"outDir": "dist",
"declaration": true
}
}
Loading