Skip to content

Commit

Permalink
Upgrade build script
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroborges committed Sep 26, 2024
1 parent 9549871 commit f01fff5
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 80 deletions.
60 changes: 40 additions & 20 deletions packages/core/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import esbuild from 'esbuild'
import * as esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'

const watch = process.argv.slice(1).includes('--watch')
Expand All @@ -9,7 +9,10 @@ const config = {
minify: true,
sourcemap: true,
target: 'es2020',
plugins: [nodeExternalsPlugin()],
plugins: [
nodeExternalsPlugin(),
rebuildLogger(),
],
}

const builds = [
Expand All @@ -19,22 +22,39 @@ const builds = [
{ entryPoints: ['src/server.ts'], format: 'cjs', outfile: 'dist/server.js', platform: 'node' },
]

builds.forEach((build) => {
esbuild
.build({ ...config, ...build, ...watcher(build) })
.then(() => console.log(`${watch ? 'Watching' : 'Built'} ${build.entryPoints} (${build.format})…`))
.catch(() => process.exit(1))
})

function watcher(build) {
return watch
? {
watch: {
onRebuild: (error) =>
error
? console.error('Watch failed:', error)
: console.log(`Rebuilding ${build.entryPoints} (${build.format})…`),
},
}
: {}
try {
const buildContexts = await Promise.all(builds.map(build => esbuild.context({ ...config, ...build })))

await Promise.all(buildContexts.map(async (ctx, index) => {
if (watch) {
await ctx.watch()
} else {
await ctx.rebuild()
await ctx.dispose()
}

console.log(`${watch ? 'Watching' : 'Built'} ${builds[index].entryPoints} (${builds[index].format})...`)
}))

watch && console.log('Watching for changes. Press Ctrl+C to exit.')
} catch (error) {
process.exit(1)
}

function rebuildLogger() {
return {
name: 'rebuild-logger',
setup(build) {
let ignoreFirstRun = true

build.onEnd(() => {
if (ignoreFirstRun) {
ignoreFirstRun = false
return
}

console.log(`Rebuilt ${build.initialOptions.entryPoints} (${build.initialOptions.format})…`)
})
},
}
}
60 changes: 40 additions & 20 deletions packages/react/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import esbuild from 'esbuild'
import * as esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'

const watch = process.argv.slice(1).includes('--watch')
Expand All @@ -9,7 +9,10 @@ const config = {
minify: true,
sourcemap: true,
target: 'es2020',
plugins: [nodeExternalsPlugin()],
plugins: [
nodeExternalsPlugin(),
rebuildLogger(),
],
}

const builds = [
Expand All @@ -19,22 +22,39 @@ const builds = [
{ entryPoints: ['src/server.ts'], format: 'cjs', outfile: 'dist/server.js', platform: 'node' },
]

builds.forEach((build) => {
esbuild
.build({ ...config, ...build, ...watcher(build) })
.then(() => console.log(`${watch ? 'Watching' : 'Built'} ${build.entryPoints} (${build.format})…`))
.catch(() => process.exit(1))
})

function watcher(build) {
return watch
? {
watch: {
onRebuild: (error) =>
error
? console.error('Watch failed:', error)
: console.log(`Rebuilding ${build.entryPoints} (${build.format})…`),
},
}
: {}
try {
const buildContexts = await Promise.all(builds.map(build => esbuild.context({ ...config, ...build })))

await Promise.all(buildContexts.map(async (ctx, index) => {
if (watch) {
await ctx.watch()
} else {
await ctx.rebuild()
await ctx.dispose()
}

console.log(`${watch ? 'Watching' : 'Built'} ${builds[index].entryPoints} (${builds[index].format})...`)
}))

watch && console.log('Watching for changes. Press Ctrl+C to exit.')
} catch (error) {
process.exit(1)
}

function rebuildLogger() {
return {
name: 'rebuild-logger',
setup(build) {
let ignoreFirstRun = true

build.onEnd(() => {
if (ignoreFirstRun) {
ignoreFirstRun = false
return
}

console.log(`Rebuilt ${build.initialOptions.entryPoints} (${build.initialOptions.format})…`)
})
},
}
}
60 changes: 40 additions & 20 deletions packages/vue2/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import esbuild from 'esbuild'
import * as esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'

const watch = process.argv.slice(1).includes('--watch')
Expand All @@ -9,7 +9,10 @@ const config = {
minify: true,
sourcemap: true,
target: 'es2020',
plugins: [nodeExternalsPlugin()],
plugins: [
nodeExternalsPlugin(),
rebuildLogger(),
],
}

const builds = [
Expand All @@ -19,22 +22,39 @@ const builds = [
{ entryPoints: ['src/server.ts'], format: 'cjs', outfile: 'dist/server.js', platform: 'node' },
]

builds.forEach((build) => {
esbuild
.build({ ...config, ...build, ...watcher(build) })
.then(() => console.log(`${watch ? 'Watching' : 'Built'} ${build.entryPoints} (${build.format})…`))
.catch(() => process.exit(1))
})

function watcher(build) {
return watch
? {
watch: {
onRebuild: (error) =>
error
? console.error('Watch failed:', error)
: console.log(`Rebuilding ${build.entryPoints} (${build.format})…`),
},
}
: {}
try {
const buildContexts = await Promise.all(builds.map(build => esbuild.context({ ...config, ...build })))

await Promise.all(buildContexts.map(async (ctx, index) => {
if (watch) {
await ctx.watch()
} else {
await ctx.rebuild()
await ctx.dispose()
}

console.log(`${watch ? 'Watching' : 'Built'} ${builds[index].entryPoints} (${builds[index].format})...`)
}))

watch && console.log('Watching for changes. Press Ctrl+C to exit.')
} catch (error) {
process.exit(1)
}

function rebuildLogger() {
return {
name: 'rebuild-logger',
setup(build) {
let ignoreFirstRun = true

build.onEnd(() => {
if (ignoreFirstRun) {
ignoreFirstRun = false
return
}

console.log(`Rebuilt ${build.initialOptions.entryPoints} (${build.initialOptions.format})…`)
})
},
}
}
60 changes: 40 additions & 20 deletions packages/vue3/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import esbuild from 'esbuild'
import * as esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'

const watch = process.argv.slice(1).includes('--watch')
Expand All @@ -9,7 +9,10 @@ const config = {
minify: true,
sourcemap: true,
target: 'es2020',
plugins: [nodeExternalsPlugin()],
plugins: [
nodeExternalsPlugin(),
rebuildLogger(),
],
}

const builds = [
Expand All @@ -19,22 +22,39 @@ const builds = [
{ entryPoints: ['src/server.ts'], format: 'cjs', outfile: 'dist/server.js', platform: 'node' },
]

builds.forEach((build) => {
esbuild
.build({ ...config, ...build, ...watcher(build) })
.then(() => console.log(`${watch ? 'Watching' : 'Built'} ${build.entryPoints} (${build.format})…`))
.catch(() => process.exit(1))
})

function watcher(build) {
return watch
? {
watch: {
onRebuild: (error) =>
error
? console.error('Watch failed:', error)
: console.log(`Rebuilding ${build.entryPoints} (${build.format})…`),
},
}
: {}
try {
const buildContexts = await Promise.all(builds.map(build => esbuild.context({ ...config, ...build })))

await Promise.all(buildContexts.map(async (ctx, index) => {
if (watch) {
await ctx.watch()
} else {
await ctx.rebuild()
await ctx.dispose()
}

console.log(`${watch ? 'Watching' : 'Built'} ${builds[index].entryPoints} (${builds[index].format})...`)
}))

watch && console.log('Watching for changes. Press Ctrl+C to exit.')
} catch (error) {
process.exit(1)
}

function rebuildLogger() {
return {
name: 'rebuild-logger',
setup(build) {
let ignoreFirstRun = true

build.onEnd(() => {
if (ignoreFirstRun) {
ignoreFirstRun = false
return
}

console.log(`Rebuilt ${build.initialOptions.entryPoints} (${build.initialOptions.format})…`)
})
},
}
}

0 comments on commit f01fff5

Please sign in to comment.