11import type { AppTools , CliPlugin } from '@modern-js/app-tools' ;
22import { getPublicDirRoutePrefixes } from '@modern-js/server-core' ;
3+ import type { Entrypoint } from '@modern-js/types' ;
34import type { BackendOptions , LocaleDetectionOptions } from '../shared/type' ;
45import { getBackendOptions , getLocaleDetectionOptions } from '../shared/utils' ;
56
7+ export type TransformRuntimeConfigFn = (
8+ extendedConfig : Record < string , any > ,
9+ entrypoint : Entrypoint ,
10+ ) => Record < string , any > ;
11+
612export interface I18nPluginOptions {
713 localeDetection ?: LocaleDetectionOptions ;
814 backend ?: BackendOptions ;
15+ transformRuntimeConfig ?: TransformRuntimeConfigFn ;
16+ [ key : string ] : any ;
917}
1018
1119export const i18nPlugin = (
1220 options : I18nPluginOptions = { } ,
1321) : CliPlugin < AppTools > => ( {
1422 name : '@modern-js/plugin-i18n' ,
1523 setup : api => {
16- const { localeDetection, backend } = options ;
24+ const { localeDetection, backend, transformRuntimeConfig, ...restOptions } =
25+ options ;
1726 api . _internalRuntimePlugins ( ( { entrypoint, plugins } ) => {
1827 const localeDetectionOptions = localeDetection
1928 ? getLocaleDetectionOptions ( entrypoint . entryName , localeDetection )
@@ -22,14 +31,28 @@ export const i18nPlugin = (
2231 ? getBackendOptions ( entrypoint . entryName , backend )
2332 : undefined ;
2433 const { metaName } = api . getAppContext ( ) ;
34+
35+ // Transform extended config if transform function is provided
36+ let extendedConfig = restOptions ;
37+ if ( transformRuntimeConfig ) {
38+ extendedConfig = transformRuntimeConfig (
39+ restOptions ,
40+ entrypoint as Entrypoint ,
41+ ) ;
42+ }
43+
44+ // Build final config with base config and transformed extended config
45+ const config = {
46+ entryName : entrypoint . entryName ,
47+ localeDetection : localeDetectionOptions ,
48+ backend : backendOptions ,
49+ ...extendedConfig ,
50+ } ;
51+
2552 plugins . push ( {
2653 name : 'i18n' ,
2754 path : `@${ metaName } /plugin-i18n/runtime` ,
28- config : {
29- entryName : entrypoint . entryName ,
30- localeDetection : localeDetectionOptions ,
31- backend : backendOptions ,
32- } ,
55+ config,
3356 } ) ;
3457 return {
3558 entrypoint,
@@ -38,7 +61,7 @@ export const i18nPlugin = (
3861 } ) ;
3962
4063 api . _internalServerPlugins ( ( { plugins } ) => {
41- const { serverRoutes } = api . getAppContext ( ) ;
64+ const { serverRoutes, metaName } = api . getAppContext ( ) ;
4265 const normalizedConfig = api . getNormalizedConfig ( ) ;
4366
4467 let staticRoutePrefixes : string [ ] = [ ] ;
@@ -65,7 +88,7 @@ export const i18nPlugin = (
6588 } ) ;
6689
6790 plugins . push ( {
68- name : '@modern-js /plugin-i18n/server' ,
91+ name : `@ ${ metaName } /plugin-i18n/server` ,
6992 options : {
7093 localeDetection,
7194 staticRoutePrefixes,
0 commit comments