From db74ddcf908fd3bb6b94428575a05a4f87ea6029 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 19 Nov 2021 09:34:53 +0100 Subject: [PATCH] Fix resolve base in esbuild loader Related to mdx-js/mdx#1821. --- lib/integration/esbuild.js | 15 ++++++++++++++- test/esbuild.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/integration/esbuild.js b/lib/integration/esbuild.js index c935775..d0db52a 100644 --- a/lib/integration/esbuild.js +++ b/lib/integration/esbuild.js @@ -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' @@ -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) { @@ -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) + } } } } diff --git a/test/esbuild.js b/test/esbuild.js index 382ee4f..7d0fe39 100644 --- a/test/esbuild.js +++ b/test/esbuild.js @@ -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' + ) + 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')