Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Last active February 5, 2022 09:31
Show Gist options
  • Save steveruizok/ba9c1807785792016be9de14484d9c9c to your computer and use it in GitHub Desktop.
Save steveruizok/ba9c1807785792016be9de14484d9c9c to your computer and use it in GitHub Desktop.
esbuild scripts for building packages
/* 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()
/* 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()
/* 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