Skip to content

Commit

Permalink
refactor: 💡 update esbuild config to use new esbuild API
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Jun 19, 2023
1 parent e10c2d9 commit a0c267b
Showing 1 changed file with 64 additions and 76 deletions.
140 changes: 64 additions & 76 deletions esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,76 @@
#!/usr/bin/env node

const esbuild = require("esbuild");
const esbuild = require('esbuild');

// Automatically exclude all node_modules from the bundled version
const { nodeExternalsPlugin } = require("esbuild-node-externals");
const { nodeExternalsPlugin } = require('esbuild-node-externals');

if (process.env.NODE_ENV === "production") {
esbuild
.build({
entryPoints: ["./src/main.ts"],
outfile: "dist/bundle.js",
bundle: true,
minify: true,
platform: "node",
sourcemap: true,
target: "node12",
plugins: [nodeExternalsPlugin()],
})
.then(() => {
console.log("===========================================");
console.log(`${new Date().toLocaleString()}: main module build succeeded.`);
})
.catch(() => process.exit(1));

esbuild
.build({
entryPoints: ["./src/cli.ts"],
outfile: "dist/cli-bundle.js",
bundle: true,
minify: true,
platform: "node",
sourcemap: true,
target: "node12",
plugins: [nodeExternalsPlugin()],
})
.then(() => {
console.log("===========================================");
console.log(`${new Date().toLocaleString()}: cli module build succeeded.`);
})
.catch(() => process.exit(1));

return;
}

esbuild
.build({
entryPoints: ["./src/main.ts"],
outfile: "dist/bundle.js",
const build = async () => {
const bundleCtx = await esbuild.context({
entryPoints: ['./src/main.ts'],
outfile: 'dist/bundle.js',
bundle: true,
platform: "node",
platform: 'node',
sourcemap: true,
target: "node12",
plugins: [nodeExternalsPlugin()],
watch: {
onRebuild(error, result) {
if (error) console.error("watch build failed:", error);
else console.log("watch build succeeded:", result);
target: 'node12',
minify: true,
plugins: [
nodeExternalsPlugin(),
{
name: 'watch',
setup(build) {
build.onEnd((result) => {
console.log('===========================================');
// error if errors array have errors
if (result.errors.length > 0) {
console.log(`${new Date().toLocaleString()}: main module build failed.`);
process.exit(1);
} else {
console.log(`${new Date().toLocaleString()}: main module build succeeded.`);
}
});
},
},
},
})
.then(() => {
console.log("===========================================");
console.log(`${new Date().toLocaleString()}: watching main build...`);
})
.catch(() => process.exit(1));
],
});

esbuild
.build({
entryPoints: ["./src/cli.ts"],
outfile: "dist/cli-bundle.js",
const cliCtx = await esbuild.context({
entryPoints: ['./src/cli.ts'],
outfile: 'dist/cli-bundle.js',
bundle: true,
platform: "node",
platform: 'node',
sourcemap: true,
target: "node12",
plugins: [nodeExternalsPlugin()],
watch: {
onRebuild(error, result) {
if (error) console.error("watch build failed:", error);
else console.log("watch build succeeded:", result);
target: 'node12',
minify: true,
plugins: [
nodeExternalsPlugin(),
{
name: 'watch',
setup(build) {
build.onEnd((result) => {
console.log('===========================================');
// error if errors array have errors
if (result.errors.length > 0) {
console.log(`${new Date().toLocaleString()}: cli module build failed.`);
process.exit(1);
} else {
console.log(`${new Date().toLocaleString()}: cli module build succeeded.`);
}
});
},
},
},
})
.then(() => {
console.log("===========================================");
console.log(`${new Date().toLocaleString()}: watching cli build...`);
})
.catch(() => process.exit(1));
],
});

if (process.env.NODE_ENV === 'production') {
await bundleCtx.rebuild();
bundleCtx.dispose();
await cliCtx.rebuild();
cliCtx.dispose();
} else {
await bundleCtx.watch();
await cliCtx.watch();
}
};

build();

0 comments on commit a0c267b

Please sign in to comment.