Skip to content

Commit

Permalink
build: add building with esbuild
Browse files Browse the repository at this point in the history
Signed-off-by: euberdeveloper <euberdeveloper@gmail.com>
  • Loading branch information
euberdeveloper committed Jun 11, 2024
1 parent bcbb6aa commit d1071f1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 96 deletions.
62 changes: 62 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import fs from 'node:fs';
import { build } from 'esbuild';
import * as importMap from "esbuild-plugin-import-map";

import packageJson from './package.json' assert { type: 'json' };

importMap.load({
imports: {
'./index': '../lib/esm/index.esm.js'
}
});

function getExternalDependencies(allow = []) {
const deps = packageJson.dependencies ? Object.keys(packageJson.dependencies).filter(dep => !allow.includes(dep)) : [];
const peerDeps = packageJson.peerDependencies ? Object.keys(packageJson.peerDependencies).filter(dep => !allow.includes(dep)) : [];
return [...deps, ...peerDeps];
}

async function buildModule() {
const shared = {
platform: 'node',
entryPoints: ['source/lib/index.ts'],
bundle: true,
minify: true,
treeShaking: true,
sourcemap: true
};

await build({
...shared,
outfile: 'bundled/lib/commonjs/index.js',
format: 'cjs',
external: getExternalDependencies()
});

await build({
...shared,
outfile: 'bundled/lib/esm/index.esm.js',
format: 'esm',
external: getExternalDependencies()
});

await build({
...shared,
entryPoints: ['source/bin/index.ts'],
outfile: 'bundled/bin/index.js',
format: 'esm',
external: getExternalDependencies(),
plugins: [importMap.plugin()],
define: {
'__VERSION__': `"${packageJson.version}"`
}
});
}

function generateCommonjsPackageJson() {
const packageJsonCommonJs = JSON.stringify({ ...packageJson, type: undefined }, null, 2);
fs.writeFileSync('./bundled/lib/commonjs/package.json', packageJsonCommonJs);
}

await buildModule();
generateCommonjsPackageJson();
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"pretranspile": "shx rm -rf dist",
"transpile": "tsc -p source",
"prebundle": "shx rm -rf dist bundled",
"bundle": "webpack",
"bundle:esm": "node build.mjs",
"bundle:dts": "dts-bundle-generator -o bundled/lib/commonjs/index.d.ts --project source/tsconfig.json source/lib/index.ts",
"bundle": "pnpm bundle:esm && pnpm bundle:dts",
"postbundle:dts": "cp bundled/lib/commonjs/index.d.ts bundled/lib/esm/index.d.ts",
"script:getr:linux": "node scripts/generate-expected-tests-results linux",
"script:getr:mac": "node scripts/generate-expected-tests-results mac",
"script:getr:windows": "node scripts/generate-expected-tests-results windows",
Expand Down
95 changes: 0 additions & 95 deletions webpack.config.js

This file was deleted.

0 comments on commit d1071f1

Please sign in to comment.