Skip to content

Commit c23373e

Browse files
committed
refactor: improve config loader
1 parent 3fd0276 commit c23373e

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/bin/loader.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { readFile } from 'node:fs/promises'
22
import { resolve } from 'node:path'
33
import { exists } from '@hypernym/utils/node'
4+
import { cyan } from '@hypernym/colors'
45
import { build } from 'esbuild'
56
import { externals } from '../config.js'
6-
import { logger, error, errorMessage } from '../utils/index.js'
7+
import { logger, error } from '../utils/index.js'
78
import type { Options } from '../types/options.js'
89
import type { Args } from '../types/args.js'
910

@@ -17,6 +18,7 @@ export async function loadConfig(
1718
write: false,
1819
format: 'esm',
1920
target: 'esnext',
21+
packages: 'external',
2022
})
2123
const code = result.outputFiles[0].text
2224
const buffer = Buffer.from(code).toString('base64')
@@ -37,6 +39,10 @@ export async function createConfigLoader(
3739
const pkg = await readFile(pkgPath, 'utf-8').catch(error)
3840
const { dependencies } = JSON.parse(pkg)
3941

42+
const warnMessage = `Missing required configuration. To start bundling, add the ${cyan(
43+
`'bundler.config.{js,mjs,ts,mts}'`,
44+
)} file to the project's root.`
45+
4046
const defaults: Options = {
4147
externals: [...Object.keys(dependencies || {}), ...externals],
4248
entries: [],
@@ -46,17 +52,17 @@ export async function createConfigLoader(
4652
const path = resolve(cwd, args.config)
4753
const isConfig = await exists(path)
4854
if (isConfig) return await loadConfig(path, defaults)
49-
else return logger.exit(errorMessage[404])
55+
else return logger.exit(warnMessage)
5056
}
5157

5258
const configName = 'bundler.config'
53-
const configExts: string[] = ['.js', '.mjs', '.ts', '.mts']
59+
const configExts: string[] = ['.ts', '.js', '.mts', '.mjs']
5460

5561
for (const ext of configExts) {
5662
const path = resolve(cwd, `${configName}${ext}`)
5763
const isConfig = await exists(path)
5864
if (isConfig) return await loadConfig(path, defaults)
5965
}
6066

61-
return logger.exit(errorMessage[404])
67+
return logger.exit(warnMessage)
6268
}

0 commit comments

Comments
 (0)