Skip to content

Commit

Permalink
fix: resolve node_modules (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Apr 27, 2020
1 parent 706c597 commit 78ae1b7
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/node/serverPluginModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,15 @@ export const modulesPlugin: Plugin = ({ root, app, watcher, resolver }) => {

// resolve from node_modules
try {
// get the module name in case of deep imports like 'foo/dist/bar.js'
let moduleName = id
const deepIndex = id.indexOf('/')
if (deepIndex > 0) {
moduleName = id.slice(0, deepIndex)
if (!path.extname(moduleName)) {
const pkgPath = resolve(root, `${moduleName}/package.json`)
const pkg = require(pkgPath)
const entryPoint = pkg.module || pkg.main || 'index.js'
return ctx.redirect(path.join(ctx.path, entryPoint))
}
const pkgPath = resolve(root, `${moduleName}/package.json`)
const pkg = require(pkgPath)
const moduleRelativePath =
deepIndex > 0
? id.slice(deepIndex + 1)
: pkg.module || pkg.main || 'index.js'
const modulePath = path.join(path.dirname(pkgPath), moduleRelativePath)
// in case of deep imports like 'foo/dist/bar.js'
const modulePath = resolve(root, id)
idToFileMap.set(id, modulePath)
fileToIdMap.set(path.basename(modulePath), id)
debugModuleResolution(`${id} -> ${getDebugPath(modulePath)}`)
Expand Down

0 comments on commit 78ae1b7

Please sign in to comment.