Skip to content

Commit

Permalink
feat: Refine CJS/ESM build configuration for browser SDK. (#640)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kinyoklion authored Oct 29, 2024
1 parent 44a2237 commit ec4377c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 80 deletions.
8 changes: 4 additions & 4 deletions packages/sdk/browser/contract-tests/entity/src/makeLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { LDLogger } from '@launchdarkly/js-client-sdk';

export function makeLogger(tag: string): LDLogger {
return {
debug(message, ...args: any[]) {
debug(message: any, ...args: any[]) {
// eslint-disable-next-line no-console
console.debug(`${new Date().toISOString()} [${tag}]: ${message}`, ...args);
},
info(message, ...args: any[]) {
info(message: any, ...args: any[]) {
// eslint-disable-next-line no-console
console.info(`${new Date().toISOString()} [${tag}]: ${message}`, ...args);
},
warn(message, ...args: any[]) {
warn(message: any, ...args: any[]) {
// eslint-disable-next-line no-console
console.warn(`${new Date().toISOString()} [${tag}]: ${message}`, ...args);
},
error(message, ...args: any[]) {
error(message: any, ...args: any[]) {
// eslint-disable-next-line no-console
console.error(`${new Date().toISOString()} [${tag}]: ${message}`, ...args);
},
Expand Down
37 changes: 22 additions & 15 deletions packages/sdk/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,38 @@
"feature management",
"sdk"
],
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"require": "./dist/index.cjs.js",
"import": "./dist/index.es.js"
"require": {
"types": "./dist/index.d.cts",
"require": "./dist/index.cjs"
},
"import": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"./compat": {
"types": "./dist/src/compat/index.d.ts",
"require": "./dist/compat.cjs.js",
"import": "./dist/compat.es.js"
"require": {
"types": "./dist/compat.d.cts",
"require": "./dist/compat.cjs"
},
"import": {
"types": "./dist/compat.d.ts",
"import": "./dist/compat.js"
}
}
},
"type": "module",
"files": [
"dist"
],
"scripts": {
"clean": "rimraf dist",
"build": "tsc --noEmit && rollup -c rollup.config.js",
"build": "tsup",
"lint": "eslint . --ext .ts,.tsx",
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
"test": "npx jest --runInBand",
Expand All @@ -46,11 +59,6 @@
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.1",
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@types/jest": "^29.5.11",
"@typescript-eslint/eslint-plugin": "^6.20.0",
Expand All @@ -66,9 +74,8 @@
"jest-environment-jsdom": "^29.7.0",
"prettier": "^3.0.0",
"rimraf": "^5.0.5",
"rollup": "^3.23.0",
"rollup-plugin-visualizer": "^5.12.0",
"ts-jest": "^29.1.1",
"tsup": "^8.3.5",
"typedoc": "0.25.0",
"typescript": "^5.5.3"
}
Expand Down
56 changes: 0 additions & 56 deletions packages/sdk/browser/rollup.config.js

This file was deleted.

9 changes: 4 additions & 5 deletions packages/sdk/browser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@
"moduleResolution": "node",
"noImplicitOverride": true,
"resolveJsonModule": true,
// Uses "." so it can load package.json.
"rootDir": ".",
"outDir": "dist",
"skipLibCheck": true,
// enables importers to jump to source
"sourceMap": true,
"sourceMap": false,
"strict": true,
"stripInternal": true,
"target": "ES2017",
"types": ["node", "jest"],
"allowJs": true
},
"include": ["src"],
"exclude": [
"vite.config.ts",
"__tests__",
"dist",
"docs",
"example",
"node_modules",
"contract-tests",
"babel.config.js",
"jestSetupFile.ts",
"setup-jest.js",
"rollup.config.js",
"**/*.test.ts*"
]
}
27 changes: 27 additions & 0 deletions packages/sdk/browser/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// It is a dev dependency and the linter doesn't understand.
// eslint-disable-next-line import/no-extraneous-dependencies
import { defineConfig } from 'tsup';

export default defineConfig({
entry: {
index: 'src/index.ts',
compat: 'src/compat/index.ts',
},
minify: true,
format: ['esm', 'cjs'],
splitting: false,
sourcemap: false,
clean: true,
noExternal: ['@launchdarkly/js-sdk-common', '@launchdarkly/js-client-sdk-common'],
dts: true,
metafile: true,
esbuildOptions(opts) {
// This would normally be `^_(?!meta|_)`, but go doesn't support negative look-ahead assertions,
// so we need to craft something that works without it.
// So start of line followed by a character that isn't followed by m or underscore, but we
// want other things that do start with m, so we need to progressively handle more characters
// of meta with exclusions.
// eslint-disable-next-line no-param-reassign
opts.mangleProps = /^_([^m|_]|m[^e]|me[^t]|met[^a])/;
},
});

0 comments on commit ec4377c

Please sign in to comment.