Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Refine CJS/ESM build configuration for browser SDK. #640

Merged
merged 16 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the bundle-size task I will add some conditional configuration to a dev build and source maps.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The esbuild support actually means it can generate a meta data file, which can be used for the analysis instead. So no debug builds for now.

"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'],
Copy link
Member Author

@kinyoklion kinyoklion Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the one thing that wasn't great about tsup is that it doesn't bundle dependencies unless they are dev dependencies. Which sounds fine, but then you need to figure out how to handle the typescript for those packages.

It has a method to bundle the typescript into .d.ts files as well, but that very much didn't work.

So this instructs it to not treat these as external. It does mean that you get these installed for a production build even though they are not then used. But that has been the precedent.

If the bundling of the typing worked, then that would be a reasonable alternative.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface works by modifying the input options, which means re-assigning.

opts.mangleProps = /^_([^m|_]|m[^e]|me[^t]|met[^a])/;
},
});