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

Commit 5037428

Browse files
committed
fix: fix loadConfig (#125)
1 parent 7cafc90 commit 5037428

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

server/config.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import log from '../shared/log.ts'
66
import util from '../shared/util.ts'
77
import type { Config, ImportMap } from '../types.ts'
88
import { VERSION } from '../version.ts'
9-
import { fixImportMap, reLocaleID } from './util.ts'
9+
import { reLocaleID } from './util.ts'
1010

1111
export const defaultConfig: Readonly<Required<Config>> = {
1212
framework: 'react',
@@ -83,17 +83,17 @@ export async function loadConfig(workingDir: string): Promise<[Config, ImportMap
8383
config.outputDir = util.cleanPath(outputDir)
8484
}
8585
if (util.isNEString(baseUrl)) {
86-
config.baseUrl = util.cleanPath(encodeURI(baseUrl))
86+
config.baseUrl = util.cleanPath(baseUrl)
8787
}
88-
if (isTarget(buildTarget)) {
88+
if (isBuildTarget(buildTarget)) {
8989
config.buildTarget = buildTarget
9090
}
91-
if (util.isNEString(defaultLocale)) {
91+
if (isLocaleID(defaultLocale)) {
9292
config.defaultLocale = defaultLocale
9393
}
9494
if (util.isArray(locales)) {
95-
locales.filter(l => !reLocaleID.test(l)).forEach(l => log.warn(`invalid locale ID '${l}'`))
96-
config.locales = Array.from(new Set(locales.filter(l => reLocaleID.test(l))))
95+
locales.filter(id => !isLocaleID(id)).forEach(id => log.warn(`invalid locale ID '${id}'`))
96+
config.locales = Array.from(new Set(locales.filter(isLocaleID)))
9797
}
9898
if (typeof ssr === 'boolean') {
9999
config.ssr = ssr
@@ -144,12 +144,12 @@ export async function loadConfig(workingDir: string): Promise<[Config, ImportMap
144144
for (const filename of Array.from(['import_map', 'import-map', 'importmap']).map(name => `${name}.json`)) {
145145
const importMapFile = path.join(workingDir, filename)
146146
if (existsFileSync(importMapFile)) {
147-
const importMap = JSON.parse(await Deno.readTextFile(importMapFile))
148-
const imports: Record<string, string> = fixImportMap(importMap.imports)
147+
const data = JSON.parse(await Deno.readTextFile(importMapFile))
148+
const imports: Record<string, string> = fixImportMap(data.imports)
149149
const scopes: Record<string, Record<string, string>> = {}
150-
if (util.isPlainObject(importMap.scopes)) {
151-
Object.entries(importMap.scopes).forEach(([key, imports]) => {
152-
scopes[key] = fixImportMap(imports)
150+
if (util.isPlainObject(data.scopes)) {
151+
Object.entries(data.scopes).forEach(([scope, imports]) => {
152+
scopes[scope] = fixImportMap(imports)
153153
})
154154
}
155155
Object.assign(importMap, { imports, scopes })
@@ -184,7 +184,7 @@ function isFramework(v: any): v is 'react' {
184184
}
185185
}
186186

187-
function isTarget(v: any): v is 'es5' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' {
187+
function isBuildTarget(v: any): v is 'es5' | 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' {
188188
switch (v) {
189189
case 'es5':
190190
case 'es2015':
@@ -199,6 +199,35 @@ function isTarget(v: any): v is 'es5' | 'es2015' | 'es2016' | 'es2017' | 'es2018
199199
}
200200
}
201201

202+
function isLocaleID(v: any): v is string {
203+
return util.isNEString(v) && reLocaleID.test(v)
204+
}
205+
202206
function isPostcssConfig(v: any): v is { plugins: (string | AcceptedPlugin | [string | ((options: Record<string, any>) => AcceptedPlugin), Record<string, any>])[] } {
203207
return util.isPlainObject(v) && util.isArray(v.plugins)
204208
}
209+
210+
function fixImportMap(v: any) {
211+
const imports: Record<string, string> = {}
212+
if (util.isPlainObject(v)) {
213+
Object.entries(v).forEach(([key, value]) => {
214+
if (key == '' || key == '/') {
215+
return
216+
}
217+
const isPrefix = key.endsWith('/')
218+
const y = (v: string) => util.isNEString(v) && (!isPrefix || v.endsWith('/'))
219+
if (y(value)) {
220+
imports[key] = value
221+
return
222+
} else if (util.isNEArray(value)) {
223+
for (const v of value) {
224+
if (y(v)) {
225+
imports[key] = v
226+
return
227+
}
228+
}
229+
}
230+
})
231+
}
232+
return imports
233+
}

server/util.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,6 @@ export async function cleanupCompilation(jsFile: string) {
8383
}
8484
}
8585

86-
/** fix import map */
87-
export function fixImportMap(v: any) {
88-
const imports: Record<string, string> = {}
89-
if (util.isPlainObject(v)) {
90-
Object.entries(v).forEach(([key, value]) => {
91-
if (key == '' || key == '/') {
92-
return
93-
}
94-
const isPrefix = key.endsWith('/')
95-
const y = (v: string) => util.isNEString(v) && (!isPrefix || v.endsWith('/'))
96-
if (y(value)) {
97-
imports[key] = value
98-
return
99-
} else if (util.isNEArray(value)) {
100-
for (const v of value) {
101-
if (y(v)) {
102-
imports[key] = v
103-
return
104-
}
105-
}
106-
}
107-
})
108-
}
109-
return imports
110-
}
111-
11286
/** parse port number */
11387
export function parsePortNumber(v: string): number {
11488
const num = parseInt(v)

0 commit comments

Comments
 (0)