Skip to content

Commit

Permalink
use in operator to check exports
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jun 9, 2023
1 parent 6c8a790 commit 75132f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,29 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
if (isDynamicResource) {
// re-export and spread as `exportedImageData` to avoid non-exported error
return `\
import * as exported from ${JSON.stringify(resourcePath)}
import * as mod from ${JSON.stringify(resourcePath)}
import { fillMetadataSegment } from 'next/dist/lib/metadata/get-metadata-route'
const imageModule = { ...exported }
export default async function (props) {
const { __metadata_id__: _, ...params } = props.params
const imageUrl = fillMetadataSegment(${JSON.stringify(
pathnamePrefix
)}, params, ${JSON.stringify(pageSegment)})
const { generateImageMetadata } = imageModule
let generateImageMetadata
if ('generateImageMetadata' in mod) {
generateImageMetadata = mod.generateImageMetadata
}
function getImageMetadata(imageMetadata, idParam) {
const data = {
alt: imageMetadata.alt,
type: imageMetadata.contentType || 'image/png',
alt: 'alt' in imageMetadata ? imageMetadata.alt : undefined,
type: 'contentType' in imageMetadata ? imageMetadata.contentType : 'image/png',
url: imageUrl + (idParam ? ('/' + idParam) : '') + ${JSON.stringify(
hashQuery
)},
}
const { size } = imageMetadata
const size = 'size' in imageMetadata ? imageMetadata.size : undefined
if (size) {
${
type === 'twitter' || type === 'openGraph'
Expand All @@ -92,7 +94,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
return getImageMetadata(imageMetadata, idParam)
})
} else {
return [getImageMetadata(imageModule, '')]
return [getImageMetadata(mod, '')]
}
}`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ export default function middlewareLoader(this: any) {
return `
import 'next/dist/esm/server/web/globals'
import { adapter } from 'next/dist/esm/server/web/adapter'
import * as _mod from ${stringifiedPagePath}
import * as mod from ${stringifiedPagePath}
const mod = { ..._mod }
const handler = mod.middleware || mod.default
let handler
if ('middleware' in mod) {
handler = mod.middleware
} else if ('default' in mod) {
handler = mod.default
}
if (typeof handler !== 'function') {
throw new Error('The Middleware "pages${page}" must export a \`middleware\` or a \`default\` function');
Expand Down

0 comments on commit 75132f3

Please sign in to comment.