/**
 * build.js
 * @ndaidong
**/

import { readFileSync, writeFileSync } from 'fs'
import { execSync } from 'child_process'

import { buildSync } from 'esbuild'

const pkg = JSON.parse(readFileSync('./package.json'))

execSync('rm -rf dist')
execSync('mkdir dist')

const buildTime = (new Date()).toISOString()
const comment = [
  `// ${pkg.name}@${pkg.version}, by ${pkg.author}`,
  `built with esbuild at ${buildTime}`,
  `published under ${pkg.license} license`
].join(' - ')

const baseOpt = {
  entryPoints: ['src/main.js'],
  bundle: true,
  charset: 'utf8',
  target: ['es2020', 'node14'],
  minify: false,
  write: true
}

const cjsVersion = {
  ...baseOpt,
  platform: 'node',
  format: 'cjs',
  mainFields: ['main'],
  outfile: `dist/cjs/${pkg.name}.js`,
  banner: {
    js: comment
  }
}
buildSync(cjsVersion)

const cjspkg = {
  name: pkg.name + '-cjs',
  version: pkg.version,
  main: `./${pkg.name}.js`
}
writeFileSync(
  'dist/cjs/package.json',
  JSON.stringify(cjspkg, null, '  '),
  'utf8'
)

@@ -26,13 +26,13 @@
 ong%0A
+ 
 **/%0A
-%0A
 impo
@@ -67,34 +67,23 @@
 Sync
- %7D from 'fs'%0Aimport %7B exec
+, rmSync, mkdir
 Sync
@@ -95,20 +95,9 @@
 om '
-child_proces
+f
 s'%0A%0A
@@ -189,52 +189,99 @@
 son'
-))%0A%0AexecSync('rm -rf dist')%0AexecSync('mkdir 
+, %7B encoding: 'utf-8' %7D))%0A%0ArmSync('dist', %7B%0A  force: true,%0A  recursive: true%0A%7D)%0AmkdirSync('
 dist
@@ -494,24 +494,72 @@
 oin(' - ')%0A%0A
+/**%0A * @type import(%E2%80%99esbuild%E2%80%98).BuildOptions%0A */%0A
 const baseOp
@@ -697,16 +697,66 @@
 true%0A%7D%0A%0A
+/**%0A * @type %7Bimport('esbuild').BuildOptions%7D%0A */%0A
 const cj
@@ -912,16 +912,64 @@
 ment%0A  %7D
+,%0A  external: %5B'*.node', './xhr-sync-worker.js'%5D
 %0A%7D%0Abuild

mark `*.node` and `./xhr-sync-worker.js` as external... use `fs` for file operate add type for config