Skip to content

Commit 4dedb0d

Browse files
committed
fix: edge case
1 parent 12b4da4 commit 4dedb0d

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/generator/resolver.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { RESOLVER_EXT } from '../constants'
44

55
const debug = Debug('vite-plugin-components:resolver')
66

7-
function timeoutError(reqPath:string){
8-
return new Promise<any>((resolve, reject)=>{
9-
setTimeout(()=>{reject("Timeout on resolving " + reqPath)}, 3000)
7+
function timeoutError(reqPath: string, timeout = 10000) {
8+
return new Promise<any>((resolve, reject) => {
9+
setTimeout(() => {
10+
reject(new Error(`Timeout on resolving ${reqPath}`))
11+
}, timeout)
1012
})
1113
}
1214

@@ -19,13 +21,12 @@ export async function generateResolver(ctx: Context, reqPath: string) {
1921
debug(sfcPath)
2022

2123
const names: string[] = await Promise.race([
22-
ctx.getImports(sfcPath),
23-
timeoutError(reqPath)
24+
ctx.getImports(sfcPath),
25+
timeoutError(reqPath),
2426
]) || []
2527

26-
if (!names?.length) {
27-
return `export default c => c`
28-
}
28+
if (!names?.length)
29+
return 'export default c => c'
2930

3031
const components = ctx.findComponents(names, [sfcPath])
3132

src/transforms/vueScriptSetup.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ const debug = Debug('vite-plugin-components:transform:script-setup')
88
export function VueScriptSetupTransformer(ctx: Context): Transform {
99
return {
1010
test({ path, query }) {
11-
return path.endsWith('.vue') && query.type === 'script' && !!query.setup
11+
return path.endsWith('.vue') && query.type === 'script' && (Boolean(query.setup) || query.setup === '')
1212
},
1313
transform({ code, path, isBuild }) {
1414
const sfcPath = ctx.normalizePath(path)
1515
debug(sfcPath)
1616

17-
let head: string[] = []
17+
const head: string[] = []
1818
let id = 0
1919

2020
let transformed = code.replace(/_resolveComponent\("(.+?)"\)/g, (str, match) => {
2121
if (match) {
22-
debug("name: " + match)
22+
debug(`name: ${match}`)
2323
const component = ctx.findComponent(normalize(match), [sfcPath])
2424
if (component) {
25-
let var_name = `__vite_component_${id}`
25+
const var_name = `__vite_component_${id}`
2626
head.push(`import ${var_name} from "/${component.path}"`)
2727
id += 1
2828
return var_name
@@ -31,13 +31,12 @@ export function VueScriptSetupTransformer(ctx: Context): Transform {
3131
return str
3232
})
3333

34-
transformed = head.join('\n') + '\n' + transformed
34+
transformed = `${head.join('\n')}\n${transformed}`
3535

3636
// debug(transformed)
3737

38-
if (isBuild) {
38+
if (isBuild)
3939
ctx.setImports(sfcPath, [])
40-
}
4140

4241
return transformed
4342
},

0 commit comments

Comments
 (0)