Skip to content

Commit

Permalink
build: migrate @rspress/core to Rslib build
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Nov 25, 2024
1 parent f708ebe commit 7390388
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 13 deletions.
6 changes: 4 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
95 changes: 95 additions & 0 deletions packages/core/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -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()],
},
],
});
5 changes: 4 additions & 1 deletion packages/core/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/node/runtimeModule/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/node/runtimeModule/siteData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 || [],
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/node/utils/flattenMdxContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ 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 };
}

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) {
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/runtime/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ type PageMeta = {
};

export async function initPageData(routePath: string): Promise<PageData> {
const { routes } = process.env.__SSR__
? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
: (require('virtual-routes') as typeof import('virtual-routes'));
// const target = process.env.__SSR__ ? 'virtual-routes-ssr' : 'virtual-routes';
const { routes } = (await import(
process.env.__SSR__ ? 'virtual-routes-ssr' : 'virtual-routes'
)) as typeof import('virtual-routes-ssr');
// const { routes } = require(
// process.env.__SSR__ ? 'virtual-routes-ssr' : 'virtual-routes',
// ) as typeof import('virtual-routes-ssr');
// const { routes } = process.env.__SSR__
// ? (require('virtual-routes-ssr') as typeof import('virtual-routes-ssr'))
// : (require('virtual-routes') as typeof import('virtual-routes'));
const matched = matchRoutes(routes, routePath)!;
if (matched) {
// Preload route component
Expand All @@ -42,9 +49,7 @@ export async function initPageData(routePath: string): Promise<PageData> {
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));
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7390388

Please sign in to comment.