From d9a17c08882e0190eeeaff59ba87f0a7a1090812 Mon Sep 17 00:00:00 2001 From: Niloy Sikdar Date: Fri, 4 Nov 2022 17:21:06 +0530 Subject: [PATCH] feat(build): use esbuild instead of tsc Replace the tsc command with esbuild package to optimize our build process as well as overall package Fix #4 Signed-off-by: Niloy Sikdar --- build.js | 32 ++++++++++++ package.json | 8 +-- postbuild.sh | 18 +------ yarn.lock | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 20 deletions(-) create mode 100644 build.js diff --git a/build.js b/build.js new file mode 100644 index 0000000..06778f8 --- /dev/null +++ b/build.js @@ -0,0 +1,32 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires +const esbuild = require('esbuild'); + +// Build for ESM +esbuild + .build({ + logLevel: 'info', + entryPoints: ['src/index.ts'], + outdir: 'dist/esm', + platform: 'node', + bundle: true, + sourcemap: false, + minify: true, + splitting: true, + format: 'esm', + target: ['ESNext'], + }) + .catch(() => process.exit(1)); + +// Build for CJS +esbuild + .build({ + logLevel: 'info', + entryPoints: ['src/index.ts', 'src/generate.ts'], + outdir: 'dist/cjs', + platform: 'node', + bundle: true, + sourcemap: false, + minify: true, + format: 'cjs', + }) + .catch(() => process.exit(1)); diff --git a/package.json b/package.json index deaaa13..e693b3a 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "license": "Apache-2.0", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", - "types": "dist/esm/index.d.ts", + "types": "dist/types/index.d.ts", "files": [ "dist" ], @@ -27,9 +27,8 @@ "scripts": { "prepare": "husky install", "watch": "tsc --watch", - "build": "rm -rf dist && yarn build:esm && yarn build:cjs", - "build:esm": "tsc -p tsconfig.json", - "build:cjs": "tsc -p tsconfig.json --module commonjs --outDir dist/cjs --target es5", + "ts-types": " tsc --emitDeclarationOnly --outDir dist/types", + "build": "rm -rf dist && yarn ts-types && node build.js", "postbuild": "chmod u+rx ./postbuild.sh && ./postbuild.sh", "release": "semantic-release --branches main", "lint": "eslint src/**", @@ -62,6 +61,7 @@ "@typescript-eslint/eslint-plugin": "^5.37.0", "@typescript-eslint/parser": "^5.37.0", "cz-conventional-changelog": "^3.3.0", + "esbuild": "^0.15.13", "eslint": "^8.23.1", "eslint-config-prettier": "^8.5.0", "eslint-plugin-prettier": "^4.2.1", diff --git a/postbuild.sh b/postbuild.sh index bb93652..6b5f845 100644 --- a/postbuild.sh +++ b/postbuild.sh @@ -3,24 +3,10 @@ # Exit immediately if a command exits with a non-zero status. set -e -# Add type commonjs to the package.json file in the dist/cjs folder -cat >dist/cjs/package.json <dist/esm/package.json <