From f01fff5a25b27c482837a43d46fd51ecca38b75b Mon Sep 17 00:00:00 2001 From: Pedro Borges Date: Thu, 26 Sep 2024 10:14:08 -0300 Subject: [PATCH] Upgrade build script --- packages/core/build.js | 60 +++++++++++++++++++++++++++-------------- packages/react/build.js | 60 +++++++++++++++++++++++++++-------------- packages/vue2/build.js | 60 +++++++++++++++++++++++++++-------------- packages/vue3/build.js | 60 +++++++++++++++++++++++++++-------------- 4 files changed, 160 insertions(+), 80 deletions(-) diff --git a/packages/core/build.js b/packages/core/build.js index 11e7e1d58..074ec405b 100755 --- a/packages/core/build.js +++ b/packages/core/build.js @@ -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') @@ -9,7 +9,10 @@ const config = { minify: true, sourcemap: true, target: 'es2020', - plugins: [nodeExternalsPlugin()], + plugins: [ + nodeExternalsPlugin(), + rebuildLogger(), + ], } const builds = [ @@ -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})…`) + }) + }, + } +} \ No newline at end of file diff --git a/packages/react/build.js b/packages/react/build.js index 11e7e1d58..074ec405b 100755 --- a/packages/react/build.js +++ b/packages/react/build.js @@ -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') @@ -9,7 +9,10 @@ const config = { minify: true, sourcemap: true, target: 'es2020', - plugins: [nodeExternalsPlugin()], + plugins: [ + nodeExternalsPlugin(), + rebuildLogger(), + ], } const builds = [ @@ -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})…`) + }) + }, + } +} \ No newline at end of file diff --git a/packages/vue2/build.js b/packages/vue2/build.js index 11e7e1d58..074ec405b 100755 --- a/packages/vue2/build.js +++ b/packages/vue2/build.js @@ -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') @@ -9,7 +9,10 @@ const config = { minify: true, sourcemap: true, target: 'es2020', - plugins: [nodeExternalsPlugin()], + plugins: [ + nodeExternalsPlugin(), + rebuildLogger(), + ], } const builds = [ @@ -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})…`) + }) + }, + } +} \ No newline at end of file diff --git a/packages/vue3/build.js b/packages/vue3/build.js index 11e7e1d58..074ec405b 100755 --- a/packages/vue3/build.js +++ b/packages/vue3/build.js @@ -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') @@ -9,7 +9,10 @@ const config = { minify: true, sourcemap: true, target: 'es2020', - plugins: [nodeExternalsPlugin()], + plugins: [ + nodeExternalsPlugin(), + rebuildLogger(), + ], } const builds = [ @@ -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})…`) + }) + }, + } +} \ No newline at end of file