diff --git a/esbuild.config.js b/esbuild.config.js index c712091..ae8bf84 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.json b/package.json index f090efc..b1e4105 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 6c3f6d1..9969e82 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,8 +12,9 @@ "sourceMap": false, "declaration": true, "skipLibCheck": true, - "strict": true + "strict": true, + "emitDeclarationOnly": true }, "include": ["src/**/*"], - "exclude": ["node_modules"] + "exclude": ["node_modules", "dist", "**/*.test.ts", "**/__tests__"] }