Skip to content

Commit

Permalink
Merge pull request #747 from Financial-Times/entrypoint-module-exports
Browse files Browse the repository at this point in the history
fix: actually support CJS in importEntryPoint
  • Loading branch information
apaleslimghost authored Jan 9, 2025
2 parents d83eeb3 + 2caed3d commit 057ff97
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions core/cli/src/plugin/entry-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Base } from '@dotcom-tool-kit/base'
import type { EntryPoint } from '@dotcom-tool-kit/plugin'
import { Validated, invalid } from '@dotcom-tool-kit/validated'
import { isPlainObject } from 'lodash'
import { __importDefault } from 'tslib'
import { indentReasons } from '../messages'

const isPlainObjectGuard = (value: unknown): value is Record<string, unknown> => isPlainObject(value)
Expand Down Expand Up @@ -36,25 +37,23 @@ export async function importEntryPoint<T extends { name: string } & Omit<typeof
])
}

if (
isPlainObjectGuard(pluginModule) &&
'default' in pluginModule &&
typeof pluginModule.default === 'function'
) {
const name = pluginModule.default.name

return type
.isCompatible<T>(pluginModule.default)
.mapError((reasons) => [
`the ${type.name.toLowerCase()} ${s.hook(name)} is not a compatible instance of ${s.code(
type.name
)}:\n - ${reasons.join('\n - ')}`
])
} else {
return invalid([
`entrypoint ${s.filepath(entryPoint.modulePath)} in plugin ${s.plugin(
entryPoint.plugin.id
)} does not have a ${s.code('default')} export`
])
if (isPlainObjectGuard(pluginModule)) {
const defaultExport = __importDefault(pluginModule).default

if (typeof defaultExport === 'function') {
return type
.isCompatible<T>(defaultExport)
.mapError((reasons) => [
`the ${type.name.toLowerCase()} ${s.hook(
defaultExport.name
)} is not a compatible instance of ${s.code(type.name)}:\n - ${reasons.join('\n - ')}`
])
}
}

return invalid([
`entrypoint ${s.filepath(entryPoint.modulePath)} in plugin ${s.plugin(
entryPoint.plugin.id
)} does not export a class as its ${s.code('default')} export or ${s.code('module.exports')}`
])
}

0 comments on commit 057ff97

Please sign in to comment.