Skip to content

Commit

Permalink
feat(nano-build): new package for esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Dec 20, 2023
1 parent a865d0a commit 224a09e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/nano-build/README.md
Original file line number Diff line number Diff line change
@@ -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.
90 changes: 90 additions & 0 deletions packages/nano-build/nano-build.cjs
Original file line number Diff line number Diff line change
@@ -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',
},
});
}
})();
38 changes: 38 additions & 0 deletions packages/nano-build/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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"
}
}

0 comments on commit 224a09e

Please sign in to comment.