Skip to content

Commit

Permalink
fix: fix rewrite extension appending for out of root files
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 25, 2020
1 parent 4fceaea commit 84fcfb6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ export function rewriteImports(
}

const bareImportRE = /^[^\/\.]/
const indexRE = /\/index\.\w+$/
const indexRemoveRE = /\/index(\.\w+)?$/

export const resolveImport = (
root: string,
Expand All @@ -246,8 +248,16 @@ export const resolveImport = (
}

// 3. resolve extensions.
const file = resolver.requestToFile(pathname)
pathname = '/' + slash(path.relative(root, file))
const file = slash(resolver.requestToFile(pathname))
const resolvedExt = path.extname(file)
if (resolvedExt !== path.extname(pathname)) {
const indexMatch = file.match(indexRE)
if (indexMatch) {
pathname = pathname.replace(indexRemoveRE, '') + indexMatch[0]
} else {
pathname += resolvedExt
}
}

// 4. mark non-src imports
const ext = path.extname(pathname)
Expand Down

0 comments on commit 84fcfb6

Please sign in to comment.