Skip to content

Commit

Permalink
Add support for baseUrl as a URL
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 21, 2023
1 parent a7bd79b commit 2c511a4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/mdx/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @property {SourceMapGenerator | null | undefined} [SourceMapGenerator]
* Add a source map (object form) as the `map` field on the resulting file
* (optional).
* @property {string | null | undefined} [baseUrl]
* @property {URL | string | null | undefined} [baseUrl]
* Resolve `import`s (and `export … from`, and `import.meta.url`) from this
* URL (optional, example: `import.meta.url`);
* this option is useful when code will run in a different place, such as
Expand Down
3 changes: 2 additions & 1 deletion packages/mdx/lib/plugin/recma-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declar
* Transform.
*/
export function recmaDocument(options) {
const baseUrl = options.baseUrl || undefined
const baseUrl_ = options.baseUrl || undefined
const baseUrl = typeof baseUrl_ === 'object' ? baseUrl_.href : baseUrl_
const useDynamicImport = options.useDynamicImport || undefined
const outputFormat = options.outputFormat || 'program'
const pragma =
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ Configuration for `createProcessor` (TypeScript type).
```

</details>
* `baseUrl` (`string`, optional, example: `import.meta.url`)
* `baseUrl` (`URL` or `string`, optional, example: `import.meta.url`)
resolve `import`s (and `export … from`, and `import.meta.url`) from this
URL;
this option is useful when code will run in a different place, such as when
Expand Down
12 changes: 12 additions & 0 deletions packages/mdx/test/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,18 @@ test('@mdx-js/mdx: evaluate', async function (t) {
}
)

await t.test(
'should support rewriting `import.meta.url` w/ `baseUrl` as an URL',
async function () {
const mod = await evaluate(
'export const x = new URL("example.png", import.meta.url).href',
{baseUrl: new URL('https://example.com'), ...runtime}
)

assert.equal(mod.x, 'https://example.com/example.png')
}
)

await t.test('should throw on an export all from', async function () {
try {
await evaluate('export * from "a"', runtime)
Expand Down

0 comments on commit 2c511a4

Please sign in to comment.