Skip to content

Commit 7d2629f

Browse files
authored
Support esModuleInterop set to false. (#1717)
* Support esModuleInterop set to false. When testing the upcoming 4.x release, we got a bunch of typescript errors emitted from this project. We quickly realized this is because the library uses the esModuleInterop flag. This makes some imports _slightly_ easier to write, but it comes at a cost: it forces any application or library using this library to *also* have esModuleInterop on. The `esModuleInterop` flag is a bit of a holdover from an earlier time, and I would not recommend using it in libraries. The main issue is that if it's set to true, you are forcing any users of the library to also have `esModuleInterop`, where if you keep have it set to `false` (the default), you leave the decision to the user. This change should have no rammifications to users with `esModuleInterop` on, but it will enable support for those that have it off. This is especially good for library authors such as myself, because I would also like to keep this flag off to not force *my* users into this feature. * All tests now pass! * Move @types/redis-parser into client sub-package and removed a comma
1 parent ddf539a commit 7d2629f

File tree

10 files changed

+104
-64
lines changed

10 files changed

+104
-64
lines changed

package-lock.json

Lines changed: 79 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/client/lib/client/commands-queue.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import LinkedList from 'yallist';
2-
import RedisParser from 'redis-parser';
1+
import * as LinkedList from 'yallist';
32
import { AbortError } from '../errors';
43
import { RedisCommandArguments, RedisCommandRawReply } from '../commands';
54

5+
// We need to use 'require', because it's not possible with Typescript to import
6+
// classes that are exported as 'module.exports = class`, without esModuleInterop
7+
// set to true.
8+
const RedisParser = require('redis-parser');
9+
610
export interface QueueCommandOptions {
711
asap?: boolean;
812
chainId?: symbol;

packages/client/lib/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import RedisSocket, { RedisSocketOptions, RedisNetSocketOptions, RedisTlsSocketO
44
import RedisCommandsQueue, { PubSubListener, PubSubSubscribeCommands, PubSubUnsubscribeCommands, QueueCommandOptions } from './commands-queue';
55
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
66
import { RedisMultiQueuedCommand } from '../multi-command';
7-
import EventEmitter from 'events';
7+
import { EventEmitter } from 'events';
88
import { CommandOptions, commandOptions, isCommandOptions } from '../command-options';
99
import { ScanOptions, ZMember } from '../commands/generic-transformers';
1010
import { ScanCommandOptions } from '../commands/SCAN';

packages/client/lib/client/socket.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import EventEmitter from 'events';
2-
import net from 'net';
3-
import tls from 'tls';
1+
import { EventEmitter } from 'events';
2+
import * as net from 'net';
3+
import * as tls from 'tls';
44
import { encodeCommand } from '../commander';
55
import { RedisCommandArguments } from '../commands';
66
import { ConnectionTimeoutError, ClientClosedError, SocketClosedUnexpectedlyError } from '../errors';

packages/client/lib/cluster/cluster-slots.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import calculateSlot from 'cluster-key-slot';
21
import RedisClient, { InstantiableRedisClient, RedisClientType } from '../client';
32
import { RedisClusterMasterNode, RedisClusterReplicaNode } from '../commands/CLUSTER_NODES';
43
import { RedisClusterClientOptions, RedisClusterOptions } from '.';
54
import { RedisModules, RedisScripts } from '../commands';
65

6+
// We need to use 'require', because it's not possible with Typescript to import
7+
// function that are exported as 'module.exports = function`, without esModuleInterop
8+
// set to true.
9+
const calculateSlot = require('cluster-key-slot');
10+
711
export interface ClusterNode<M extends RedisModules, S extends RedisScripts> {
812
id: string;
913
client: RedisClientType<M, S>;

packages/client/lib/cluster/index.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { strict as assert } from 'assert';
22
import testUtils, { GLOBAL } from '../test-utils';
3-
import calculateSlot from 'cluster-key-slot';
43
import { ClusterSlotStates } from '../commands/CLUSTER_SETSLOT';
54
import { SQUARE_SCRIPT } from '../client/index.spec';
65

6+
// We need to use 'require', because it's not possible with Typescript to import
7+
// function that are exported as 'module.exports = function`, without esModuleInterop
8+
// set to true.
9+
const calculateSlot = require('cluster-key-slot');
10+
711
describe('Cluster', () => {
812
testUtils.testWithCluster('sendCommand', async cluster => {
913
await cluster.connect();

packages/client/lib/ts-declarations/redis-parser.d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@istanbuljs/nyc-config-typescript": "^1.0.1",
2121
"@redis/test-utils": "*",
2222
"@types/node": "^16.11.7",
23+
"@types/redis-parser": "^3.0.0",
2324
"@types/sinon": "^10.0.6",
2425
"@types/yallist": "^4.0.1",
2526
"@typescript-eslint/eslint-plugin": "^5.3.1",

packages/test-utils/lib/dockers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { once } from 'events';
33
import { RedisModules, RedisScripts } from '@redis/client/lib/commands';
44
import RedisClient, { RedisClientType } from '@redis/client/lib/client';
55
import { promiseTimeout } from '@redis/client/lib/utils';
6-
import path from 'path';
6+
import * as path from 'path';
77
import { promisify } from 'util';
88
import { exec } from 'child_process';
99
const execAsync = promisify(exec);

tsconfig.base.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"compilerOptions": {
44
"declaration": true,
55
"allowJs": true,
6-
"useDefineForClassFields": true
6+
"useDefineForClassFields": true,
7+
"esModuleInterop": false
78
},
89
"files": [
9-
"./packages/client/lib/ts-declarations/cluster-key-slot.d.ts",
10-
"./packages/client/lib/ts-declarations/redis-parser.d.ts"
10+
"./packages/client/lib/ts-declarations/cluster-key-slot.d.ts"
1111
],
1212
"ts-node": {
1313
"files": true

0 commit comments

Comments
 (0)