Skip to content

Commit

Permalink
fix: support linked monorepos (close #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 7, 2020
1 parent bddca2e commit eb0a885
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
25 changes: 21 additions & 4 deletions src/node/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import path from 'path'
import slash from 'slash'
import { statSync } from 'fs'
import { cleanUrl } from './utils'
import {
idToFileMap,
moduleRE,
fileToRequestMap
} from './server/serverPluginModuleResolve'

export interface Resolver {
requestToFile(publicPath: string, root: string): string | undefined
Expand All @@ -15,11 +20,23 @@ export interface InternalResolver {
idToRequest(id: string): string | undefined
}

const defaultRequestToFile = (publicPath: string, root: string) =>
path.join(root, publicPath.slice(1))
const defaultRequestToFile = (publicPath: string, root: string): string => {
if (moduleRE.test(publicPath)) {
const moduleFilePath = idToFileMap.get(publicPath.replace(moduleRE, ''))
if (moduleFilePath) {
return moduleFilePath
}
}
return path.join(root, publicPath.slice(1))
}

const defaultFileToRequest = (filePath: string, root: string) =>
`/${slash(path.relative(root, filePath))}`
const defaultFileToRequest = (filePath: string, root: string): string => {
const moduleRequest = fileToRequestMap.get(filePath)
if (moduleRequest) {
return moduleRequest
}
return `/${slash(path.relative(root, filePath))}`
}

const defaultIdToRequest = (id: string) => {
if (id.startsWith('@') && id.indexOf('/') < 0) {
Expand Down
1 change: 1 addition & 0 deletions src/node/server/serverPluginHmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export const hmrPlugin: Plugin = ({ root, app, server, watcher, resolver }) => {
const prevDescriptor = cacheEntry && cacheEntry.descriptor
if (!prevDescriptor) {
// the file has never been accessed yet
debugHmr(`no existing descriptor found for ${file}`)
return
}

Expand Down
17 changes: 12 additions & 5 deletions src/node/server/serverPluginModuleResolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import slash from 'slash'
const debug = require('debug')('vite:resolve')

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

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

const getDebugPath = (root: string, p: string) => {
const relative = path.relative(root, p)
return relative.startsWith('..') ? p : relative
}

// plugin for resolving /@modules/:id requests.
export const moduleResolvePlugin: Plugin = ({ root, app }) => {
export const moduleResolvePlugin: Plugin = ({ root, app, watcher }) => {
app.use(async (ctx, next) => {
if (!moduleRE.test(ctx.path)) {
return next()
Expand All @@ -31,10 +32,16 @@ export const moduleResolvePlugin: Plugin = ({ root, app }) => {

const serve = async (id: string, file: string, type: string) => {
idToFileMap.set(id, file)
fileToRequestMap.set(file, ctx.path)
debug(`(${type}) ${id} -> ${getDebugPath(root, file)}`)
// cached read sets etag, body and status on ctx so there is no need
// to go further to other middlewares.
await cachedRead(ctx, file)

// resolved module file is outside of root dir, but is not in node_modules.
// this is likely a linked monorepo/workspace, watch the file for HMR.
if (!file.startsWith(root) && !/node_modules/.test(file)) {
watcher.add(file)
}
await next()
}

// special handling for vue's runtime.
Expand Down

0 comments on commit eb0a885

Please sign in to comment.