diff --git a/.changeset/dull-radios-sparkle.md b/.changeset/dull-radios-sparkle.md new file mode 100644 index 000000000..76f207898 --- /dev/null +++ b/.changeset/dull-radios-sparkle.md @@ -0,0 +1,6 @@ +--- +'astro': patch +'@astrojs/mdx': patch +--- + +Fix MDX working with a ts config file diff --git a/packages/astro/src/core/config.ts b/packages/astro/src/core/config.ts index d5525cbf8..b8057df99 100644 --- a/packages/astro/src/core/config.ts +++ b/packages/astro/src/core/config.ts @@ -16,6 +16,7 @@ import { z } from 'zod'; import { LogOptions } from './logger/core.js'; import { appendForwardSlash, prependForwardSlash, trimSlashes } from './path.js'; import { arraify, isObject } from './util.js'; +import * as vite from 'vite'; load.use([loadTypeScript]); @@ -409,10 +410,13 @@ export async function resolveConfigURL( const flags = resolveFlags(configOptions.flags || {}); let userConfigPath: string | undefined; + + if (flags?.config) { userConfigPath = /^\.*\//.test(flags.config) ? flags.config : `./${flags.config}`; userConfigPath = fileURLToPath(new URL(userConfigPath, `file://${root}/`)); } + // Resolve config file path using Proload // If `userConfigPath` is `undefined`, Proload will search for `astro.config.[cm]?[jt]s` const configPath = await resolve('astro', { @@ -447,21 +451,7 @@ export async function openConfig(configOptions: LoadConfigOptions): Promise; + filePath?: string; +} + +async function tryLoadConfig(configOptions: LoadConfigOptions, flags: CLIFlags, userConfigPath: string | undefined, root: string): Promise { + try { + // Automatically load config file using Proload + // If `userConfigPath` is `undefined`, Proload will search for `astro.config.[cm]?[jt]s` + const config = await load('astro', { + mustExist: !!userConfigPath, + cwd: root, + filePath: userConfigPath, + }); + + return config as TryLoadConfigResult; + } catch(e) { + if (e instanceof ProloadError && flags.config) { + throw new Error(`Unable to resolve --config "${flags.config}"! Does the file exist?`); + } + + const configURL = await resolveConfigURL(configOptions); + if(!configURL) { + throw e; + } + + // Fallback to use Vite DevServer + const viteServer = await vite.createServer({ + server: { middlewareMode: true, hmr: false }, + appType: 'custom' + }); + try { + const mod = await viteServer.ssrLoadModule(fileURLToPath(configURL)); + + if(mod?.default) { + return { + value: mod.default, + filePath: fileURLToPath(configURL), + }; + }; + } finally { + await viteServer.close(); + } + } +} + /** * Attempt to load an `astro.config.mjs` file * @deprecated @@ -500,21 +536,7 @@ export async function loadConfig(configOptions: LoadConfigOptions): Promise { before(async () => { fixture = await loadFixture({ - root: new URL('./fixtures/mdx-page/', import.meta.url), - integrations: [mdx()], + root: new URL('./fixtures/mdx-page/', import.meta.url) }); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b198726e9..8dc64c9fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2231,6 +2231,14 @@ importers: reading-time: 1.5.0 remark-toc: 8.0.1 + packages/integrations/mdx/test/fixtures/mdx-page: + specifiers: + '@astrojs/mdx': workspace:* + astro: workspace:* + dependencies: + '@astrojs/mdx': link:../../.. + astro: link:../../../../../astro + packages/integrations/netlify: specifiers: '@astrojs/webapi': ^0.12.0