Skip to content

Commit

Permalink
chore: 🤖 update build script to use es module
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Jul 2, 2023
1 parent 721fd6e commit 6dc0836
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions esbuild.ts → esbuild.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
#!/usr/bin/env node

const esbuild = require('esbuild');
import esbuild from 'esbuild';

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

const build = async () => {
const bundleCtx = await esbuild.context({
entryPoints: ['./src/main.ts'],
outfile: 'dist/bundle.js',
outfile: process.env.ESM_BUILD ? 'dist/bundle.js' : 'dist/bundle.cjs',
bundle: true,
platform: 'node',
sourcemap: true,
target: 'node12',
format: process.env.ESM_BUILD ? 'esm' : 'cjs',
minify: true,
banner: {
js: process.env.ESM_BUILD
? `
import path from 'path';
import { fileURLToPath } from 'url';
import { createRequire as topLevelCreateRequire } from 'module';
const require = topLevelCreateRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
`
: ``,
},
plugins: [
nodeExternalsPlugin(),
{
Expand All @@ -36,12 +49,25 @@ const build = async () => {

const cliCtx = await esbuild.context({
entryPoints: ['./src/cli.ts'],
outfile: 'dist/cli-bundle.js',
outfile: process.env.ESM_BUILD ? 'dist/cli-bundle.js' : 'dist/cli-bundle.cjs',
bundle: true,
platform: 'node',
sourcemap: true,
target: 'node12',
format: process.env.ESM_BUILD ? 'esm' : 'cjs',
minify: true,
banner: {
js: process.env.ESM_BUILD
? `
import path from 'path';
import { fileURLToPath } from 'url';
import { createRequire as topLevelCreateRequire } from 'module';
const require = topLevelCreateRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
`
: ``,
},
plugins: [
nodeExternalsPlugin(),
{
Expand Down

0 comments on commit 6dc0836

Please sign in to comment.