Skip to content

Commit

Permalink
fix: avoid import if no config present (prod build error)
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Feb 16, 2023
1 parent bda8295 commit cb19048
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions packages/integrations/markdoc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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: [
Expand All @@ -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;`;
},
},
],
Expand Down

0 comments on commit cb19048

Please sign in to comment.