@web/storybook-builder
Version:
Storybook builder powered by `@web/dev-server`
48 lines • 2.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rollupPluginMdx = void 0;
const mdx_1 = require("@mdx-js/mdx");
const promises_1 = require("node:fs/promises");
const node_path_1 = require("node:path");
const rehype_external_links_1 = __importDefault(require("rehype-external-links"));
const rehype_slug_1 = __importDefault(require("rehype-slug"));
function rollupPluginMdx(options) {
let mdxPluginOptions;
let jsxOptions;
return {
name: 'rollup-plugin-mdx',
async buildStart() {
({ mdxPluginOptions, jsxOptions } = await options.presets.apply('options', {}));
},
async resolveId(id) {
if (id.endsWith('.mdx.js')) {
return id;
}
},
async load(id) {
var _a, _b;
if (!id.endsWith('.mdx.js'))
return;
const mdxPath = id.replace(/\.js$/, '');
const mdxCode = await (0, promises_1.readFile)(mdxPath.split('/').join(node_path_1.sep), { encoding: 'utf8' });
const mdxLoaderOptions = await options.presets.apply('mdxLoaderOptions', Object.assign(Object.assign({}, mdxPluginOptions), { mdxCompileOptions: Object.assign(Object.assign({
// this is done by Storybook in 3 different places:
// 1. addon-essentials preset (not working for users who install addon-docs directly without addon-essentials)
// 2. addon-docs vite plugin (not run for our builder, because it's in the builder-vite hook)
// 3. addon-docs webpack loader (not run for our builder, because it's in the builder-webpack hook)
// so we need to duplicate same logic here
providerImportSource: (0, node_path_1.join)((0, node_path_1.dirname)(require.resolve('@storybook/addon-docs/package.json')), '/dist/shims/mdx-react-shim.mjs') }, mdxPluginOptions === null || mdxPluginOptions === void 0 ? void 0 : mdxPluginOptions.mdxCompileOptions), { rehypePlugins: [
...((_b = (_a = mdxPluginOptions === null || mdxPluginOptions === void 0 ? void 0 : mdxPluginOptions.mdxCompileOptions) === null || _a === void 0 ? void 0 : _a.rehypePlugins) !== null && _b !== void 0 ? _b : []),
rehype_slug_1.default,
rehype_external_links_1.default,
] }), jsxOptions }));
const mdxResult = await (0, mdx_1.compile)(mdxCode, mdxLoaderOptions.mdxCompileOptions);
return mdxResult.toString();
},
};
}
exports.rollupPluginMdx = rollupPluginMdx;
//# sourceMappingURL=rollup-plugin-mdx.js.map