Skip to content

Commit 0af17f8

Browse files
committed
refactor(build): enhance build system and tooling
- Add conditional source map generation to esbuild config - Improve post-build transform for Node ESM interop - Standardize success messages with logger.success() - Rebuild with source maps before coverage analysis - Migrate build scripts to use @socketsecurity/lib logger - Remove unused get-local-package-aliases utility from registry - Optimize build process for production registry code
1 parent 7d241b2 commit 0af17f8

File tree

5 files changed

+27
-115
lines changed

5 files changed

+27
-115
lines changed

registry/.config/esbuild.config.mjs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import path from 'node:path'
77
import { fileURLToPath } from 'node:url'
88
import fg from 'fast-glob'
99

10-
import { getLocalPackageAliases } from '../scripts/utils/get-local-package-aliases.mjs'
10+
import { envAsBoolean } from '@socketsecurity/lib/env/helpers'
1111

1212
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1313
const rootPath = path.join(__dirname, '..')
@@ -22,38 +22,6 @@ const entryPoints = fg.sync('**/*.{ts,mts,cts}', {
2222
ignore: ['**/*.d.ts', '**/types/**'],
2323
})
2424

25-
/**
26-
* Plugin to handle local package aliases when bundle: false
27-
* esbuild's built-in alias only works with bundle: true, so we need a custom plugin
28-
*/
29-
function createAliasPlugin() {
30-
const aliases = getLocalPackageAliases(rootPath)
31-
32-
// Only create plugin if we have local aliases
33-
if (Object.keys(aliases).length === 0) {
34-
return null
35-
}
36-
37-
return {
38-
name: 'local-package-aliases',
39-
setup(build) {
40-
// Intercept imports for aliased packages
41-
for (const [packageName, aliasPath] of Object.entries(aliases)) {
42-
build.onResolve({ filter: new RegExp(`^${packageName}$`) }, () => {
43-
// Return the path to the local package dist
44-
return { path: aliasPath, external: true }
45-
})
46-
47-
// Handle subpath imports like '@socketsecurity/lib/spinner'
48-
build.onResolve({ filter: new RegExp(`^${packageName}/`) }, args => {
49-
const subpath = args.path.slice(packageName.length + 1)
50-
return { path: path.join(aliasPath, subpath), external: true }
51-
})
52-
}
53-
},
54-
}
55-
}
56-
5725
// Build configuration for CommonJS output
5826
export const buildConfig = {
5927
entryPoints,
@@ -64,7 +32,8 @@ export const buildConfig = {
6432
format: 'cjs',
6533
platform: 'node',
6634
target: 'node18',
67-
sourcemap: true,
35+
// Enable source maps for coverage (set COVERAGE=true env var)
36+
sourcemap: envAsBoolean(process.env.COVERAGE),
6837
// Disable minification for better Node ESM interop
6938
// Node ESM requires clear exports: module.exports = { foo, bar }
7039
minify: false,
@@ -73,9 +42,6 @@ export const buildConfig = {
7342
metafile: true,
7443
logLevel: 'info',
7544

76-
// Use plugin for local package aliases (built-in alias requires bundle: true)
77-
plugins: [createAliasPlugin()].filter(Boolean),
78-
7945
// Note: Cannot use "external" with bundle: false
8046
// esbuild automatically treats all imports as external when not bundling
8147

registry/scripts/build-js.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import { fileURLToPath } from 'node:url'
99
import { build, context } from 'esbuild'
1010
import fg from 'fast-glob'
1111

12-
import { printError, printSuccess } from '../../scripts/utils/cli-helpers.mjs'
12+
import { getDefaultLogger } from '@socketsecurity/lib/logger'
13+
1314
import {
1415
analyzeMetafile,
1516
buildConfig,
1617
watchConfig,
1718
} from '../.config/esbuild.config.mjs'
1819

20+
const logger = getDefaultLogger()
21+
1922
const __dirname = path.dirname(fileURLToPath(import.meta.url))
2023
const rootPath = path.join(__dirname, '..')
2124
const srcPath = path.join(rootPath, 'src')
@@ -112,7 +115,7 @@ async function buildJS() {
112115
return 0
113116
} catch (error) {
114117
if (!isQuiet) {
115-
printError('JavaScript build failed')
118+
logger.error('JavaScript build failed')
116119
console.error(error)
117120
}
118121
return 1
@@ -140,11 +143,11 @@ async function watchJS() {
140143
build.onEnd(result => {
141144
if (result.errors.length > 0) {
142145
if (!isQuiet) {
143-
printError('Rebuild failed')
146+
logger.error('Rebuild failed')
144147
}
145148
} else {
146149
if (!isQuiet) {
147-
printSuccess('Rebuild succeeded')
150+
logger.success('Rebuild succeeded')
148151

149152
if (result?.metafile && isVerbose) {
150153
const analysis = analyzeMetafile(result.metafile)
@@ -173,7 +176,7 @@ async function watchJS() {
173176
await new Promise(() => {})
174177
} catch (error) {
175178
if (!isQuiet) {
176-
printError('Watch mode failed')
179+
logger.error('Watch mode failed')
177180
console.error(error)
178181
}
179182
return 1

registry/scripts/build.mjs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ import path from 'node:path'
77
import { fileURLToPath } from 'node:url'
88

99
import { build, context } from 'esbuild'
10-
import colors from 'yoctocolors-cjs'
1110
import fg from 'fast-glob'
11+
12+
import { isQuiet } from '@socketsecurity/lib/argv/flags'
13+
import { getDefaultLogger } from '@socketsecurity/lib/logger'
14+
import { printFooter, printHeader } from '@socketsecurity/lib/stdio/header'
15+
1216
import {
1317
analyzeMetafile,
1418
buildConfig,
1519
watchConfig,
1620
} from '../.config/esbuild.config.mjs'
17-
import { isQuiet } from '../../scripts/utils/flags.mjs'
18-
import {
19-
logger,
20-
printCompletedHeader,
21-
printFooter,
22-
printHeader,
23-
} from '../../scripts/utils/helpers.mjs'
2421
import { parseArgs } from '../../scripts/utils/parse-args.mjs'
2522
import { runSequence } from '../../scripts/utils/run-command.mjs'
2623

24+
const logger = getDefaultLogger()
25+
2726
const rootPath = path.resolve(
2827
path.dirname(fileURLToPath(import.meta.url)),
2928
'..',
@@ -432,7 +431,7 @@ async function main() {
432431
}
433432

434433
if (!quiet) {
435-
printCompletedHeader('Build Cleaned')
434+
logger.success('Build Cleaned')
436435
}
437436

438437
// Run source and types builds in parallel.
@@ -463,9 +462,9 @@ async function main() {
463462
// Print final status and footer.
464463
if (!quiet) {
465464
if (exitCode === 0) {
466-
console.log(colors.green('✓ Build completed successfully!'))
465+
logger.success('Build completed successfully!')
467466
} else {
468-
console.error(colors.red('✗ Build failed'))
467+
logger.fail('Build failed')
469468
}
470469
printFooter()
471470
}

registry/scripts/post-build-transform.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { readFileSync, writeFileSync } from 'node:fs'
88
import path from 'node:path'
99
import { fileURLToPath } from 'node:url'
1010

11+
import { getDefaultLogger } from '@socketsecurity/lib/logger'
12+
13+
const logger = getDefaultLogger()
14+
1115
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1216
const rootPath = path.join(__dirname, '..')
1317
const indexPath = path.join(rootPath, 'dist', 'index.js')
@@ -41,8 +45,8 @@ try {
4145
// Write back the transformed content
4246
writeFileSync(indexPath, transformedContent, 'utf8')
4347

44-
console.log('✓ Transformed exports for Node ESM interop')
48+
logger.success('Transformed exports for Node ESM interop')
4549
} catch (error) {
46-
console.error('Post-build transform failed:', error.message)
50+
logger.error(`Post-build transform failed: ${error.message}`)
4751
process.exit(1)
4852
}

registry/scripts/utils/get-local-package-aliases.mjs

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)