Skip to content

Commit

Permalink
fix: Use correct format when resolving exports from relative paths (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Jul 16, 2024
1 parent 279d8b5 commit 632802f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
31 changes: 18 additions & 13 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,24 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
if (isStarExportLine(n) === true) {
const [, modFile] = n.split('* from ')

async function processSubModule (url, ctx) {
const setters = await processModule({ srcUrl: url, context: ctx, parentGetSource, parentResolve, excludeDefault: true })
for (const [name, setter] of setters.entries()) {
addSetter(name, setter, true)
}
}

if (isBareSpecifier(modFile)) {
// Bare specifiers need to be resolved relative to the parent module.
const result = await parentResolve(modFile, { parentURL: srcUrl })
await processSubModule(result.url, { ...context, format: result.format })
} else {
await processSubModule(new URL(modFile, srcUrl).href, context)
// Relative paths need to be resolved relative to the parent module
const newSpecifier = isBareSpecifier(modFile) ? modFile : new URL(modFile, srcUrl).href
// We need to call `parentResolve` to resolve bare specifiers to a full
// URL. We also need to call `parentResolve` for all sub-modules to get
// the `format`. We can't rely on the parents `format` to know if this
// sub-module is ESM or CJS!
const result = await parentResolve(newSpecifier, { parentURL: srcUrl })

const subSetters = await processModule({
srcUrl: result.url,
context: { ...context, format: result.format },
parentGetSource,
parentResolve,
excludeDefault: true
})

for (const [name, setter] of subSetters.entries()) {
addSetter(name, setter, true)
}
} else {
addSetter(n, `
Expand Down
9 changes: 6 additions & 3 deletions test/hook/vue-server-renderer.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// https://github.com/nodejs/import-in-the-middle/issues/139
import { strictEqual } from 'assert'
import * as lib from 'vue/server-renderer'
// https://github.com/nodejs/import-in-the-middle/issues/139
import * as libServer from 'vue/server-renderer'
// https://github.com/nodejs/import-in-the-middle/issues/144
import * as lib from 'vue'

strictEqual(typeof lib.renderToString, 'function')
strictEqual(typeof libServer.renderToString, 'function')
strictEqual(typeof lib.ref, 'function')

0 comments on commit 632802f

Please sign in to comment.