Skip to content

Commit

Permalink
Fix resolve base in esbuild loader
Browse files Browse the repository at this point in the history
Related to mdx-js/mdx#1821.
  • Loading branch information
wooorm committed Nov 19, 2021
1 parent ec01e18 commit db74ddc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/integration/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
* @typedef {ProcessorOptions & {allowDangerousRemoteMdx?: boolean}} Options
*/

import assert from 'node:assert'
import {promises as fs} from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import got from 'got'
import {VFile} from 'vfile'
Expand Down Expand Up @@ -48,6 +50,7 @@ export function esbuild(options = {}) {
const filter = extnamesToRegex(extnames)
/* eslint-disable-next-line security/detect-non-literal-regexp */
const filterHttp = new RegExp('^https?:\\/{2}.+' + filter.source)
const http = /^https?:\/{2}/
const filterHttpOrRelative = /^(https?:\/{2}|.{1,2}\/).*/

if (allowDangerousRemoteMdx) {
Expand Down Expand Up @@ -197,9 +200,19 @@ export function esbuild(options = {}) {
})
}

// Safety check: the file has a path, so there has to be a `dirname`.
assert(file.dirname, 'expected `dirname` to be defined')

// V8 on Erbium.
/* c8 ignore next 2 */
return {contents: value, errors, warnings, resolveDir: p.cwd()}
return {
contents: value,
errors,
warnings,
resolveDir: http.test(file.path)
? p.cwd()
: path.resolve(file.cwd, file.dirname)
}
}
}
}
37 changes: 37 additions & 0 deletions test/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,43 @@ test('xdm (esbuild)', async (t) => {
await fs.unlink(path.join(base, 'esbuild.mdx'))
await fs.unlink(path.join(base, 'esbuild.js'))

// Resolve directory.
await fs.writeFile(
path.join(base, 'esbuild-resolve.mdx'),
'import Content from "./folder/file.mdx"\n\n<Content/>'
)
await fs.mkdir(path.join(base, 'folder'))
await fs.writeFile(
path.join(base, 'folder', 'file.mdx'),
'import {data} from "./file.js"\n\n{data}'
)
await fs.writeFile(
path.join(base, 'folder', 'file.js'),
'export const data = 0.1'
)
await esbuild.build({
bundle: true,
define: {'process.env.NODE_ENV': '"development"'},
entryPoints: [path.join(base, 'esbuild-resolve.mdx')],
outfile: path.join(base, 'esbuild-resolve.js'),
format: 'esm',
plugins: [esbuildXdm()]
})
/** @type {MDXContent} */
Content =
/* @ts-expect-error file is dynamically generated */
(await import('./context/esbuild-resolve.js')).default // type-coverage:ignore-line

t.equal(
renderToStaticMarkup(React.createElement(Content)),
'0.1',
'should compile'
)

await fs.unlink(path.join(base, 'esbuild-resolve.mdx'))
await fs.unlink(path.join(base, 'esbuild-resolve.js'))
await fs.rmdir(path.join(base, 'folder'), {recursive: true})

// Markdown.
await fs.writeFile(path.join(base, 'esbuild.md'), '\ta')

Expand Down

0 comments on commit db74ddc

Please sign in to comment.