@web/storybook-builder
Version:
Storybook builder powered by `@web/dev-server`
71 lines • 3.94 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 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 { compile } = await Promise.resolve().then(() => __importStar(require('@mdx-js/mdx'))); // for CJS compatibility
const mdxResult = await compile(mdxCode, mdxLoaderOptions.mdxCompileOptions);
return mdxResult.toString();
},
};
}
exports.rollupPluginMdx = rollupPluginMdx;
//# sourceMappingURL=rollup-plugin-mdx.js.map