Skip to content

Commit

Permalink
feat: support deep module imports
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 23, 2020
1 parent 3e5076d commit c11cfc8
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/server/plugins/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,19 @@ export const modulesPlugin: Plugin = ({ root, app }) => {

// resolve from node_modules
try {
const pkgPath = resolve(root, `${id}/package.json`)
// 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)
}
const pkgPath = resolve(root, `${moduleName}/package.json`)
const pkg = require(pkgPath)
const modulePath = path.join(
path.dirname(pkgPath),
pkg.module || pkg.main
)
const moduleRelativePath =
deepIndex > 0
? id.slice(deepIndex + 1)
: pkg.module || pkg.main || 'index.js'
const modulePath = path.join(path.dirname(pkgPath), moduleRelativePath)
idToFileMap.set(id, modulePath)
fileToIdMap.set(path.basename(modulePath), id)
ctx.body = await cachedRead(modulePath)
Expand Down

0 comments on commit c11cfc8

Please sign in to comment.