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: Use correct format when resolving exports from relative paths #145

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
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
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')
Loading