Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve node_modules #15

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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