Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for file contents from other esbuild plugins #52

Merged
merged 4 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})