From b5871eba505e5a109b8b8ae07d6f8a70c6d970eb Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 12 May 2020 13:42:36 -0400 Subject: [PATCH] fix: should resolve web_modules during rewrite too --- src/node/server/serverPluginModuleResolve.ts | 18 ++++++++---------- src/node/server/serverPluginModuleRewrite.ts | 9 +++++++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/node/server/serverPluginModuleResolve.ts b/src/node/server/serverPluginModuleResolve.ts index 0a7a01c8267544..fdae8549b115df 100644 --- a/src/node/server/serverPluginModuleResolve.ts +++ b/src/node/server/serverPluginModuleResolve.ts @@ -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\// @@ -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') } @@ -84,17 +83,16 @@ export const moduleResolvePlugin: ServerPlugin = ({ root, app, watcher }) => { }) } -export async function resolveWebModule( - root: string, - id: string -): Promise { - 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 diff --git a/src/node/server/serverPluginModuleRewrite.ts b/src/node/server/serverPluginModuleRewrite.ts index 2704062f575bf1..b00b5c6fa84666 100644 --- a/src/node/server/serverPluginModuleRewrite.ts +++ b/src/node/server/serverPluginModuleRewrite.ts @@ -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') @@ -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