From 9809738ccc9f454fc869c9906ea445d2f1d370aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carneiro?= Date: Wed, 21 May 2025 10:00:04 +0100 Subject: [PATCH 1/3] Config bundling to work for CJS and ESM --- esbuild.config.js | 95 +++++++++++++++++++++++++++++------------------ package-lock.json | 4 +- package.json | 8 +++- tsconfig.json | 9 +++-- 4 files changed, 72 insertions(+), 44 deletions(-) diff --git a/esbuild.config.js b/esbuild.config.js index c7120916..ae8bf84e 100644 --- a/esbuild.config.js +++ b/esbuild.config.js @@ -1,49 +1,72 @@ import fs from 'fs'; - import esbuild from 'esbuild'; - import packageJson from './package.json' with { type: 'json' }; -const buildOptions = { - entryPoints: ['./src/index.ts'], - bundle: true, - outfile: 'dist/index.js', - platform: 'node', - format: 'cjs', - sourcemap: true, - target: 'node18', - minify: true, - treeShaking: true, - external: [...Object.keys(packageJson.peerDependencies ?? {}), ...Object.keys(packageJson.dependencies ?? {})], - plugins: [ - { - name: 'log-bundle-size', - setup(build) { - build.onEnd(() => { - const filePath = 'dist/index.js'; - const size = fs.statSync(filePath).size; - const sizeInMB = (size / (1024 * 1024)).toFixed(2); - console.log(`\nBundle size: ${sizeInMB} MB (${size} bytes)`); - }); +const externalDeps = [ + ...Object.keys(packageJson.peerDependencies ?? {}), + ...Object.keys(packageJson.dependencies ?? {}), +]; + +const buildFormats = [ + { + format: 'esm', + outfile: 'dist/esm/index.js', + platform: 'node', + }, + { + format: 'cjs', + outfile: 'dist/cjs/index.js', + platform: 'node', + }, +]; + +function buildForFormat({ format, outfile, platform }) { + return esbuild.build({ + entryPoints: ['./src/index.ts'], + bundle: true, + outfile, + platform, + format, + sourcemap: true, + target: 'node18', + minify: true, + treeShaking: true, + external: externalDeps, + metafile: true, + plugins: [ + { + name: 'log-bundle-size', + setup(build) { + build.onEnd(() => { + const size = fs.statSync(outfile).size; + const sizeInMB = (size / (1024 * 1024)).toFixed(2); + console.log(`\n[${format.toUpperCase()}] ${outfile}: ${sizeInMB} MB (${size} bytes)`); + }); + }, }, - }, - ], - metafile: true, -}; + ], + }); +} + +async function buildAll() { + for (const { format, outfile, platform } of buildFormats) { + await buildForFormat({ format, outfile, platform }).then(async (result) => { + // Output analysis to console + console.log(await esbuild.analyzeMetafile(result.metafile)); + }); + } +} if (process.argv.includes('--watch')) { - // Watch mode - esbuild.context(buildOptions).then((context) => { + // Watch mode - only build the first format + buildForFormat(buildFormats[0]).then((context) => { context.watch(); console.log('Watching for changes...'); }); } else { // Build mode with analysis - esbuild - .build(buildOptions) - .then(async (result) => { - // Output analysis to console - console.log(await esbuild.analyzeMetafile(result.metafile)); - }) - .catch(() => process.exit(1)); + buildAll().catch((error) => { + console.error(error); + process.exit(1); + }); } diff --git a/package-lock.json b/package-lock.json index 579deb32..10ea8f2f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@epilot/pricing", - "version": "4.52.0", + "version": "4.52.1-alpha.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@epilot/pricing", - "version": "4.52.0", + "version": "4.52.1-alpha.3", "license": "UNLICENSED", "dependencies": { "dinero.js": "^1.9.1" diff --git a/package.json b/package.json index f090efc7..b1e41059 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,13 @@ { "name": "@epilot/pricing", "version": "4.52.0", - "private": true, "description": "Pricing Library", - "main": "dist/index.js", + "main": "dist/cjs/index.js", + "module": "dist/esm/index.js", + "exports": { + "import": "./dist/esm/index.js", + "require": "./dist/cjs/index.js" + }, "types": "dist/index.d.ts", "repository": { "type": "git", diff --git a/tsconfig.json b/tsconfig.json index 6c3f6d19..2eebc832 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,9 +2,9 @@ "compilerOptions": { "rootDir": "src", "outDir": "dist", - "target": "es2017", - "module": "commonjs", - "moduleResolution": "node", + "target": "ES2020", + "module": "ESNext", + "moduleResolution": "bundler", "lib": ["esnext", "dom"], "esModuleInterop": true, "noImplicitAny": true, @@ -12,7 +12,8 @@ "sourceMap": false, "declaration": true, "skipLibCheck": true, - "strict": true + "strict": true, + "emitDeclarationOnly": true }, "include": ["src/**/*"], "exclude": ["node_modules"] From 03fd5ea092ad1fd1c47a91a413fc0d3617b04fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carneiro?= Date: Wed, 21 May 2025 10:03:24 +0100 Subject: [PATCH 2/3] Revert uninentended changes on package lock --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 10ea8f2f..579deb32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@epilot/pricing", - "version": "4.52.1-alpha.3", + "version": "4.52.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@epilot/pricing", - "version": "4.52.1-alpha.3", + "version": "4.52.0", "license": "UNLICENSED", "dependencies": { "dinero.js": "^1.9.1" From dd557908069434df3d7d5ea29485ec0722a7e37f Mon Sep 17 00:00:00 2001 From: Alexandre Marques Date: Wed, 21 May 2025 10:32:52 +0100 Subject: [PATCH 3/3] chore: update tsconfig.json to exclude test declaration files --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 2eebc832..9969e825 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,5 +16,5 @@ "emitDeclarationOnly": true }, "include": ["src/**/*"], - "exclude": ["node_modules"] + "exclude": ["node_modules", "dist", "**/*.test.ts", "**/__tests__"] }