Skip to content

Commit 2d2accd

Browse files
authored
fix: Fix common package to work with node16 module resolution. (#627)
When using typescript with moduleResolution set to "node16" types were not loading which are part of the common package. This relates to changes to the default was in which file resolution occurs. This PR will address this, but we will want a better long-term fix. In this PR we convince node that the `.d.ts` files are the correct extension by making a small `package.json` that sets the module type. In the future we would want the `.d.ts` files to instead be `.d.cts`. Potentially we can move to tsup: https://tsup.egoist.dev/ But the changes were too extensive for this fix. Testing: tsconfig.json ``` { "$schema": "https://json.schemastore.org/tsconfig", "_version": "20.1.0", "compilerOptions": { "lib": ["es2023"], "module": "node16", "target": "es2022", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "moduleResolution": "node16" } } ``` package.json ``` { "name": "hello-node-typescript", "version": "1.0.0", "description": "Hello LaunchDarkly for Node.js with TypeScript", "main": "index.ts", "scripts": { "start": "ts-node ./index.ts" }, "author": "LaunchDarkly <[email protected]>", "license": "Apache-2.0", "devDependencies": { "@types/node": "*", "ts-node": "*", "typescript": "*" }, "dependencies": { "@launchdarkly/node-server-sdk": "../js-core/packages/sdk/server-node", "@launchdarkly/js-server-sdk-common": "../js-core/packages/shared/sdk-server", "@launchdarkly/js-sdk-common": "../js-core/packages/shared/common" } } ``` Code: ``` import { init } from '@launchdarkly/node-server-sdk'; import type { LDSingleKindContext, LDLogger, LDOptions } from '@launchdarkly/node-server-sdk'; // Set sdkKey to your LaunchDarkly SDK key. const sdkKey = ""; function showMessage(s: string) { console.log("*** " + s); console.log(""); } const logger: LDLogger = { debug: console.debug, info: console.info, warn: console.warn, error: console.error, }; const options: LDOptions = { logger }; const client = init(sdkKey, options); client.once('ready', async function () { showMessage("SDK successfully initialized!"); const context: LDSingleKindContext = {kind: 'user', key: 'bob'}; const res = await client.variation('my-boolean-flag', context, false); console.log("The result", res); }); ``` This uses a few types, such as the context, logger, and options to ensure they are resolving correctly.
1 parent dee53af commit 2d2accd

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

packages/shared/common/package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "@launchdarkly/js-sdk-common",
33
"version": "2.10.0",
44
"type": "module",
5-
"main": "./dist/index.mjs",
6-
"types": "./dist/index.d.ts",
5+
"main": "./dist/esm/index.mjs",
6+
"types": "./dist/esm/index.d.ts",
77
"homepage": "https://github.com/launchdarkly/js-core/tree/main/packages/shared/common",
88
"repository": {
99
"type": "git",
@@ -19,13 +19,15 @@
1919
"client"
2020
],
2121
"exports": {
22-
"types": "./dist/index.d.ts",
23-
"require": "./dist/index.cjs",
24-
"import": "./dist/index.mjs"
22+
"require": { "types": "./dist/cjs/index.d.ts", "default": "./dist/cjs/index.cjs"},
23+
"import": { "types": "./dist/esm/index.d.ts", "default": "./dist/esm/index.mjs"}
2524
},
2625
"scripts": {
2726
"test": "npx jest --ci",
28-
"build": "npx tsc --noEmit && rollup -c rollup.config.js",
27+
"make-cjs-package-json": "echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
28+
"make-esm-package-json": "echo '{\"type\":\"module\"}' > dist/esm/package.json",
29+
"make-package-jsons": "npm run make-cjs-package-json && npm run make-esm-package-json",
30+
"build": "npx tsc --noEmit && rollup -c rollup.config.js && npm run make-package-jsons",
2931
"clean": "rimraf dist",
3032
"lint": "npx eslint . --ext .ts",
3133
"lint:fix": "yarn run lint --fix",

packages/shared/common/rollup.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const getSharedConfig = (format, file) => ({
2020

2121
export default [
2222
{
23-
...getSharedConfig('es', 'dist/index.mjs'),
23+
...getSharedConfig('es', 'dist/esm/index.mjs'),
2424
plugins: [
2525
typescript({
2626
module: 'esnext',
@@ -35,7 +35,7 @@ export default [
3535
],
3636
},
3737
{
38-
...getSharedConfig('cjs', 'dist/index.cjs'),
39-
plugins: [typescript({ tsconfig: './tsconfig.json' }), common(), json()],
38+
...getSharedConfig('cjs', 'dist/cjs/index.cjs'),
39+
plugins: [typescript({ tsconfig: './tsconfig.json', outputToFilesystem: true, }), common(), json()],
4040
},
4141
];

packages/shared/common/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"sourceMap": true,
1414
"declaration": true,
1515
"declarationMap": true, // enables importers to jump to source
16-
"stripInternal": true,
17-
"composite": true
16+
"stripInternal": true
1817
},
1918
"include": ["src"],
2019
"exclude": ["**/*.test.ts", "dist", "node_modules", "__tests__"]

packages/shared/sdk-client/src/DataManager.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
ProcessStreamResponse,
1010
subsystem,
1111
} from '@launchdarkly/js-sdk-common';
12-
import { LDStreamProcessor } from '@launchdarkly/js-sdk-common/dist/api/subsystem';
1312

1413
import { LDIdentifyOptions } from './api/LDIdentifyOptions';
1514
import { Configuration } from './configuration/Configuration';
@@ -203,9 +202,9 @@ export abstract class BaseDataManager implements DataManager {
203202
}
204203

205204
private _decorateProcessorWithStatusReporting(
206-
processor: LDStreamProcessor,
205+
processor: subsystem.LDStreamProcessor,
207206
statusManager: DataSourceStatusManager,
208-
): LDStreamProcessor {
207+
): subsystem.LDStreamProcessor {
209208
return {
210209
start: () => {
211210
// update status before starting processor to ensure potential errors are reported after initializing

0 commit comments

Comments
 (0)