Skip to content

Commit db74ddc

Browse files
committed
Fix resolve base in esbuild loader
Related to mdx-js/mdx#1821.
1 parent ec01e18 commit db74ddc

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/integration/esbuild.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
* @typedef {ProcessorOptions & {allowDangerousRemoteMdx?: boolean}} Options
1414
*/
1515

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

5356
if (allowDangerousRemoteMdx) {
@@ -197,9 +200,19 @@ export function esbuild(options = {}) {
197200
})
198201
}
199202

203+
// Safety check: the file has a path, so there has to be a `dirname`.
204+
assert(file.dirname, 'expected `dirname` to be defined')
205+
200206
// V8 on Erbium.
201207
/* c8 ignore next 2 */
202-
return {contents: value, errors, warnings, resolveDir: p.cwd()}
208+
return {
209+
contents: value,
210+
errors,
211+
warnings,
212+
resolveDir: http.test(file.path)
213+
? p.cwd()
214+
: path.resolve(file.cwd, file.dirname)
215+
}
203216
}
204217
}
205218
}

test/esbuild.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,43 @@ test('xdm (esbuild)', async (t) => {
4747
await fs.unlink(path.join(base, 'esbuild.mdx'))
4848
await fs.unlink(path.join(base, 'esbuild.js'))
4949

50+
// Resolve directory.
51+
await fs.writeFile(
52+
path.join(base, 'esbuild-resolve.mdx'),
53+
'import Content from "./folder/file.mdx"\n\n<Content/>'
54+
)
55+
await fs.mkdir(path.join(base, 'folder'))
56+
await fs.writeFile(
57+
path.join(base, 'folder', 'file.mdx'),
58+
'import {data} from "./file.js"\n\n{data}'
59+
)
60+
await fs.writeFile(
61+
path.join(base, 'folder', 'file.js'),
62+
'export const data = 0.1'
63+
)
64+
await esbuild.build({
65+
bundle: true,
66+
define: {'process.env.NODE_ENV': '"development"'},
67+
entryPoints: [path.join(base, 'esbuild-resolve.mdx')],
68+
outfile: path.join(base, 'esbuild-resolve.js'),
69+
format: 'esm',
70+
plugins: [esbuildXdm()]
71+
})
72+
/** @type {MDXContent} */
73+
Content =
74+
/* @ts-expect-error file is dynamically generated */
75+
(await import('./context/esbuild-resolve.js')).default // type-coverage:ignore-line
76+
77+
t.equal(
78+
renderToStaticMarkup(React.createElement(Content)),
79+
'0.1',
80+
'should compile'
81+
)
82+
83+
await fs.unlink(path.join(base, 'esbuild-resolve.mdx'))
84+
await fs.unlink(path.join(base, 'esbuild-resolve.js'))
85+
await fs.rmdir(path.join(base, 'folder'), {recursive: true})
86+
5087
// Markdown.
5188
await fs.writeFile(path.join(base, 'esbuild.md'), '\ta')
5289

0 commit comments

Comments
 (0)