From b31bc348d9ff3753274ea0f925c0792fc99a6ac4 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 19 Jul 2022 15:42:13 -0400 Subject: [PATCH 1/7] deps: add es-module-lexer --- packages/integrations/mdx/package.json | 7 ++++--- pnpm-lock.yaml | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/integrations/mdx/package.json b/packages/integrations/mdx/package.json index 1268f80bd2e1..9ea034634c43 100644 --- a/packages/integrations/mdx/package.json +++ b/packages/integrations/mdx/package.json @@ -30,7 +30,8 @@ "test": "mocha --exit --timeout 20000" }, "dependencies": { - "@mdx-js/rollup": "^2.1.1" + "@mdx-js/rollup": "^2.1.1", + "es-module-lexer": "^0.10.5" }, "devDependencies": { "@types/chai": "^4.3.1", @@ -39,8 +40,8 @@ "astro": "workspace:*", "astro-scripts": "workspace:*", "chai": "^4.3.6", - "mocha": "^9.2.2", - "linkedom": "^0.14.12" + "linkedom": "^0.14.12", + "mocha": "^9.2.2" }, "engines": { "node": "^14.18.0 || >=16.12.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8bcedfd28cf8..38df67c29af4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2098,10 +2098,12 @@ importers: astro: workspace:* astro-scripts: workspace:* chai: ^4.3.6 + es-module-lexer: ^0.10.5 linkedom: ^0.14.12 mocha: ^9.2.2 dependencies: '@mdx-js/rollup': 2.1.2 + es-module-lexer: 0.10.5 devDependencies: '@types/chai': 4.3.1 '@types/mocha': 9.1.1 From 7d225422e6b335542d2152b34fd4af571353488a Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 19 Jul 2022 15:42:24 -0400 Subject: [PATCH 2/7] feat: inject url export on mdx files --- packages/integrations/mdx/src/index.ts | 16 ++++++++++++---- packages/integrations/mdx/src/utils.ts | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 packages/integrations/mdx/src/utils.ts diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index bd3d50c862d5..141139dcbb8f 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -1,11 +1,13 @@ -import mdxPlugin from '@mdx-js/rollup'; import type { AstroIntegration } from 'astro'; +import mdxPlugin from '@mdx-js/rollup'; +import { parse as parseESM } from 'es-module-lexer'; +import { getFileInfo } from './utils.js'; export default function mdx(): AstroIntegration { return { name: '@astrojs/mdx', hooks: { - 'astro:config:setup': ({ updateConfig, addPageExtension, command }: any) => { + 'astro:config:setup': ({ updateConfig, config, addPageExtension, command }: any) => { addPageExtension('.mdx'); updateConfig({ vite: { @@ -24,10 +26,16 @@ export default function mdx(): AstroIntegration { name: '@astrojs/mdx', transform(code: string, id: string) { if (!id.endsWith('.mdx')) return; + + const [, moduleExports] = parseESM(code); + if (!moduleExports.includes('url')) { + const { fileUrl } = getFileInfo(id, config); + code += `export const url = ${JSON.stringify(fileUrl)};`; + } // TODO: decline HMR updates until we have a stable approach return `${code}\nif (import.meta.hot) { - import.meta.hot.decline(); - }`; + import.meta.hot.decline(); + }`; }, }, ], diff --git a/packages/integrations/mdx/src/utils.ts b/packages/integrations/mdx/src/utils.ts new file mode 100644 index 000000000000..1091e851173a --- /dev/null +++ b/packages/integrations/mdx/src/utils.ts @@ -0,0 +1,21 @@ +import type { AstroConfig } from 'astro'; + +function appendForwardSlash(path: string) { + return path.endsWith('/') ? path : path + '/'; +} + +/** @see 'vite-plugin-utils' for source */ +export function getFileInfo(id: string, config: AstroConfig) { + const sitePathname = appendForwardSlash( + config.site ? new URL(config.base, config.site).pathname : config.base + ); + + const fileId = id.split('?')[0]; + let fileUrl = fileId.includes('/pages/') + ? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, '') + : undefined; + if (fileUrl && config.trailingSlash === 'always') { + fileUrl = appendForwardSlash(fileUrl); + } + return { fileId, fileUrl }; +} From c25a257bd19afa1c6ae2a92c8ebc7abebd72d6d0 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 19 Jul 2022 15:59:28 -0400 Subject: [PATCH 3/7] fix: apply url transform in prod --- packages/integrations/mdx/src/index.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 141139dcbb8f..8361b2a2ffb8 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -22,20 +22,23 @@ export default function mdx(): AstroIntegration { mdExtensions: [], }), }, - command === 'dev' && { + { name: '@astrojs/mdx', transform(code: string, id: string) { if (!id.endsWith('.mdx')) return; - const [, moduleExports] = parseESM(code); + if (!moduleExports.includes('url')) { const { fileUrl } = getFileInfo(id, config); code += `export const url = ${JSON.stringify(fileUrl)};`; } - // TODO: decline HMR updates until we have a stable approach - return `${code}\nif (import.meta.hot) { - import.meta.hot.decline(); - }`; + if (command === 'dev') { + // TODO: decline HMR updates until we have a stable approach + code += `\nif (import.meta.hot) { + import.meta.hot.decline(); + }` + } + return code; }, }, ], From ee9b15bdf5bea3b4a60119b86603634762e6fabe Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 19 Jul 2022 15:59:45 -0400 Subject: [PATCH 4/7] test: page urls with overrides --- .../mdx-url-export/src/pages/pages.json.js | 9 ++++++ .../mdx-url-export/src/pages/test-1.mdx | 1 + .../mdx-url-export/src/pages/test-2.mdx | 1 + .../src/pages/with-url-override.mdx | 3 ++ .../mdx/test/mdx-component.test.js | 2 +- .../integrations/mdx/test/mdx-page.test.js | 2 +- .../integrations/mdx/test/mdx-url-export.js | 28 +++++++++++++++++++ 7 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/pages.json.js create mode 100644 packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-1.mdx create mode 100644 packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-2.mdx create mode 100644 packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/with-url-override.mdx create mode 100644 packages/integrations/mdx/test/mdx-url-export.js diff --git a/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/pages.json.js b/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/pages.json.js new file mode 100644 index 000000000000..36b6f7af69aa --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/pages.json.js @@ -0,0 +1,9 @@ +export async function get() { + const mdxPages = await import.meta.glob('./*.mdx', { eager: true }); + + return { + body: JSON.stringify({ + urls: Object.values(mdxPages ?? {}).map(v => v?.url), + }) + } +} diff --git a/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-1.mdx b/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-1.mdx new file mode 100644 index 000000000000..c9b984787ff2 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-1.mdx @@ -0,0 +1 @@ +# I'm a page with a url of "/test-1!" diff --git a/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-2.mdx b/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-2.mdx new file mode 100644 index 000000000000..360f72fc351a --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/test-2.mdx @@ -0,0 +1 @@ +# I'm a page with a url of "/test-2!" diff --git a/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/with-url-override.mdx b/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/with-url-override.mdx new file mode 100644 index 000000000000..e9961928b6d4 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-url-export/src/pages/with-url-override.mdx @@ -0,0 +1,3 @@ +export const url = '/AH!' + +# I'm a test with a url override! diff --git a/packages/integrations/mdx/test/mdx-component.test.js b/packages/integrations/mdx/test/mdx-component.test.js index 1a610be05985..eab21b726868 100644 --- a/packages/integrations/mdx/test/mdx-component.test.js +++ b/packages/integrations/mdx/test/mdx-component.test.js @@ -4,7 +4,7 @@ import { expect } from 'chai'; import { parseHTML } from 'linkedom'; import { loadFixture } from '../../../astro/test/test-utils.js'; -describe('MDX Component', () => { +describe.skip('MDX Component', () => { let fixture; before(async () => { diff --git a/packages/integrations/mdx/test/mdx-page.test.js b/packages/integrations/mdx/test/mdx-page.test.js index e375a9f175eb..edf6e7f2484a 100644 --- a/packages/integrations/mdx/test/mdx-page.test.js +++ b/packages/integrations/mdx/test/mdx-page.test.js @@ -4,7 +4,7 @@ import { expect } from 'chai'; import { parseHTML } from 'linkedom'; import { loadFixture } from '../../../astro/test/test-utils.js'; -describe('MDX Page', () => { +describe.skip('MDX Page', () => { let fixture; before(async () => { diff --git a/packages/integrations/mdx/test/mdx-url-export.js b/packages/integrations/mdx/test/mdx-url-export.js new file mode 100644 index 000000000000..76d6709f0257 --- /dev/null +++ b/packages/integrations/mdx/test/mdx-url-export.js @@ -0,0 +1,28 @@ +import mdx from '@astrojs/mdx'; + +import { expect } from 'chai'; +import { loadFixture } from '../../../astro/test/test-utils.js'; + +describe('MDX url export', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/mdx-url-export/', import.meta.url), + integrations: [mdx()], + }); + + await fixture.build(); + }); + + it('generates correct urls in glob result', async () => { + const { urls } = JSON.parse(await fixture.readFile('/pages.json')); + expect(urls).to.include('/test-1'); + expect(urls).to.include('/test-2'); + }); + + it('respects "export url" overrides in glob result', async () => { + const { urls } = JSON.parse(await fixture.readFile('/pages.json')); + expect(urls).to.include('/AH!'); + }); +}); From bf4b5d3b914e548fa1a5ee87c6afca2f2964347d Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 19 Jul 2022 17:07:34 -0400 Subject: [PATCH 5/7] fix: revert test skips --- packages/integrations/mdx/test/mdx-component.test.js | 2 +- packages/integrations/mdx/test/mdx-page.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/integrations/mdx/test/mdx-component.test.js b/packages/integrations/mdx/test/mdx-component.test.js index eab21b726868..1a610be05985 100644 --- a/packages/integrations/mdx/test/mdx-component.test.js +++ b/packages/integrations/mdx/test/mdx-component.test.js @@ -4,7 +4,7 @@ import { expect } from 'chai'; import { parseHTML } from 'linkedom'; import { loadFixture } from '../../../astro/test/test-utils.js'; -describe.skip('MDX Component', () => { +describe('MDX Component', () => { let fixture; before(async () => { diff --git a/packages/integrations/mdx/test/mdx-page.test.js b/packages/integrations/mdx/test/mdx-page.test.js index edf6e7f2484a..e375a9f175eb 100644 --- a/packages/integrations/mdx/test/mdx-page.test.js +++ b/packages/integrations/mdx/test/mdx-page.test.js @@ -4,7 +4,7 @@ import { expect } from 'chai'; import { parseHTML } from 'linkedom'; import { loadFixture } from '../../../astro/test/test-utils.js'; -describe.skip('MDX Page', () => { +describe('MDX Page', () => { let fixture; before(async () => { From 1231dbe5e8708154251ee2c3ed58f3c7f3218c94 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 19 Jul 2022 17:11:08 -0400 Subject: [PATCH 6/7] chore: changeset --- .changeset/shiny-pumpkins-return.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/shiny-pumpkins-return.md diff --git a/.changeset/shiny-pumpkins-return.md b/.changeset/shiny-pumpkins-return.md new file mode 100644 index 000000000000..6482832a5110 --- /dev/null +++ b/.changeset/shiny-pumpkins-return.md @@ -0,0 +1,5 @@ +--- +'@astrojs/mdx': patch +--- + +Include page url in MDX glob result From 5541549a8157e75dfd770b57e0adb5406f60064e Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 20 Jul 2022 10:15:52 -0400 Subject: [PATCH 7/7] fix: add newline before export --- packages/integrations/mdx/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 8361b2a2ffb8..7b4a24ff53ef 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -30,7 +30,7 @@ export default function mdx(): AstroIntegration { if (!moduleExports.includes('url')) { const { fileUrl } = getFileInfo(id, config); - code += `export const url = ${JSON.stringify(fileUrl)};`; + code += `\nexport const url = ${JSON.stringify(fileUrl)};`; } if (command === 'dev') { // TODO: decline HMR updates until we have a stable approach