fix: avoid import if no config present (prod build error)

This commit is contained in:
bholmesdev 2023-02-10 09:00:53 -05:00
parent a6cbf7b7e1
commit 9c5d41959d

View file

@ -3,6 +3,7 @@ import type { InlineConfig } from 'vite';
import _Markdoc from '@markdoc/markdoc'; import _Markdoc from '@markdoc/markdoc';
import { parseFrontmatter } from './utils.js'; import { parseFrontmatter } from './utils.js';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import fs from 'node:fs';
const contentEntryType = { const contentEntryType = {
extensions: ['.mdoc'], extensions: ['.mdoc'],
@ -25,6 +26,15 @@ export default function markdoc(partialOptions: {} = {}): AstroIntegration {
addContentEntryType(contentEntryType); addContentEntryType(contentEntryType);
console.log('Markdoc working!'); console.log('Markdoc working!');
const markdocConfigUrl = new URL('./markdoc.config', config.srcDir); 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 = { const viteConfig: InlineConfig = {
plugins: [ 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( 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 code
)};\nexport function getParsed() { return Markdoc.parse(body); }\nexport async function getTransformed(inlineConfig) { )};\nexport function getParsed() { return Markdoc.parse(body); }\nexport async function getTransformed(inlineConfig) {
let config = inlineConfig; let config = inlineConfig;\n${
if (!config) { fs.existsSync(markdocConfigUrl) ? configFileLoad : ''
try { }\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;`;
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;`;
}, },
}, },
], ],