Skip to content

Commit

Permalink
Merge pull request #3 from Alwatr/feat/nanobuild
Browse files Browse the repository at this point in the history
feat(nano-build): new package for esbuild
  • Loading branch information
alimd authored Dec 20, 2023
2 parents a865d0a + 0364e51 commit 9b78cbf
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.js
*.mjs
*.cjs
*.d.ts
/demo/es-bench/
/uniquely/flight-finder-pwa
Expand Down
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
"Alwatr",
"flatomise",
"nanolib",
"outdir",
"outfile",
"tsbuildinfo"
]
}
3 changes: 2 additions & 1 deletion packages/flat-string/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
"cb": "yarn run clean && yarn run build",
"d": "yarn run build:es && ALWATR_DEBUG=1 yarn node",
"build": "yarn run build:ts & yarn run build:es",
"build:es": "echo underdevelopment",
"build:es": "nano-build",
"build:ts": "tsc --build",
"watch": "yarn run watch:ts & yarn run watch:es",
"watch:es": "yarn run build:es --watch",
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput",
"clean": "rm -rfv dist .tsbuildinfo"
},
"devDependencies": {
"@alwatr/nano-build": "workspace:^",
"@alwatr/tsconfig-base": "workspace:^",
"typescript": "^5.3.3"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/flat-string/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* flatString(myStr);
* ```
*/
export const flatString = (str: string): string => {
export function flatString (str: string): string {
// @ts-expect-error because it alters wrong compilation errors.
str | 0;
return str;
};
}
2 changes: 1 addition & 1 deletion packages/flat-string/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@alwatr/tsconfig-base",
"extends": "@alwatr/tsconfig-base/tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
Expand Down
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"
}
}
Loading

0 comments on commit 9b78cbf

Please sign in to comment.