From 649c2f0849fa4f8e18e6eec754073d7d5ffba561 Mon Sep 17 00:00:00 2001 From: Adam Laycock Date: Mon, 10 May 2021 18:02:36 +0100 Subject: [PATCH] Add support for file contents from other esbuild plugins Closes GH-52. --- lib/integration/esbuild.js | 9 +++++++-- test/esbuild.js | 39 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/integration/esbuild.js b/lib/integration/esbuild.js index 429fbbf..172b32e 100644 --- a/lib/integration/esbuild.js +++ b/lib/integration/esbuild.js @@ -36,10 +36,15 @@ export function esbuild(options) { } /** - * @param {OnLoadArgs} data + * @param {Omit. & {pluginData?: {contents?: string}}} data */ async function onload(data) { - var doc = String(await fs.readFile(data.path)) + /** @type {string} */ + var doc = + data.pluginData && data.pluginData.contents + ? data.pluginData.contents + : String(await fs.readFile(data.path)) + var file = vfile({contents: doc, path: data.path}) /** @type {VFileMessage[]} */ var messages = [] diff --git a/test/esbuild.js b/test/esbuild.js index 39caae6..440effd 100644 --- a/test/esbuild.js +++ b/test/esbuild.js @@ -169,8 +169,7 @@ test('xdm (esbuild)', async function (t) { }, notes: [], pluginName: 'esbuild-xdm', - text: - 'Unexpected character `/` (U+002F) before local name, expected a character that can start a name, such as a letter, `$`, or `_` (note: to create a link in MDX, use `[text](url)`)' + text: 'Unexpected character `/` (U+002F) before local name, expected a character that can start a name, such as a letter, `$`, or `_` (note: to create a link in MDX, use `[text](url)`)' }, 'should pass errors' ) @@ -334,5 +333,41 @@ test('xdm (esbuild)', async function (t) { console.log('\nnote: the preceding errors and warnings are expected!\n') + /** @type import('esbuild').Plugin */ + const inlinePlugin = { + name: 'inline plugin', + setup: (build) => { + build.onResolve({filter: /index\.mdx/}, () => { + return { + path: path.join(process.cwd(), 'index.mdx'), + pluginData: { + contents: `# Test` + } + } + }) + } + } + + await esbuild.build({ + entryPoints: [path.join(process.cwd(), 'index.mdx')], + plugins: [inlinePlugin, esbuildXdm()], + define: {'process.env.NODE_ENV': '"development"'}, + format: 'esm', + bundle: true, + outfile: path.join(base, 'esbuild-compile-from-memory.js') + }) + + Content = + /** @ts-ignore file is dynamically generated */ + (await import('./context/esbuild-compile-from-memory.js')).default // type-coverage:ignore-line + + t.equal( + renderToStaticMarkup(React.createElement(Content)), + '

Test

', + 'should compile from `pluginData.content`' + ) + + await fs.unlink(path.join(base, 'esbuild-compile-from-memory.js')) + t.end() })