Skip to content

Commit

Permalink
Add support for file contents from other esbuild plugins
Browse files Browse the repository at this point in the history
Closes GH-52.
  • Loading branch information
Arcath authored May 10, 2021
1 parent 9cdb4ac commit 649c2f0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/integration/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ export function esbuild(options) {
}

/**
* @param {OnLoadArgs} data
* @param {Omit.<OnLoadArgs, 'pluginData'> & {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 = []
Expand Down
39 changes: 37 additions & 2 deletions test/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down Expand Up @@ -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)),
'<h1>Test</h1>',
'should compile from `pluginData.content`'
)

await fs.unlink(path.join(base, 'esbuild-compile-from-memory.js'))

t.end()
})

0 comments on commit 649c2f0

Please sign in to comment.