Skip to content
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
95 changes: 59 additions & 36 deletions esbuild.config.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 6 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"lib": ["esnext", "dom"],
"esModuleInterop": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"sourceMap": false,
"declaration": true,
"skipLibCheck": true,
"strict": true
"strict": true,
"emitDeclarationOnly": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/__tests__"]
}