From 5c61f5778ced51fe59fbbdc936064d304acaf5aa Mon Sep 17 00:00:00 2001 From: Titus Date: Thu, 30 Dec 2021 18:37:23 +0100 Subject: [PATCH] Fix resolve base in esbuild loader (#1854) Closes GH-1821. Related-to: wooorm/xdm@db74ddc --- packages/esbuild/lib/index.js | 21 +++++++++++--- packages/esbuild/test/index.test.js | 45 ++++++++++++++++++++++++++--- packages/mdx/lib/condition.js | 2 +- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/packages/esbuild/lib/index.js b/packages/esbuild/lib/index.js index e6db896a6..f324dae4d 100644 --- a/packages/esbuild/lib/index.js +++ b/packages/esbuild/lib/index.js @@ -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' @@ -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) { @@ -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) + } } } } diff --git a/packages/esbuild/test/index.test.js b/packages/esbuild/test/index.test.js index 51c347eed..2d4eb60c7 100644 --- a/packages/esbuild/test/index.test.js +++ b/packages/esbuild/test/index.test.js @@ -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' + ) + 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') @@ -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' @@ -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' + 'import Content from "https://raw.githubusercontent.com/mdx-js/mdx/main/packages/esbuild/test/files/md-file.md"\n\n' ) await esbuild.build({ @@ -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' + 'import Content from "https://raw.githubusercontent.com/mdx-js/mdx/main/packages/esbuild/test/files/mdx-file-importing-markdown.mdx"\n\n' ) await esbuild.build({ diff --git a/packages/mdx/lib/condition.js b/packages/mdx/lib/condition.js index 5bf0a4465..974ab7d0c 100644 --- a/packages/mdx/lib/condition.js +++ b/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'