Skip to content

Commit 9baa6ca

Browse files
authored
refactor(virtual-module): i18n.json restart -> reload/hmr (#2019)
1 parent f3e6544 commit 9baa6ca

File tree

7 files changed

+47
-17
lines changed

7 files changed

+47
-17
lines changed

e2e/fixtures/i18n/doc/zh/_meta.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"text": "Guide",
4-
"link": "/guide/"
4+
"link": "/guide/basic/install"
55
},
66
{
77
"text": "Api",

packages/cli/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { version } from '../package.json';
99
import { loadConfigFile, resolveDocRoot } from './config/loadConfigFile';
1010
import update from './update';
1111

12-
const CONFIG_FILES = ['rspress.config.ts', 'rspress.config.js', 'i18n.json'];
12+
const CONFIG_FILES = ['rspress.config.ts', 'rspress.config.js'];
1313

1414
const META_FILE = '_meta.json';
1515

packages/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"remark-rehype": "^11.1.1",
105105
"remark-stringify": "^11.0.0",
106106
"rimraf": "^6.0.1",
107+
"rsbuild-plugin-virtual-module": "0.1.1",
107108
"tailwindcss": "^3.4.17",
108109
"typescript": "^5.8.2",
109110
"vfile": "^5.3.7"

packages/core/src/node/initRsbuild.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
removeLeadingSlash,
1515
removeTrailingSlash,
1616
} from '@rspress/shared';
17+
import { pluginVirtualModule } from 'rsbuild-plugin-virtual-module';
1718
import type { PluginDriver } from './PluginDriver';
1819
import {
1920
CLIENT_ENTRY,
@@ -28,7 +29,8 @@ import {
2829
import { hintThemeBreakingChange } from './logger/hint';
2930
import type { RouteService } from './route/RouteService';
3031
import { initRouteService } from './route/init';
31-
import { rsbuildPluginDocVM } from './runtimeModule';
32+
import { type FactoryContext, rsbuildPluginDocVM } from './runtimeModule';
33+
import { i18nVMPlugin } from './runtimeModule/i18n';
3234
import { serveSearchIndexMiddleware } from './searchIndex';
3335
import { detectReactVersion, resolveReactAlias } from './utils';
3436
import { detectCustomIcon } from './utils/detectCustomIcon';
@@ -95,15 +97,21 @@ async function createInternalBuildConfig(
9597
enableSSG ? resolveReactAlias(reactVersion, true) : Promise.resolve({}),
9698
]);
9799

100+
const context: Omit<FactoryContext, 'isSSR' | 'alias'> = {
101+
userDocRoot,
102+
config,
103+
runtimeTempDir,
104+
routeService,
105+
pluginDriver,
106+
};
98107
return {
99108
plugins: [
100109
...(isPluginIncluded(config, PLUGIN_REACT_NAME) ? [] : [pluginReact()]),
101-
rsbuildPluginDocVM({
102-
userDocRoot,
103-
config,
104-
runtimeTempDir,
105-
routeService,
106-
pluginDriver,
110+
rsbuildPluginDocVM(context),
111+
pluginVirtualModule({
112+
virtualModules: {
113+
...i18nVMPlugin(context),
114+
},
107115
}),
108116
],
109117
server: {

packages/core/src/node/runtimeModule/i18n.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join } from 'node:path';
2+
import type { TransformHandler } from '@rsbuild/core';
23
import type { UserConfig } from '@rspress/shared';
34
import { logger } from '@rspress/shared/logger';
45
import { type FactoryContext, RuntimeModuleID } from '.';
@@ -19,10 +20,20 @@ export function getI18nData(docConfig: UserConfig) {
1920
}
2021
}
2122

22-
export function i18nVMPlugin(context: FactoryContext) {
23+
/**
24+
* Generate i18n text for client runtime
25+
*/
26+
export function i18nVMPlugin(
27+
context: Omit<FactoryContext, 'isSSR' | 'alias'>,
28+
): Record<string, TransformHandler> {
2329
const { config } = context;
24-
const i18nData = getI18nData(config);
2530
return {
26-
[RuntimeModuleID.I18nText]: `export default ${JSON.stringify(i18nData, null, 2)}`,
31+
[RuntimeModuleID.I18nText]: ({ addDependency, addMissingDependency }) => {
32+
addDependency(config.i18nSourcePath || DEFAULT_I18N_SOURCE);
33+
addMissingDependency(config.i18nSourcePath || DEFAULT_I18N_SOURCE);
34+
const i18nData = getI18nData(config);
35+
36+
return `export default ${JSON.stringify(i18nData, null, 2)}`;
37+
},
2738
};
2839
}

packages/core/src/node/runtimeModule/index.ts

-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { PluginDriver } from '../PluginDriver';
55
import type { RouteService } from '../route/RouteService';
66
import { globalStylesVMPlugin } from './globalStyles';
77
import { globalUIComponentsVMPlugin } from './globalUIComponents';
8-
import { i18nVMPlugin } from './i18n';
98
import { routeVMPlugin } from './routeData';
109
import { searchHookVMPlugin } from './searchHooks';
1110
import { siteDataVMPlugin } from './siteData/index';
@@ -42,10 +41,6 @@ export const runtimeModuleFactory: RuntimeModuleFactory[] = [
4241
* Generate global styles from config and plugins
4342
*/
4443
globalStylesVMPlugin,
45-
/**
46-
* Generate i18n text for client runtime
47-
*/
48-
i18nVMPlugin,
4944
/**
5045
* Generate search hook module
5146
*/

pnpm-lock.yaml

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)