From 224a09e9e20c0b8b1ff1de3c224ef84ee2be1f5b Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Thu, 21 Dec 2023 01:15:27 +0330 Subject: [PATCH] feat(nano-build): new package for esbuild --- packages/nano-build/README.md | 3 + packages/nano-build/nano-build.cjs | 90 ++++++++++++++++++++++++++++++ packages/nano-build/package.json | 38 +++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 packages/nano-build/README.md create mode 100755 packages/nano-build/nano-build.cjs create mode 100644 packages/nano-build/package.json diff --git a/packages/nano-build/README.md b/packages/nano-build/README.md new file mode 100644 index 00000000..0038fc8f --- /dev/null +++ b/packages/nano-build/README.md @@ -0,0 +1,3 @@ +# Nano build + +Build/bundle tools for ECMAScript, TypeScript, and JavaScript libraries. It's easy to use, doesn't require any setup, and adheres to best practices. It has no dependencies and uses esbuild for enhanced performance. diff --git a/packages/nano-build/nano-build.cjs b/packages/nano-build/nano-build.cjs new file mode 100755 index 00000000..f896c5ae --- /dev/null +++ b/packages/nano-build/nano-build.cjs @@ -0,0 +1,90 @@ +const {context, build} = require('esbuild'); +const {resolve} = require('path'); +const packageJsonPath = resolve(process.cwd(), 'package.json'); +const packageJson = require(packageJsonPath); + +console.log('🚀 nano-build'); +console.log('📦 ' + packageJson.name); + +const watchMode = process.argv.includes('--watch'); + +const devMode = process.env.NODE_ENV !== 'production'; + +(async () => { + /** + * @type {import('esbuild').BuildOptions} + */ + const esbuildOptions = { + entryPoints: ['src/main.ts'], + outdir: 'dist', + logLevel: 'info', + platform: 'node', + target: 'es2020', + format: 'esm', + cjs: true, + minify: true, + mangleProps: '_$', + // treeShaking: true, + sourcemap: true, + sourcesContent: true, + bundle: true, + packages: 'external', + splitting: false, + charset: 'utf8', + legalComments: 'none', + banner: { + js: '/* ' + packageJson.name + ' v' + packageJson.version + ' */', + }, + define: { + __package_version: `'${packageJson.version}'`, + }, + ...packageJson['nano-build'], + ...packageJson['nano-build-' + (devMode ? 'development' : 'production')], + }; + + const alsoCjs = esbuildOptions.format === 'esm' && esbuildOptions.cjs; + delete esbuildOptions.cjs; + + if(alsoCjs) { + esbuildOptions.outExtension = { + ...esbuildOptions.outExtension, + '.js': '.mjs', + }; + } + + // Remove null fields from esbuildOptions + Object.keys(esbuildOptions).forEach((key) => { + if (esbuildOptions[key] === null) { + delete esbuildOptions[key]; + } + }); + + if (esbuildOptions.outdir !== undefined) { + delete esbuildOptions.outfile; + } + + console.log('esbuildOptions: %o', esbuildOptions); + + esbuildOptions.mangleProps = new RegExp(esbuildOptions.mangleProps); + + if (watchMode) { + console.log('👀 Watching...'); + const esbuildContext = await context(esbuildOptions); + esbuildContext.watch(); + return; + } + + // else + console.log('🛠️ Building...'); + await build(esbuildOptions); + if (alsoCjs) { + await build({ + ...esbuildOptions, + format: 'cjs', + outExtension: { + ...esbuildOptions.outExtension, + '.js': '.cjs', + }, + }); + } +})(); diff --git a/packages/nano-build/package.json b/packages/nano-build/package.json new file mode 100644 index 00000000..06a45d37 --- /dev/null +++ b/packages/nano-build/package.json @@ -0,0 +1,38 @@ +{ + "name": "@alwatr/nano-build", + "version": "1.0.0", + "description": "Build/bundle tools for ECMAScript, TypeScript, and JavaScript libraries. It's easy to use, doesn't require any setup, and adheres to best practices. It has no dependencies and uses esbuild for enhanced performance.", + "author": "S. Ali Mihandoost ", + "keywords": [ + "build", + "bundle", + "esbuild", + "typescript", + "esm", + "alwatr" + ], + "bin": "./nano-build.cjs", + "license": "MIT", + "files": [ + "**/*.{d.ts.map,d.ts,js.map,js,html,md}" + ], + "publishConfig": { + "access": "public" + }, + "repository": { + "type": "git", + "url": "https://github.com/Alwatr/nanolib", + "directory": "packages/nano-build" + }, + "homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/nano-build#readme", + "bugs": { + "url": "https://github.com/Alwatr/nanolib/issues" + }, + "devDependencies": { + "@alwatr/tsconfig-base": "workspace:^", + "typescript": "^5.3.3" + }, + "dependencies": { + "esbuild": "^0.19.10" + } +}