Skip to content

Commit 5c61f57

Browse files
authoredDec 30, 2021
Fix resolve base in esbuild loader (#1854)
Closes GH-1821. Related-to: wooorm/xdm@db74ddc
1 parent cb49ac0 commit 5c61f57

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed
 

‎packages/esbuild/lib/index.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
* @typedef {ProcessorOptions & {allowDangerousRemoteMdx?: boolean}} Options
1414
*/
1515

16-
import {promises as fs} from 'fs'
17-
import process from 'process'
16+
import assert from 'node:assert'
17+
import {promises as fs} from 'node:fs'
18+
import path from 'node:path'
19+
import process from 'node:process'
1820
import {URL} from 'url'
1921
import got from 'got'
2022
import {VFile} from 'vfile'
@@ -49,6 +51,7 @@ export function esbuild(options = {}) {
4951
const filter = extnamesToRegex(extnames)
5052
/* eslint-disable-next-line security/detect-non-literal-regexp */
5153
const filterHttp = new RegExp('^https?:\\/{2}.+' + filter.source)
54+
const http = /^https?:\/{2}/
5255
const filterHttpOrRelative = /^(https?:\/{2}|.{1,2}\/).*/
5356

5457
if (allowDangerousRemoteMdx) {
@@ -205,9 +208,19 @@ export function esbuild(options = {}) {
205208
})
206209
}
207210

211+
// Safety check: the file has a path, so there has to be a `dirname`.
212+
assert(file.dirname, 'expected `dirname` to be defined')
213+
208214
// V8 on Erbium.
209-
/* c8 ignore next 2 */
210-
return {contents: value, errors, warnings, resolveDir: p.cwd()}
215+
/* c8 ignore next 9 */
216+
return {
217+
contents: value,
218+
errors,
219+
warnings,
220+
resolveDir: http.test(file.path)
221+
? p.cwd()
222+
: path.resolve(file.cwd, file.dirname)
223+
}
211224
}
212225
}
213226
}

‎packages/esbuild/test/index.test.js

+41-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,45 @@ test('@mdx-js/esbuild', async () => {
4545
await fs.unlink(new URL('./esbuild.mdx', import.meta.url))
4646
await fs.unlink(new URL('./esbuild.js', import.meta.url))
4747

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

@@ -337,8 +376,6 @@ test('@mdx-js/esbuild', async () => {
337376

338377
await fs.unlink(new URL('./esbuild-warnings.mdx', import.meta.url))
339378

340-
console.log('\nnote: the preceding errors and warnings are expected!\n')
341-
342379
await fs.writeFile(
343380
new URL('./esbuild-plugin-crash.mdx', import.meta.url),
344381
'# hi'
@@ -466,7 +503,7 @@ test('@mdx-js/esbuild', async () => {
466503
// Remote markdown.
467504
await fs.writeFile(
468505
new URL('./esbuild-with-remote-md.mdx', import.meta.url),
469-
'import Content from "https://raw.githubusercontent.com/wooorm/xdm/main/test/files/md-file.md"\n\n<Content />'
506+
'import Content from "https://raw.githubusercontent.com/mdx-js/mdx/main/packages/esbuild/test/files/md-file.md"\n\n<Content />'
470507
)
471508

472509
await esbuild.build({
@@ -498,7 +535,7 @@ test('@mdx-js/esbuild', async () => {
498535
// Remote MDX importing more markdown.
499536
await fs.writeFile(
500537
new URL('./esbuild-with-remote-mdx.mdx', import.meta.url),
501-
'import Content from "https://raw.githubusercontent.com/wooorm/xdm/main/test/files/mdx-file-importing-markdown.mdx"\n\n<Content />'
538+
'import Content from "https://raw.githubusercontent.com/mdx-js/mdx/main/packages/esbuild/test/files/mdx-file-importing-markdown.mdx"\n\n<Content />'
502539
)
503540

504541
await esbuild.build({

‎packages/mdx/lib/condition.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import process from 'node:process'
1+
import process from 'process'
22

33
export const development = process.env.NODE_ENV === 'development'

1 commit comments

Comments
 (1)

vercel[bot] commented on Dec 30, 2021

@vercel[bot]

Successfully deployed to the following URLs:

Please sign in to comment.