aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/dull-radios-sparkle.md6
-rw-r--r--packages/astro/src/core/config.ts82
-rw-r--r--packages/integrations/mdx/src/index.ts2
-rw-r--r--packages/integrations/mdx/test/fixtures/mdx-page/astro.config.ts5
-rw-r--r--packages/integrations/mdx/test/fixtures/mdx-page/package.json7
-rw-r--r--packages/integrations/mdx/test/mdx-page.test.js3
-rw-r--r--pnpm-lock.yaml8
7 files changed, 80 insertions, 33 deletions
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<Open
);
}
- // Automatically load config file using Proload
- // If `userConfigPath` is `undefined`, Proload will search for `astro.config.[cm]?[jt]s`
- let config;
- try {
- config = await load('astro', {
- mustExist: !!userConfigPath,
- cwd: root,
- filePath: userConfigPath,
- });
- } catch (err) {
- if (err instanceof ProloadError && flags.config) {
- throw new Error(`Unable to resolve --config "${flags.config}"! Does the file exist?`);
- }
- throw err;
- }
+ const config = await tryLoadConfig(configOptions, flags, userConfigPath, root);
if (config) {
userConfig = config.value;
userConfigPath = config.filePath;
@@ -483,6 +473,52 @@ export async function openConfig(configOptions: LoadConfigOptions): Promise<Open
};
}
+interface TryLoadConfigResult {
+ value: Record<string, any>;
+ filePath?: string;
+}
+
+async function tryLoadConfig(configOptions: LoadConfigOptions, flags: CLIFlags, userConfigPath: string | undefined, root: string): Promise<TryLoadConfigResult | undefined> {
+ 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<Astr
);
}
- // Automatically load config file using Proload
- // If `userConfigPath` is `undefined`, Proload will search for `astro.config.[cm]?[jt]s`
- let config;
- try {
- config = await load('astro', {
- mustExist: !!userConfigPath,
- cwd: root,
- filePath: userConfigPath,
- });
- } catch (err) {
- if (err instanceof ProloadError && flags.config) {
- throw new Error(`Unable to resolve --config "${flags.config}"! Does the file exist?`);
- }
- throw err;
- }
+ const config = await tryLoadConfig(configOptions, flags, userConfigPath, root);
if (config) {
userConfig = config.value;
}
diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts
index 139e08515..81187cc60 100644
--- a/packages/integrations/mdx/src/index.ts
+++ b/packages/integrations/mdx/src/index.ts
@@ -52,7 +52,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
// Workarounds tried:
// - "import * as remarkShikiTwoslash"
// - "import { default as remarkShikiTwoslash }"
- (remarkShikiTwoslash as any).default,
+ (remarkShikiTwoslash as any).default ?? remarkShikiTwoslash,
config.markdown.shikiConfig,
]);
rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
diff --git a/packages/integrations/mdx/test/fixtures/mdx-page/astro.config.ts b/packages/integrations/mdx/test/fixtures/mdx-page/astro.config.ts
new file mode 100644
index 000000000..f1d5e8bd7
--- /dev/null
+++ b/packages/integrations/mdx/test/fixtures/mdx-page/astro.config.ts
@@ -0,0 +1,5 @@
+import mdx from '@astrojs/mdx';
+
+export default {
+ integrations: [mdx()]
+}
diff --git a/packages/integrations/mdx/test/fixtures/mdx-page/package.json b/packages/integrations/mdx/test/fixtures/mdx-page/package.json
new file mode 100644
index 000000000..c8f3217b3
--- /dev/null
+++ b/packages/integrations/mdx/test/fixtures/mdx-page/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "@test/mdx-page",
+ "dependencies": {
+ "astro": "workspace:*",
+ "@astrojs/mdx": "workspace:*"
+ }
+}
diff --git a/packages/integrations/mdx/test/mdx-page.test.js b/packages/integrations/mdx/test/mdx-page.test.js
index e375a9f17..487923d42 100644
--- a/packages/integrations/mdx/test/mdx-page.test.js
+++ b/packages/integrations/mdx/test/mdx-page.test.js
@@ -9,8 +9,7 @@ describe('MDX Page', () => {
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