-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nano-build): new package for esbuild
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |