Skip to content

Commit

Permalink
fix: should resolve web_modules during rewrite too
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 12, 2020
1 parent e8e4a4b commit b5871eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
18 changes: 8 additions & 10 deletions src/node/server/serverPluginModuleResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const debug = require('debug')('vite:resolve')

export const idToFileMap = new Map()
export const fileToRequestMap = new Map()
const webModulesMap = new Map()

export const moduleRE = /^\/@modules\//

Expand Down Expand Up @@ -56,7 +55,7 @@ export const moduleResolvePlugin: ServerPlugin = ({ root, app, watcher }) => {

// resolve from web_modules
try {
const webModulePath = await resolveWebModule(root, id)
const webModulePath = resolveWebModule(root, id)
if (webModulePath) {
return serve(id, webModulePath, 'web_modules')
}
Expand Down Expand Up @@ -84,17 +83,16 @@ export const moduleResolvePlugin: ServerPlugin = ({ root, app, watcher }) => {
})
}

export async function resolveWebModule(
root: string,
id: string
): Promise<string | undefined> {
let webModulePath = webModulesMap.get(id)
if (webModulePath) {
return webModulePath
const webModulesMap = new Map()

export function resolveWebModule(root: string, id: string): string | undefined {
const cached = webModulesMap.get(id)
if (cached) {
return cached
}
// id could be a common chunk
if (!id.endsWith('.js')) id += '.js'
webModulePath = path.join(root, 'web_modules', id)
const webModulePath = path.join(root, 'web_modules', id)
if (fs.existsSync(webModulePath)) {
webModulesMap.set(id, webModulePath)
return webModulePath
Expand Down
9 changes: 7 additions & 2 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import {
resolveRelativeRequest
} from '../utils'
import chalk from 'chalk'
import { resolveNodeModuleEntry } from './serverPluginModuleResolve'
import {
resolveNodeModuleEntry,
resolveWebModule
} from './serverPluginModuleResolve'

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

Expand Down Expand Up @@ -281,7 +284,9 @@ export const resolveImport = (
if (bareImportRE.test(id)) {
// directly resolve bare module names to its entry path so that relative
// imports from it (including source map urls) can work correctly
return `/@modules/${resolveNodeModuleEntry(root, id) || id}`
return `/@modules/${
resolveWebModule(root, id) || resolveNodeModuleEntry(root, id) || id
}`
} else {
let { pathname, query } = resolveRelativeRequest(importer, id)
// append an extension to extension-less imports
Expand Down

0 comments on commit b5871eb

Please sign in to comment.