From 94d569998c0e691edc0a6c60f2091c19e1cf4e59 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Mon, 25 Nov 2024 18:26:56 +0800 Subject: [PATCH] build: migrate @rspress/core to Rslib build --- packages/core/package.json | 6 +- packages/core/rslib.config.ts | 95 +++++++++++++++++++ packages/core/src/node/build.ts | 5 +- packages/core/src/node/runtimeModule/i18n.ts | 4 +- .../src/node/runtimeModule/siteData/index.ts | 3 +- .../core/src/node/utils/flattenMdxContent.ts | 3 +- packages/core/src/runtime/App.tsx | 12 +-- packages/core/tsconfig.json | 2 +- pnpm-lock.yaml | 9 ++ 9 files changed, 126 insertions(+), 13 deletions(-) create mode 100644 packages/core/rslib.config.ts diff --git a/packages/core/package.json b/packages/core/package.json index c6bb1b1c6..008d70306 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -30,8 +30,8 @@ } }, "scripts": { - "dev": "modern build -w", - "build": "modern build", + "dev": "rslib build -w", + "build": "rslib build", "reset": "rimraf ./**/node_modules", "test": "vitest run --passWithNoTests" }, @@ -79,6 +79,8 @@ "devDependencies": { "@modern-js/plugin-tailwindcss": "2.62.1", "@modern-js/tsconfig": "2.62.1", + "@rslib/core": "0.1.0", + "@microsoft/api-extractor": "^7.48.0", "@types/hast": "3.0.4", "@types/html-to-text": "^9.0.4", "@types/jest": "~29.5.14", diff --git a/packages/core/rslib.config.ts b/packages/core/rslib.config.ts new file mode 100644 index 000000000..42805a079 --- /dev/null +++ b/packages/core/rslib.config.ts @@ -0,0 +1,95 @@ +import { defineConfig } from '@rslib/core'; +import { pluginReact } from '@rsbuild/plugin-react'; + +export default defineConfig({ + lib: [ + { + format: 'esm', + dts: { + bundle: true, + }, + syntax: 'es2020', + shims: { + esm: { + require: true, + }, + }, + output: { + distPath: { + root: './dist', + }, + externals: [ + { + 'react-syntax-highlighter/dist/cjs/languages/prism/supported-languages': + 'commonjs react-syntax-highlighter/dist/cjs/languages/prism/supported-languages', + }, + '@rspress/mdx-rs', + 'jsdom', + 'tailwindcss', + '@rspress/plugin-container-syntax', + '../compiled/globby/index.js', + ], + }, + }, + { + format: 'esm', + syntax: 'es2020', + dts: { + bundle: true, + }, + shims: { + esm: { + require: true, + }, + }, + output: { + distPath: { + root: './dist', + }, + externals: { + '@rspress/mdx-rs': 'commonjs @rspress/mdx-rs', + }, + }, + source: { + entry: { + loader: './src/node/mdx/loader.ts', + }, + }, + }, + { + bundle: false, + dts: true, + format: 'esm', + syntax: 'es2020', + source: { + entry: { + index: './src/runtime/*', + }, + tsconfigPath: './src/runtime/tsconfig.json', + }, + output: { + externals: [ + ({ request, dependencyType, contextInfo }: any, callback: any) => { + if (/^(virtual-routes-ssr|virtual-routes)$/.test(request)) { + // Externalize to a commonjs module using the request path + return callback( + null, + `${dependencyType === 'commonjs' ? 'commonjs' : 'module-import'} ${request}`, + ); + } + + callback(); + }, + // { + // 'virtual-routes-ssr': 'commonjs virtual-routes-ssr', + // 'virtual-routes': 'commonjs virtual-routes', + // }, + ], + distPath: { + root: './dist/runtime', + }, + }, + plugins: [pluginReact()], + }, + ], +}); diff --git a/packages/core/src/node/build.ts b/packages/core/src/node/build.ts index 72ff8b699..ffbc7dd05 100644 --- a/packages/core/src/node/build.ts +++ b/packages/core/src/node/build.ts @@ -76,7 +76,10 @@ export async function renderPages( try { const { default: fs } = await import('@rspress/shared/fs-extra'); - const { version } = await import('../../package.json'); + // const version = '1.1.1'; + const { version } = await import( + /* webpackMode: "eager" */ '../../package.json' + ); // There are two cases where we will fallback to CSR: // 1. ssr bundle load failed // 2. ssr bundle render failed diff --git a/packages/core/src/node/runtimeModule/i18n.ts b/packages/core/src/node/runtimeModule/i18n.ts index 8ff100ff3..4c8308970 100644 --- a/packages/core/src/node/runtimeModule/i18n.ts +++ b/packages/core/src/node/runtimeModule/i18n.ts @@ -7,7 +7,9 @@ const DEFAULT_I18N_SOURCE = join(process.cwd(), 'i18n.json'); export function getI18nData(docConfig: UserConfig) { const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig; try { - delete require.cache[i18nSourcePath]; + // TODO: a workaround to skip require.cache parse + const cacheKey = 'cache'; + delete require[cacheKey][i18nSourcePath]; // eslint-disable-next-line import/no-dynamic-require const i18nSource = require(i18nSourcePath); return i18nSource; diff --git a/packages/core/src/node/runtimeModule/siteData/index.ts b/packages/core/src/node/runtimeModule/siteData/index.ts index 78469e35a..d1702161f 100644 --- a/packages/core/src/node/runtimeModule/siteData/index.ts +++ b/packages/core/src/node/runtimeModule/siteData/index.ts @@ -44,7 +44,7 @@ export async function siteDataVMPlugin(context: FactoryContext) { // If the dev server restart when config file, we will reuse the siteData instead of extracting the siteData from source files again. const domain = userConfig?.search && userConfig?.search?.mode === 'remote' - ? (userConfig?.search.domain ?? '') + ? userConfig?.search.domain ?? '' : ''; const pages = await extractPageData( replaceRules, @@ -119,6 +119,7 @@ export async function siteDataVMPlugin(context: FactoryContext) { logo: userConfig?.logo || '', logoText: userConfig?.logoText || '', ssg: Boolean(userConfig?.ssg) ?? true, + root: userConfig?.root || '', multiVersion: { default: userConfig?.multiVersion?.default || '', versions: userConfig?.multiVersion?.versions || [], diff --git a/packages/core/src/node/utils/flattenMdxContent.ts b/packages/core/src/node/utils/flattenMdxContent.ts index fc59a515f..bcebe2a42 100644 --- a/packages/core/src/node/utils/flattenMdxContent.ts +++ b/packages/core/src/node/utils/flattenMdxContent.ts @@ -91,7 +91,7 @@ export async function flattenMdxContent( let result = content; try { - ast = processor.parse(content) as Root; + ast = processor.parse(content) as unknown as Root; } catch (e) { // Fallback: if mdx parse failed, just return the content return { flattenContent: content, deps }; @@ -99,6 +99,7 @@ export async function flattenMdxContent( const importNodes = ast.children .filter(node => node.type === ('mdxjsEsm' as any)) + // @ts-expect-error .flatMap(node => (node.data?.estree as ESTree)?.body || []) .filter(node => node.type === 'ImportDeclaration'); for (const importNode of importNodes) { diff --git a/packages/core/src/runtime/App.tsx b/packages/core/src/runtime/App.tsx index 65df7820f..5aa38cc28 100644 --- a/packages/core/src/runtime/App.tsx +++ b/packages/core/src/runtime/App.tsx @@ -30,9 +30,11 @@ type PageMeta = { }; export async function initPageData(routePath: string): Promise { - const { routes } = process.env.__SSR__ - ? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr')) - : (require('virtual-routes') as typeof import('virtual-routes')); + const { routes } = (await import( + /* webpackMode: "eager" */ process.env.__SSR__ + ? 'virtual-routes-ssr' + : 'virtual-routes' + )) as typeof import('virtual-routes-ssr'); const matched = matchRoutes(routes, routePath)!; if (matched) { // Preload route component @@ -42,9 +44,7 @@ export async function initPageData(routePath: string): Promise { const extractPageInfo = siteData.pages.find(page => { const normalize = (p: string) => // compat the path that has no / suffix and ignore case - p - .replace(/\/$/, '') - .toLowerCase(); + p.replace(/\/$/, '').toLowerCase(); return isEqualPath(normalize(page.routePath), normalize(routePath)); }); diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 8b0f2b401..ccd2a6e48 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -20,7 +20,7 @@ "include": [ "src", "vitest.config.ts", - "./modern.config.ts", + "./rslib.config.ts", "./tailwind.config.ts" ], "exclude": ["runtime.ts", "theme.ts", "node_modules"] diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a659cc816..93f3ce8ec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -694,12 +694,18 @@ importers: specifier: ^3.0.0 version: 3.0.0 devDependencies: + '@microsoft/api-extractor': + specifier: ^7.48.0 + version: 7.48.0(@types/node@18.11.17) '@modern-js/plugin-tailwindcss': specifier: 2.62.1 version: 2.62.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(tailwindcss@3.4.15(ts-node@10.9.2(@types/node@18.11.17)(typescript@5.5.3))) '@modern-js/tsconfig': specifier: 2.62.1 version: 2.62.1 + '@rslib/core': + specifier: 0.1.0 + version: 0.1.0(@microsoft/api-extractor@7.48.0(@types/node@18.11.17))(typescript@5.5.3) '@types/hast': specifier: 3.0.4 version: 3.0.4 @@ -4131,6 +4137,7 @@ packages: glob@7.2.3: resolution: {integrity: sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==} + deprecated: Glob versions prior to v9 are no longer supported glob@8.1.0: resolution: {integrity: sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==} @@ -4318,6 +4325,7 @@ packages: inflight@1.0.6: resolution: {integrity: sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==} + deprecated: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful. inherits@2.0.4: resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==} @@ -5514,6 +5522,7 @@ packages: rimraf@3.0.2: resolution: {integrity: sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==} + deprecated: Rimraf versions prior to v4 are no longer supported hasBin: true rollup@4.20.0: