Skip to content

Commit

Permalink
fix: support resolving .mjs and other exts in module deep imports
Browse files Browse the repository at this point in the history
close #127
  • Loading branch information
yyx990803 committed May 13, 2020
1 parent 7f5e459 commit 02753b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ const defaultFileToRequest = (filePath: string, root: string): string => {
return `/${slash(path.relative(root, filePath))}`
}

export const supportedExts = ['.js', '.ts', '.jsx', '.tsx', '.json']
export const supportedExts = ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']

const debug = require('debug')('vite:resolve')

const resolveExt = (id: string) => {
export const resolveExt = (id: string) => {
const cleanId = cleanUrl(id)
if (!path.extname(cleanId)) {
let inferredExt = ''
Expand Down
12 changes: 11 additions & 1 deletion src/node/server/serverPluginModuleResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import resolve from 'resolve-from'
import { ServerPlugin } from '.'
import { resolveVue, cachedRead } from '../utils'
import { URL } from 'url'
import { supportedExts } from '../resolver'

const debug = require('debug')('vite:resolve')

Expand Down Expand Up @@ -155,9 +156,18 @@ function resolveNodeModule(root: string, id: string): string | undefined {
nodeModulesMap.set(id, entryPoint)
return entryPoint
} else {
// possibly a deep import, try resolving directly
// possibly a deep import
try {
return resolve(root, id)
} catch (e) {}

// no match and no ext, try all exts
if (!path.extname(id)) {
for (const ext of supportedExts) {
try {
return resolve(root, id + ext)
} catch (e) {}
}
}
}
}

0 comments on commit 02753b7

Please sign in to comment.