Skip to content

Commit

Permalink
fix: fix resolver ensurejs check
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 30, 2020
1 parent 770e558 commit 7b126af
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import slash from 'slash'
import { statSync } from 'fs'

export interface Resolver {
requestToFile(publicPath: string, root: string): string | undefined
Expand Down Expand Up @@ -28,7 +29,14 @@ const defaultIdToRequest = (id: string) => {
const queryRE = /\?.*$/
const ensureJs = (id: string) => {
const cleanId = id.replace(queryRE, '')
if (!/\.\w+/.test(cleanId)) {
if (!/\.\w+$/.test(cleanId)) {
// try to see if there is actually a corresponding .js file on disk.
// if not, return the id as-is
try {
statSync(cleanId + '.js')
} catch (e) {
return id
}
const queryMatch = id.match(queryRE)
const query = queryMatch ? queryMatch[0] : ''
return cleanId + '.js' + query
Expand All @@ -53,12 +61,8 @@ export function createResolver(
if (!resolved) {
resolved = defaultRequestToFile(publicPath, root)
}
// @ is reserved for special modules, leave as-is
if (resolved.startsWith(`/@`)) {
return resolved
} else {
return ensureJs(resolved)
}
resolved = ensureJs(resolved)
return resolved
},
fileToRequest: (filePath) => {
for (const r of resolvers) {
Expand Down

0 comments on commit 7b126af

Please sign in to comment.