Skip to content

Commit ec4377c

Browse files
authored
feat: Refine CJS/ESM build configuration for browser SDK. (#640)
The goals here are to: - Make the build simpler. - Make the build "modern". Currently we have worked around some build issues in other packages. Mainly with node module resolution. Despite the settings in the package.json node wants certain file extensions for certain types including .cts for the CJS type definitions. So far tsup seems to be the simplest way to get all things with expected names without a multi-step process. Or without having to make a hack package.json that just has the modules type. Please review #637 first.
1 parent 44a2237 commit ec4377c

File tree

5 files changed

+57
-80
lines changed

5 files changed

+57
-80
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import { LDLogger } from '@launchdarkly/js-client-sdk';
22

33
export function makeLogger(tag: string): LDLogger {
44
return {
5-
debug(message, ...args: any[]) {
5+
debug(message: any, ...args: any[]) {
66
// eslint-disable-next-line no-console
77
console.debug(`${new Date().toISOString()} [${tag}]: ${message}`, ...args);
88
},
9-
info(message, ...args: any[]) {
9+
info(message: any, ...args: any[]) {
1010
// eslint-disable-next-line no-console
1111
console.info(`${new Date().toISOString()} [${tag}]: ${message}`, ...args);
1212
},
13-
warn(message, ...args: any[]) {
13+
warn(message: any, ...args: any[]) {
1414
// eslint-disable-next-line no-console
1515
console.warn(`${new Date().toISOString()} [${tag}]: ${message}`, ...args);
1616
},
17-
error(message, ...args: any[]) {
17+
error(message: any, ...args: any[]) {
1818
// eslint-disable-next-line no-console
1919
console.error(`${new Date().toISOString()} [${tag}]: ${message}`, ...args);
2020
},

packages/sdk/browser/package.json

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,38 @@
1616
"feature management",
1717
"sdk"
1818
],
19+
"type": "module",
20+
"main": "./dist/index.cjs",
21+
"module": "./dist/index.js",
22+
"types": "./dist/index.d.ts",
1923
"exports": {
2024
".": {
21-
"types": "./dist/src/index.d.ts",
22-
"require": "./dist/index.cjs.js",
23-
"import": "./dist/index.es.js"
25+
"require": {
26+
"types": "./dist/index.d.cts",
27+
"require": "./dist/index.cjs"
28+
},
29+
"import": {
30+
"types": "./dist/index.d.ts",
31+
"import": "./dist/index.js"
32+
}
2433
},
2534
"./compat": {
26-
"types": "./dist/src/compat/index.d.ts",
27-
"require": "./dist/compat.cjs.js",
28-
"import": "./dist/compat.es.js"
35+
"require": {
36+
"types": "./dist/compat.d.cts",
37+
"require": "./dist/compat.cjs"
38+
},
39+
"import": {
40+
"types": "./dist/compat.d.ts",
41+
"import": "./dist/compat.js"
42+
}
2943
}
3044
},
31-
"type": "module",
3245
"files": [
3346
"dist"
3447
],
3548
"scripts": {
3649
"clean": "rimraf dist",
37-
"build": "tsc --noEmit && rollup -c rollup.config.js",
50+
"build": "tsup",
3851
"lint": "eslint . --ext .ts,.tsx",
3952
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
4053
"test": "npx jest --runInBand",
@@ -46,11 +59,6 @@
4659
},
4760
"devDependencies": {
4861
"@jest/globals": "^29.7.0",
49-
"@rollup/plugin-commonjs": "^25.0.0",
50-
"@rollup/plugin-json": "^6.1.0",
51-
"@rollup/plugin-node-resolve": "^15.0.2",
52-
"@rollup/plugin-terser": "^0.4.3",
53-
"@rollup/plugin-typescript": "^11.1.1",
5462
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
5563
"@types/jest": "^29.5.11",
5664
"@typescript-eslint/eslint-plugin": "^6.20.0",
@@ -66,9 +74,8 @@
6674
"jest-environment-jsdom": "^29.7.0",
6775
"prettier": "^3.0.0",
6876
"rimraf": "^5.0.5",
69-
"rollup": "^3.23.0",
70-
"rollup-plugin-visualizer": "^5.12.0",
7177
"ts-jest": "^29.1.1",
78+
"tsup": "^8.3.5",
7279
"typedoc": "0.25.0",
7380
"typescript": "^5.5.3"
7481
}

packages/sdk/browser/rollup.config.js

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

packages/sdk/browser/tsconfig.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,27 @@
88
"moduleResolution": "node",
99
"noImplicitOverride": true,
1010
"resolveJsonModule": true,
11-
// Uses "." so it can load package.json.
1211
"rootDir": ".",
1312
"outDir": "dist",
1413
"skipLibCheck": true,
15-
// enables importers to jump to source
16-
"sourceMap": true,
14+
"sourceMap": false,
1715
"strict": true,
1816
"stripInternal": true,
1917
"target": "ES2017",
2018
"types": ["node", "jest"],
2119
"allowJs": true
2220
},
21+
"include": ["src"],
2322
"exclude": [
2423
"vite.config.ts",
2524
"__tests__",
2625
"dist",
2726
"docs",
2827
"example",
2928
"node_modules",
30-
"contract-tests",
3129
"babel.config.js",
32-
"jestSetupFile.ts",
30+
"setup-jest.js",
31+
"rollup.config.js",
3332
"**/*.test.ts*"
3433
]
3534
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// It is a dev dependency and the linter doesn't understand.
2+
// eslint-disable-next-line import/no-extraneous-dependencies
3+
import { defineConfig } from 'tsup';
4+
5+
export default defineConfig({
6+
entry: {
7+
index: 'src/index.ts',
8+
compat: 'src/compat/index.ts',
9+
},
10+
minify: true,
11+
format: ['esm', 'cjs'],
12+
splitting: false,
13+
sourcemap: false,
14+
clean: true,
15+
noExternal: ['@launchdarkly/js-sdk-common', '@launchdarkly/js-client-sdk-common'],
16+
dts: true,
17+
metafile: true,
18+
esbuildOptions(opts) {
19+
// This would normally be `^_(?!meta|_)`, but go doesn't support negative look-ahead assertions,
20+
// so we need to craft something that works without it.
21+
// So start of line followed by a character that isn't followed by m or underscore, but we
22+
// want other things that do start with m, so we need to progressively handle more characters
23+
// of meta with exclusions.
24+
// eslint-disable-next-line no-param-reassign
25+
opts.mangleProps = /^_([^m|_]|m[^e]|me[^t]|met[^a])/;
26+
},
27+
});

0 commit comments

Comments
 (0)