Skip to content

Commit 2c511a4

Browse files
committedOct 21, 2023
Add support for baseUrl as a URL
1 parent a7bd79b commit 2c511a4

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed
 

‎packages/mdx/lib/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @property {SourceMapGenerator | null | undefined} [SourceMapGenerator]
1616
* Add a source map (object form) as the `map` field on the resulting file
1717
* (optional).
18-
* @property {string | null | undefined} [baseUrl]
18+
* @property {URL | string | null | undefined} [baseUrl]
1919
* Resolve `import`s (and `export … from`, and `import.meta.url`) from this
2020
* URL (optional, example: `import.meta.url`);
2121
* this option is useful when code will run in a different place, such as

‎packages/mdx/lib/plugin/recma-document.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declar
4646
* Transform.
4747
*/
4848
export function recmaDocument(options) {
49-
const baseUrl = options.baseUrl || undefined
49+
const baseUrl_ = options.baseUrl || undefined
50+
const baseUrl = typeof baseUrl_ === 'object' ? baseUrl_.href : baseUrl_
5051
const useDynamicImport = options.useDynamicImport || undefined
5152
const outputFormat = options.outputFormat || 'program'
5253
const pragma =

‎packages/mdx/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ Configuration for `createProcessor` (TypeScript type).
493493
```
494494

495495
</details>
496-
* `baseUrl` (`string`, optional, example: `import.meta.url`)
496+
* `baseUrl` (`URL` or `string`, optional, example: `import.meta.url`)
497497
resolve `import`s (and `export … from`, and `import.meta.url`) from this
498498
URL;
499499
this option is useful when code will run in a different place, such as when

‎packages/mdx/test/evaluate.js

+12
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ test('@mdx-js/mdx: evaluate', async function (t) {
360360
}
361361
)
362362

363+
await t.test(
364+
'should support rewriting `import.meta.url` w/ `baseUrl` as an URL',
365+
async function () {
366+
const mod = await evaluate(
367+
'export const x = new URL("example.png", import.meta.url).href',
368+
{baseUrl: new URL('https://example.com'), ...runtime}
369+
)
370+
371+
assert.equal(mod.x, 'https://example.com/example.png')
372+
}
373+
)
374+
363375
await t.test('should throw on an export all from', async function () {
364376
try {
365377
await evaluate('export * from "a"', runtime)

0 commit comments

Comments
 (0)
Please sign in to comment.