Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: migrate @rspress/core to Rslib build #1627

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 6 additions & 6 deletions packages/core/src/runtime/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ 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 { 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
Expand All @@ -42,9 +44,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.