@web/storybook-builder
Version:
Storybook builder powered by `@web/dev-server`
85 lines • 3.84 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__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 = rollupPluginMdx;
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) {
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', {
...mdxPluginOptions,
mdxCompileOptions: {
// this is done by Storybook in addon-docs in 2 different places:
// 1. in vite plugin (not run for our builder)
// 2. in webpack loader (not run for our builder)
// 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?.mdxCompileOptions,
rehypePlugins: [
...(mdxPluginOptions?.mdxCompileOptions?.rehypePlugins ?? []),
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();
},
};
}
//# sourceMappingURL=rollup-plugin-mdx.js.map