Skip to content

Commit

Permalink
Fix resolve base in esbuild loader (#1854)
Browse files Browse the repository at this point in the history
Closes GH-1821.
Related-to: wooorm/xdm@db74ddc
  • Loading branch information
wooorm committed Dec 30, 2021
1 parent cb49ac0 commit 5c61f57
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
21 changes: 17 additions & 4 deletions packages/esbuild/lib/index.js
Expand Up @@ -13,8 +13,10 @@
* @typedef {ProcessorOptions & {allowDangerousRemoteMdx?: boolean}} Options
*/

import {promises as fs} from 'fs'
import process from 'process'
import assert from 'node:assert'
import {promises as fs} from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import {URL} from 'url'
import got from 'got'
import {VFile} from 'vfile'
Expand Down Expand Up @@ -49,6 +51,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 @@ -205,9 +208,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()}
/* c8 ignore next 9 */
return {
contents: value,
errors,
warnings,
resolveDir: http.test(file.path)
? p.cwd()
: path.resolve(file.cwd, file.dirname)
}
}
}
}
45 changes: 41 additions & 4 deletions packages/esbuild/test/index.test.js
Expand Up @@ -45,6 +45,45 @@ test('@mdx-js/esbuild', async () => {
await fs.unlink(new URL('./esbuild.mdx', import.meta.url))
await fs.unlink(new URL('./esbuild.js', import.meta.url))

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

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

await fs.unlink(new URL('./esbuild-resolve.mdx', import.meta.url))
await fs.unlink(new URL('./esbuild-resolve.js', import.meta.url))
await fs.rmdir(new URL('./folder/', import.meta.url), {recursive: true})

// Markdown.
await fs.writeFile(new URL('./esbuild.md', import.meta.url), '\ta')

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

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

console.log('\nnote: the preceding errors and warnings are expected!\n')

await fs.writeFile(
new URL('./esbuild-plugin-crash.mdx', import.meta.url),
'# hi'
Expand Down Expand Up @@ -466,7 +503,7 @@ test('@mdx-js/esbuild', async () => {
// Remote markdown.
await fs.writeFile(
new URL('./esbuild-with-remote-md.mdx', import.meta.url),
'import Content from "https://raw.githubusercontent.com/wooorm/xdm/main/test/files/md-file.md"\n\n<Content />'
'import Content from "https://raw.githubusercontent.com/mdx-js/mdx/main/packages/esbuild/test/files/md-file.md"\n\n<Content />'
)

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

await esbuild.build({
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/lib/condition.js
@@ -1,3 +1,3 @@
import process from 'node:process'
import process from 'process'

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

1 comment on commit 5c61f57

@vercel
Copy link

@vercel vercel bot commented on 5c61f57 Dec 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.