Last active
February 5, 2022 09:31
-
-
Save steveruizok/ba9c1807785792016be9de14484d9c9c to your computer and use it in GitHub Desktop.
esbuild scripts for building packages
This file contains hidden or 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
/* eslint-disable */ | |
const fs = require('fs') | |
const path = require('path') | |
const esbuild = require('esbuild') | |
const { gzip } = require('zlib') | |
const { log } = console | |
const cwd = process.cwd() | |
const pkg = require(path.join(cwd, 'package.json')) | |
async function main() { | |
if (fs.existsSync(path.join(cwd, 'dist'))) { | |
fs.rmSync(path.join(cwd, 'dist'), { recursive: true }, (e) => { | |
if (e) { | |
throw e | |
} | |
}) | |
} | |
const deps = [...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.peerDependencies ?? {})] | |
try { | |
esbuild.buildSync({ | |
entryPoints: [path.join(cwd, 'src/index.ts')], | |
outdir: 'dist', | |
minify: false, | |
bundle: true, | |
format: 'cjs', | |
target: 'es6', | |
treeShaking: true, | |
tsconfig: path.join(cwd, 'tsconfig.build.json'), | |
external: deps, | |
metafile: true, | |
sourcemap: true, | |
define: { | |
'process.env.NODE_ENV': '"production"', | |
}, | |
}) | |
const esmResult = esbuild.buildSync({ | |
entryPoints: [path.join(cwd, 'src/index.ts')], | |
outdir: 'dist', | |
outExtension: { '.js': '.mjs' }, | |
minify: false, | |
bundle: true, | |
format: 'esm', | |
target: 'es6', | |
treeShaking: true, | |
tsconfig: path.join(cwd, 'tsconfig.build.json'), | |
external: deps, | |
metafile: true, | |
sourcemap: true, | |
define: { | |
'process.env.NODE_ENV': '"production"', | |
}, | |
}) | |
const esmSize = Object.values(esmResult.metafile.outputs).reduce( | |
(acc, { bytes }) => acc + bytes, | |
0 | |
) | |
fs.readFile('./dist/index.mjs', (_err, data) => { | |
gzip(data, (_err, result) => { | |
log( | |
`✔ ${pkg.name}: Build succeeded. ${(esmSize / 1000).toFixed(1)}kb / ${( | |
result.length / 1000 | |
).toFixed(1)}kb gzipped` | |
) | |
}) | |
}) | |
} catch (e) { | |
log(`x ${pkg.name}: Build failed due to an error.`) | |
log(e) | |
} | |
} | |
main() |
This file contains hidden or 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
/* eslint-disable */ | |
const esbuild = require('esbuild') | |
const fs = require('fs') | |
const path = require('path') | |
const { log } = console | |
const cwd = process.cwd() | |
const pkg = require(path.join(cwd, 'package.json')) | |
async function main() { | |
if (fs.existsSync(path.join(cwd, 'dist'))) { | |
fs.rmSync(path.join(cwd, 'dist'), { recursive: true }, (e) => { | |
if (e) { | |
throw e | |
} | |
}) | |
} | |
const deps = [...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.peerDependencies ?? {})] | |
try { | |
await esbuild.build({ | |
entryPoints: ['src/index.ts'], | |
outfile: 'dist/index.js', | |
bundle: true, | |
minify: false, | |
sourcemap: true, | |
incremental: true, | |
platform: 'node', | |
format: 'esm', | |
target: 'es6', | |
external: deps, | |
define: { | |
'process.env.NODE_ENV': '"development"', | |
}, | |
watch: { | |
onRebuild(err) { | |
err ? error('❌ Failed') : log('✅ Updated') | |
}, | |
}, | |
}) | |
} catch (err) { | |
process.exit(1) | |
} | |
} | |
main() |
This file contains hidden or 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
/* eslint-disable */ | |
const esbuild = require('esbuild') | |
const fs = require('fs') | |
const path = require('path') | |
const { log } = console | |
const cwd = process.cwd() | |
const pkg = require(path.join(cwd, 'package.json')) | |
async function main() { | |
if (fs.existsSync(path.join(cwd, 'dist'))) { | |
fs.rmSync(path.join(cwd, 'dist'), { recursive: true }, (e) => { | |
if (e) { | |
throw e | |
} | |
}) | |
} | |
const deps = [...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.peerDependencies ?? {})] | |
try { | |
await esbuild.build({ | |
entryPoints: [path.join(cwd, 'src/index.ts')], | |
outdir: 'dist', | |
outExtension: { '.js': '.mjs' }, | |
minify: false, | |
bundle: true, | |
format: 'esm', | |
target: 'es6', | |
treeShaking: true, | |
tsconfig: path.join(cwd, 'tsconfig.json'), | |
external: deps, | |
metafile: true, | |
sourcemap: true, | |
incremental: true, | |
define: { | |
'process.env.NODE_ENV': '"development"', | |
}, | |
watch: { | |
onRebuild(err) { | |
err ? error('❌ Failed') : log('✅ Updated') | |
}, | |
}, | |
}) | |
} catch (err) { | |
process.exit(1) | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment