UNPKG

@modern-js/plugin-rspress

Version:

A Modern.js plugin to integrate rspress

304 lines (303 loc) • 12.2 kB
"use strict"; var __webpack_modules__ = { "@rspress/core": function(module) { module.exports = import("@rspress/core"); } }; /************************************************************************/ // The module cache var __webpack_module_cache__ = {}; // The require function function __webpack_require__(moduleId) { // Check if module is in cache var cachedModule = __webpack_module_cache__[moduleId]; if (void 0 !== cachedModule) return cachedModule.exports; // Create a new module (and put it into the cache) var module = __webpack_module_cache__[moduleId] = { exports: {} }; // Execute the module function __webpack_modules__[moduleId](module, module.exports, __webpack_require__); // Return the exports of the module return module.exports; } /************************************************************************/ // webpack/runtime/compat_get_default_export (()=>{ // getDefaultExport function for compatibility with non-ESM modules __webpack_require__.n = function(module) { var getter = module && module.__esModule ? function() { return module['default']; } : function() { return module; }; __webpack_require__.d(getter, { a: getter }); return getter; }; })(); // webpack/runtime/define_property_getters (()=>{ __webpack_require__.d = function(exports1, definition) { for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { enumerable: true, get: definition[key] }); }; })(); // webpack/runtime/has_own_property (()=>{ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }; })(); // webpack/runtime/make_namespace_object (()=>{ // define __esModule on exports __webpack_require__.r = function(exports1) { if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { value: 'Module' }); Object.defineProperty(exports1, '__esModule', { value: true }); }; })(); /************************************************************************/ var __webpack_exports__ = {}; // ESM COMPAT FLAG __webpack_require__.r(__webpack_exports__); // EXPORTS __webpack_require__.d(__webpack_exports__, { modulePluginDoc: ()=>/* binding */ modulePluginDoc, default: ()=>/* binding */ src }); const external_node_path_namespaceObject = require("node:path"); const external_node_fs_namespaceObject = require("node:fs"); var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject); const external_fast_glob_namespaceObject = require("fast-glob"); var external_fast_glob_default = /*#__PURE__*/ __webpack_require__.n(external_fast_glob_namespaceObject); const plugin_preview_namespaceObject = require("@rspress/plugin-preview"); const plugin_api_docgen_namespaceObject = require("@rspress/plugin-api-docgen"); const external_lodash_namespaceObject = require("lodash"); var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject); const mergeModuleDocConfig = (...configs)=>external_lodash_default().mergeWith({}, ...configs, (target, source, key)=>{ const pair = [ target, source ]; if (pair.some(external_lodash_default().isUndefined)) // fallback to lodash default merge behavior return; // always use source override target, if source defined. if ([ 'sidebar', 'locales' ].includes(key)) return source ?? target; if (pair.some(external_lodash_default().isArray)) return [ ...external_lodash_default().castArray(target), ...external_lodash_default().castArray(source) ]; // convert function to chained function if (pair.some(external_lodash_default().isFunction)) return [ target, source ]; }); async function launchDoc({ appDir, isProduction, pluginOptions }) { var _doc_themeConfig_locales, _doc_themeConfig, _doc_locales; const { doc = {}, iframeOptions, iframePosition, defaultRenderMode, entries, apiParseTool, parseToolOptions } = pluginOptions || {}; // TODO: remove let previewMode = null == pluginOptions ? void 0 : pluginOptions.previewMode; if ('web' === previewMode) previewMode = 'internal'; if ('mobile' === previewMode) previewMode = 'iframe'; const json = JSON.parse(external_node_fs_default().readFileSync((0, external_node_path_namespaceObject.resolve)(appDir, './package.json'), 'utf8')); const root = (0, external_node_path_namespaceObject.resolve)(appDir, doc.root ?? 'docs'); const { dev, build } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "@rspress/core")); const languages = (null === (_doc_themeConfig = doc.themeConfig) || void 0 === _doc_themeConfig ? void 0 : null === (_doc_themeConfig_locales = _doc_themeConfig.locales) || void 0 === _doc_themeConfig_locales ? void 0 : _doc_themeConfig_locales.map((locale)=>locale.lang)) || (null === (_doc_locales = doc.locales) || void 0 === _doc_locales ? void 0 : _doc_locales.map((locale)=>locale.lang)) || [ '' ]; const defaultLanguage = doc.lang || ''; const sidebar = {}; let haveMetaJson = false; const getAutoSidebarGroup = async (lang, prefix)=>{ const base = (0, external_node_path_namespaceObject.join)(root, lang); const source = [ '*.(js|jsx|ts|tsx|md|mdx|json)' ]; const traverse = async (cwd)=>{ // FIXME: win32 const [files, directories] = await Promise.all([ external_fast_glob_default()(source, { cwd, onlyFiles: true, ignore: [ 'index.*' ] }), external_fast_glob_default()([ '*' ], { cwd, onlyDirectories: true, ignore: [ 'public' ] }) ]); // files --> string[] if (files.filter((file)=>'_meta.json' === file).length > 0) { haveMetaJson = true; return []; } const fileItems = files.map((file)=>{ const link = `/${(0, external_node_path_namespaceObject.relative)(base, (0, external_node_path_namespaceObject.join)(cwd, file)).replace(/\.[^.]+$/, '')}`; return prefix + link; }); // dir --> SidebarGroup[] const directoryItems = await Promise.all(directories.map(async (directory)=>{ const directoryCwd = (0, external_node_path_namespaceObject.join)(cwd, directory); const hasIndex = (await external_fast_glob_default()([ 'index.*' ], { cwd: directoryCwd, onlyFiles: true })).length > 0; const link = `${prefix}/${(0, external_node_path_namespaceObject.relative)(base, directoryCwd)}/`; const items = await traverse(directoryCwd); const text = directory[0].toUpperCase() + directory.slice(1); if (hasIndex) return { link, collapsible: items.length > 0, items, text }; return { collapsible: items.length > 0, items, text }; })); return [ ...fileItems, ...directoryItems ]; }; return { text: 'Module List', link: `${prefix}/`, collapsible: false, items: await traverse(base) }; }; await Promise.all(languages.map(async (lang)=>{ const havePrefix = lang && lang !== defaultLanguage; const prefix = havePrefix ? `/${lang}` : ''; const sidebarGroup = await getAutoSidebarGroup(lang, prefix); sidebar[`${prefix}/`] = [ sidebarGroup ]; })); const modernDocConfig = mergeModuleDocConfig({ root, title: json.name, globalStyles: (0, external_node_path_namespaceObject.join)(__dirname, '..', 'static', 'global-styles', 'index.css'), themeConfig: { darkMode: false, sidebar: haveMetaJson ? void 0 : sidebar }, markdown: { mdxRs: false, globalComponents: [ (0, external_node_path_namespaceObject.join)(__dirname, '..', 'static', 'global-components', 'Overview.tsx') ] }, builderConfig: { resolve: { alias: { 'rspress/runtime': '@rspress/core/runtime', 'rspress/theme': '@rspress/core/theme', '@rspress/core': (0, external_node_path_namespaceObject.join)(require.resolve('@rspress/core/package.json'), '..') } } }, head: [ ` <script> window.MODERN_THEME = 'light'; </script> ` ], plugins: [ (0, plugin_preview_namespaceObject.pluginPreview)({ previewMode, iframePosition, iframeOptions, defaultRenderMode }), (0, plugin_api_docgen_namespaceObject.pluginApiDocgen)({ entries, apiParseTool, appDir, parseToolOptions }) ] }, { ...doc, base: isProduction ? doc.base : '' }); if (isProduction) await build({ appDirectory: appDir, docDirectory: root, config: modernDocConfig }); else await dev({ appDirectory: appDir, docDirectory: root, config: modernDocConfig }); } const modulePluginDoc = (pluginOptions)=>({ name: '@modern-js/plugin-module-doc', setup: (api)=>({ registerDev () { return { name: 'doc', menuItem: { name: 'doc', value: 'doc' }, subCommands: [ 'doc' ], async action () { const appContext = api.useAppContext(); const { appDirectory } = appContext; await launchDoc({ pluginOptions, appDir: appDirectory, isProduction: false }); } }; }, registerBuildPlatform () { return { platform: 'doc', async build () { const appContext = api.useAppContext(); const { appDirectory } = appContext; await launchDoc({ pluginOptions, appDir: appDirectory, isProduction: true }); } }; } }) }); // deprecated default export /* ESM default export */ const src = modulePluginDoc; var __webpack_export_target__ = exports; for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__]; if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', { value: true });