Skip to content

Commit 8cd0cdc

Browse files
authored
feat: Add ESM support for common and common-client (rollup) (#604)
1 parent 3389983 commit 8cd0cdc

File tree

16 files changed

+75
-50
lines changed

16 files changed

+75
-50
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ yarn-error.log
2121
.vscode
2222
dump.rdb
2323
.wrangler
24+
stats.html

packages/sdk/browser/contract-tests/entity/src/ClientEntity.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ export class ClientEntity {
156156
if (!identifyParams) {
157157
throw malformedCommand;
158158
}
159-
await this.client.identify(identifyParams.user || identifyParams.context, {
160-
waitForNetworkResults: true,
161-
});
159+
await this.client.identify(identifyParams.user || identifyParams.context);
162160
return undefined;
163161
}
164162

@@ -204,7 +202,7 @@ export async function newSdkClientEntity(options: CreateInstanceParams) {
204202
let failed = false;
205203
try {
206204
await Promise.race([
207-
client.identify(initialContext, { waitForNetworkResults: true }),
205+
client.identify(initialContext),
208206
new Promise((_resolve, reject) => {
209207
setTimeout(reject, timeout);
210208
}),

packages/sdk/browser/jest.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ export default {
33
verbose: true,
44
preset: 'ts-jest/presets/default-esm',
55
testEnvironment: 'jest-environment-jsdom',
6-
transform: {
7-
'^.+\\.tsx?$': ['ts-jest', { useESM: true, tsconfig: 'tsconfig.json' }],
8-
},
96
testPathIgnorePatterns: ['./dist', './src'],
107
testMatch: ['**.test.ts'],
118
};

packages/sdk/browser/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
},
3737
"dependencies": {
3838
"@launchdarkly/js-client-sdk-common": "1.8.0",
39-
"escape-string-regexp": "^5.0.0"
39+
"escape-string-regexp": "^5.0.0",
40+
"rollup-plugin-visualizer": "^5.12.0"
4041
},
4142
"devDependencies": {
4243
"@jest/globals": "^29.7.0",

packages/sdk/browser/rollup.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import json from '@rollup/plugin-json';
33
import resolve from '@rollup/plugin-node-resolve';
44
import terser from '@rollup/plugin-terser';
55
import typescript from '@rollup/plugin-typescript';
6+
import { visualizer } from 'rollup-plugin-visualizer';
67

78
const getSharedConfig = (format, file) => ({
89
input: 'src/index.ts',
@@ -34,6 +35,8 @@ export default [
3435
resolve(),
3536
terser(),
3637
json(),
38+
// The 'sourcemap' option allows using the minified size, not the size before minification.
39+
visualizer({ sourcemap: true }),
3740
],
3841
},
3942
{

packages/sdk/browser/src/BrowserClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import {
1010
LDClientImpl,
1111
LDContext,
1212
LDEmitter,
13+
LDEmitterEventName,
1314
LDHeaders,
1415
Platform,
1516
} from '@launchdarkly/js-client-sdk-common';
16-
import { EventName } from '@launchdarkly/js-client-sdk-common/dist/LDEmitter';
1717

1818
import BrowserDataManager from './BrowserDataManager';
1919
import { BrowserIdentifyOptions as LDIdentifyOptions } from './BrowserIdentifyOptions';
@@ -233,12 +233,12 @@ export class BrowserClient extends LDClientImpl implements LDClient {
233233
browserDataManager.setAutomaticStreamingState(!!this.emitter.listenerCount('change'));
234234
}
235235

236-
override on(eventName: EventName, listener: Function): void {
236+
override on(eventName: LDEmitterEventName, listener: Function): void {
237237
super.on(eventName, listener);
238238
this.updateAutomaticStreamingState();
239239
}
240240

241-
override off(eventName: EventName, listener: Function): void {
241+
override off(eventName: LDEmitterEventName, listener: Function): void {
242242
super.off(eventName, listener);
243243
this.updateAutomaticStreamingState();
244244
}

packages/sdk/browser/src/goals/GoalTracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default class GoalTracker {
7373
);
7474

7575
const pageviewGoals = goalsMatchingUrl.filter((goal) => goal.kind === 'pageview');
76-
const clickGoals = goalsMatchingUrl.filter((goal) => goal.kind === 'click');
76+
const clickGoals = goalsMatchingUrl.filter((goal) => goal.kind === 'click') as ClickGoal[];
7777

7878
pageviewGoals.forEach((event) => onEvent(event));
7979

packages/sdk/browser/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
"allowSyntheticDefaultImports": true,
44
"declaration": true,
55
"declarationMap": true,
6-
"jsx": "react-jsx",
76
"lib": ["ES2017", "dom"],
8-
"module": "ES6",
7+
"module": "ESNext",
98
"moduleResolution": "node",
109
"noImplicitOverride": true,
1110
"resolveJsonModule": true,
File renamed without changes.

packages/shared/common/package.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "@launchdarkly/js-sdk-common",
33
"version": "2.9.0",
4-
"type": "commonjs",
5-
"main": "./dist/index.js",
4+
"type": "module",
5+
"main": "./dist/index.mjs",
66
"types": "./dist/index.d.ts",
77
"homepage": "https://github.com/launchdarkly/js-core/tree/main/packages/shared/common",
88
"repository": {
@@ -18,18 +18,27 @@
1818
"analytics",
1919
"client"
2020
],
21+
"exports": {
22+
"types": "./dist/index.d.ts",
23+
"require": "./dist/index.cjs",
24+
"import": "./dist/index.mjs"
25+
},
2126
"scripts": {
2227
"test": "npx jest --ci",
23-
"build-types": "npx tsc --declaration true --emitDeclarationOnly true --declarationDir dist",
24-
"build": "npx tsc",
25-
"clean": "npx tsc --build --clean",
28+
"build": "npx tsc --noEmit && rollup -c rollup.config.js",
29+
"clean": "rimraf dist",
2630
"lint": "npx eslint . --ext .ts",
2731
"lint:fix": "yarn run lint --fix",
2832
"prettier": "prettier --write 'src/*.@(js|ts|tsx|json)'",
2933
"check": "yarn && yarn prettier && yarn lint && tsc && yarn test"
3034
},
3135
"license": "Apache-2.0",
3236
"devDependencies": {
37+
"@rollup/plugin-commonjs": "^25.0.0",
38+
"@rollup/plugin-json": "^6.1.0",
39+
"@rollup/plugin-node-resolve": "^15.0.2",
40+
"@rollup/plugin-terser": "^0.4.3",
41+
"@rollup/plugin-typescript": "^11.1.1",
3342
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
3443
"@types/jest": "^29.4.0",
3544
"@typescript-eslint/eslint-plugin": "^6.20.0",
@@ -44,7 +53,10 @@
4453
"jest": "^29.5.0",
4554
"launchdarkly-js-test-helpers": "^2.2.0",
4655
"prettier": "^3.0.0",
56+
"rimraf": "6.0.1",
57+
"rollup": "^3.23.0",
4758
"ts-jest": "^29.0.5",
59+
"tslib": "^2.7.0",
4860
"typedoc": "0.25.0",
4961
"typescript": "5.1.6"
5062
}

0 commit comments

Comments
 (0)