mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
|
// https://github.com/parcel-bundler/parcel/pull/7922#issuecomment-1750704973
|
||
|
|
||
|
import { compile } from '@mdx-js/mdx';
|
||
|
import { default as ThrowableDiagnostic } from '@parcel/diagnostic';
|
||
|
import { Transformer } from '@parcel/plugin';
|
||
|
|
||
|
export default new Transformer({
|
||
|
async transform({ asset }) {
|
||
|
const source = await asset.getCode();
|
||
|
|
||
|
let codeVFile;
|
||
|
|
||
|
try {
|
||
|
codeVFile = await compile(source, {
|
||
|
development: true,
|
||
|
jsx: true,
|
||
|
providerImportSource: '@mdx-js/react',
|
||
|
});
|
||
|
} catch (error) {
|
||
|
const { start, end } = error.position;
|
||
|
|
||
|
const highlight = {
|
||
|
message: error.reason,
|
||
|
start,
|
||
|
end,
|
||
|
};
|
||
|
|
||
|
if (!(end.line && end.column)) {
|
||
|
highlight.end = { ...start };
|
||
|
}
|
||
|
|
||
|
// Adjust for parser and reporter differences
|
||
|
highlight.start.column -= 1;
|
||
|
highlight.end.column -= 1;
|
||
|
|
||
|
throw new ThrowableDiagnostic({
|
||
|
diagnostic: {
|
||
|
message: 'Unable to compile MDX',
|
||
|
codeFrames: [
|
||
|
{
|
||
|
filePath: asset.filePath,
|
||
|
code: source,
|
||
|
codeHighlights: [highlight],
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
|
||
|
const code = String(codeVFile);
|
||
|
|
||
|
asset.type = 'jsx';
|
||
|
asset.setCode(code);
|
||
|
|
||
|
return [asset];
|
||
|
},
|
||
|
});
|