-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 💡 update esbuild config to use new esbuild API
- Loading branch information
Showing
1 changed file
with
64 additions
and
76 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 |
---|---|---|
@@ -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(); |