Skip to content
Open
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
7 changes: 3 additions & 4 deletions src/spectaql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ async function run(opts) {
return fileExists(path.normalize(`${themeDir}/${pathSuffix}`))
})

let arrangeDataModule = arrangeDataDefaultFn
let arrangeData = takeDefaultExport(arrangeDataDefaultFn)
if (customDataArrangerSuffixThatExists) {
try {
arrangeDataModule = await dynamicImport(
const arrangeDataModule = await dynamicImport(
url.pathToFileURL(
path.normalize(`${themeDir}/${customDataArrangerSuffixThatExists}`)
)
)
arrangeData = takeDefaultExport(arrangeDataModule)
} catch (err) {
console.error(err)
if (
Expand All @@ -100,8 +101,6 @@ async function run(opts) {
}
}

const arrangeData = takeDefaultExport(arrangeDataModule)

const items = arrangeData({
introspectionResponse,
graphQLSchema,
Expand Down
12 changes: 12 additions & 0 deletions src/spectaql/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export async function dynamicImport(path) {
) {
return mojule.default
}
// In Node.js 23+, when dynamically importing a CommonJS module that was transpiled
// by Babel with module.exports, the structure can be:
// { __esModule: true, default: { default: [Function] }, 'module.exports': { default: [Function] } }
// In this case, we want to return mojule.default (which contains { default: [Function] })
// so that takeDefaultExport can properly extract the function
if (
mojule.__esModule === true &&
mojule.default?.default &&
mojule['module.exports']?.default
) {
return mojule.default
}
return mojule
}

Expand Down