Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 4087505

Browse files
author
Je
committed
refactor: improve the _loadConfig method
1 parent c236c69 commit 4087505

File tree

1 file changed

+64
-45
lines changed

1 file changed

+64
-45
lines changed

project.ts

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ export default class Project {
254254
}
255255
await Promise.all([outputDir, distDir].map(dir => ensureDir(dir)))
256256

257+
// copy public files
258+
const publicDir = path.join(this.rootDir, 'public')
259+
if (util.existsDir(publicDir)) {
260+
for await (const { path: p } of walk(publicDir, { includeDirs: false })) {
261+
const rp = path.resolve(util.trimPrefix(p, publicDir))
262+
await Deno.copyFile(p, path.join(outputDir, rp))
263+
}
264+
}
265+
257266
// write modules
258267
const { sourceMap } = this.config
259268
await Promise.all(Array.from(outputModules).map((moduleId) => {
@@ -287,15 +296,6 @@ export default class Project {
287296
await writeTextFile(path.join(outputDir, 'index.html'), this.getDefaultIndexHtml())
288297
}
289298

290-
// copy public files
291-
const publicDir = path.join(this.rootDir, 'public')
292-
if (util.existsDir(publicDir)) {
293-
for await (const { path: p } of walk(publicDir, { includeDirs: false })) {
294-
const rp = path.resolve(util.trimPrefix(p, publicDir))
295-
await Deno.copyFile(p, path.join(outputDir, rp))
296-
}
297-
}
298-
299299
log.info(`Done in ${Math.round(performance.now() - start)}ms`)
300300
}
301301

@@ -312,44 +312,63 @@ export default class Project {
312312
Object.assign(this.config.importMap, { imports: Object.assign({}, this.config.importMap.imports, imports) })
313313
}
314314

315-
const configFile = path.join(this.rootDir, 'aleph.config.json')
316-
if (util.existsFile(configFile)) {
317-
const {
318-
srcDir,
319-
ouputDir,
320-
baseUrl,
321-
ssr,
322-
buildTarget,
323-
sourceMap,
324-
defaultLocale
325-
} = JSON.parse(await Deno.readTextFile(configFile))
326-
if (util.isNEString(srcDir)) {
327-
Object.assign(this.config, { srcDir: util.cleanPath(srcDir) })
328-
}
329-
if (util.isNEString(ouputDir)) {
330-
Object.assign(this.config, { ouputDir: util.cleanPath(ouputDir) })
331-
}
332-
if (util.isNEString(baseUrl)) {
333-
Object.assign(this.config, { baseUrl: util.cleanPath(encodeURI(baseUrl)) })
334-
}
335-
if (util.isNEString(defaultLocale)) {
336-
Object.assign(this.config, { defaultLocale })
337-
}
338-
if (typeof ssr === 'boolean') {
339-
Object.assign(this.config, { ssr })
340-
} else if (util.isPlainObject(ssr)) {
341-
const fallback = util.isNEString(ssr.fallback) ? ssr.fallback : '404.html'
342-
const include = util.isArray(ssr.include) ? ssr.include : []
343-
const exclude = util.isArray(ssr.exclude) ? ssr.exclude : []
344-
Object.assign(this.config, { ssr: { fallback, include, exclude } })
345-
}
346-
if (/^es(20\d{2}|next)$/i.test(buildTarget)) {
347-
Object.assign(this.config, { buildTarget: buildTarget.toLowerCase() })
348-
}
349-
if (typeof sourceMap === 'boolean') {
350-
Object.assign(this.config, { sourceMap })
315+
const config: Record<string, any> = {}
316+
for await (const { path: p } of walk(this.srcDir, { includeDirs: false, exts: ['.js', '.mjs', '.ts', '.json'], skip: [/\.d\.ts$/i], maxDepth: 1 })) {
317+
const name = path.basename(p)
318+
if (name.split('.')[0] === 'config') {
319+
if (name.endsWith('.json')) {
320+
try {
321+
const conf = JSON.parse(await Deno.readTextFile(p))
322+
Object.assign(config, conf)
323+
log.debug(name, config)
324+
} catch (e) {
325+
log.fatal('parse config.json:', e.message)
326+
}
327+
} else {
328+
const { default: conf } = await import(p)
329+
if (util.isPlainObject(conf)) {
330+
Object.assign(config, conf)
331+
log.debug(name, config)
332+
}
333+
}
351334
}
352335
}
336+
337+
const {
338+
srcDir,
339+
ouputDir,
340+
baseUrl,
341+
ssr,
342+
buildTarget,
343+
sourceMap,
344+
defaultLocale
345+
} = config
346+
if (util.isNEString(srcDir)) {
347+
Object.assign(this.config, { srcDir: util.cleanPath(srcDir) })
348+
}
349+
if (util.isNEString(ouputDir)) {
350+
Object.assign(this.config, { ouputDir: util.cleanPath(ouputDir) })
351+
}
352+
if (util.isNEString(baseUrl)) {
353+
Object.assign(this.config, { baseUrl: util.cleanPath(encodeURI(baseUrl)) })
354+
}
355+
if (util.isNEString(defaultLocale)) {
356+
Object.assign(this.config, { defaultLocale })
357+
}
358+
if (typeof ssr === 'boolean') {
359+
Object.assign(this.config, { ssr })
360+
} else if (util.isPlainObject(ssr)) {
361+
const fallback = util.isNEString(ssr.fallback) ? ssr.fallback : '404.html'
362+
const include = util.isArray(ssr.include) ? ssr.include : []
363+
const exclude = util.isArray(ssr.exclude) ? ssr.exclude : []
364+
Object.assign(this.config, { ssr: { fallback, include, exclude } })
365+
}
366+
if (/^es(20\d{2}|next)$/i.test(buildTarget)) {
367+
Object.assign(this.config, { buildTarget: buildTarget.toLowerCase() })
368+
}
369+
if (typeof sourceMap === 'boolean') {
370+
Object.assign(this.config, { sourceMap })
371+
}
353372
}
354373

355374
private async _init() {

0 commit comments

Comments
 (0)