diff --git a/hook.js b/hook.js index fb4d598..296e90d 100644 --- a/hook.js +++ b/hook.js @@ -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, ` diff --git a/test/hook/vue-server-renderer.mjs b/test/hook/vue-server-renderer.mjs index 2e0b3b2..69791d0 100644 --- a/test/hook/vue-server-renderer.mjs +++ b/test/hook/vue-server-renderer.mjs @@ -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')