loader.js 931 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. const { compile } = require('./dist/index');
  2. // FIXME: we shouldn't be doing this, but we need it
  3. // for react MDX story definitions, e.g.
  4. //
  5. // <Story name="foo"><div>hi></div></Story>
  6. //
  7. // Which generates the code:
  8. //
  9. // export const foo = () => <div>hi</div>;
  10. const DEFAULT_RENDERER = `
  11. import React from 'react';
  12. `;
  13. // Lifted from MDXv1 loader
  14. // https://github.com/mdx-js/mdx/blob/v1/packages/loader/index.js
  15. //
  16. // Added
  17. // - webpack5 support
  18. // - MDX compiler built in
  19. const loader = async function (content) {
  20. const callback = this.async();
  21. const options = Object.assign({}, this.getOptions(), {
  22. filepath: this.resourcePath,
  23. });
  24. let result;
  25. try {
  26. result = await compile(content, options);
  27. } catch (err) {
  28. console.error('Error loading:', this.resourcePath)
  29. return callback(err);
  30. }
  31. const code = `${DEFAULT_RENDERER}\n${result}`;
  32. return callback(null, code);
  33. };
  34. module.exports = loader;