From cb1904876f074c50c206131e9588878230bbab47 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 10 Feb 2023 09:00:53 -0500 Subject: [PATCH] fix: avoid import if no config present (prod build error) --- packages/integrations/markdoc/src/index.ts | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/integrations/markdoc/src/index.ts b/packages/integrations/markdoc/src/index.ts index 9d386a555fd1..c7ba70599809 100644 --- a/packages/integrations/markdoc/src/index.ts +++ b/packages/integrations/markdoc/src/index.ts @@ -3,6 +3,7 @@ import type { InlineConfig } from 'vite'; import _Markdoc from '@markdoc/markdoc'; import { parseFrontmatter } from './utils.js'; import { fileURLToPath } from 'node:url'; +import fs from 'node:fs'; const contentEntryType = { extensions: ['.mdoc'], @@ -25,6 +26,15 @@ export default function markdoc(partialOptions: {} = {}): AstroIntegration { addContentEntryType(contentEntryType); console.log('Markdoc working!'); const markdocConfigUrl = new URL('./markdoc.config', config.srcDir); + const configFileLoad = `if (!config) { + try { + const importedConfig = await import(${JSON.stringify(markdocConfigUrl.pathname)}); + console.log({importedConfig}) + config = importedConfig.default.transform; + } catch { + config = {}; + } + }`; const viteConfig: InlineConfig = { plugins: [ @@ -35,17 +45,9 @@ export default function markdoc(partialOptions: {} = {}): AstroIntegration { return `import { jsx as h } from 'astro/jsx-runtime';\nimport { Markdoc } from '@astrojs/markdoc';\nimport { Renderer } from '@astrojs/markdoc/components';\nexport const body = ${JSON.stringify( code )};\nexport function getParsed() { return Markdoc.parse(body); }\nexport async function getTransformed(inlineConfig) { -let config = inlineConfig; -if (!config) { - try { - const importedConfig = await import(${JSON.stringify(markdocConfigUrl.pathname)}); - console.log({importedConfig}) - config = importedConfig.default.transform; - } catch { - config = {}; - } -} -return Markdoc.transform(getParsed(), config) }\nexport async function Content ({ config, components }) { return h(Renderer, { content: await getTransformed(config), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`; +let config = inlineConfig;\n${ + fs.existsSync(markdocConfigUrl) ? configFileLoad : '' + }\nreturn Markdoc.transform(getParsed(), config) }\nexport async function Content ({ config, components }) { return h(Renderer, { content: await getTransformed(config), components }); }\nContent[Symbol.for('astro.needsHeadRendering')] = true;`; }, }, ],