Skip to content

Commit

Permalink
feat: support intl in server
Browse files Browse the repository at this point in the history
  • Loading branch information
ClarkXia committed Apr 26, 2024
1 parent 585af12 commit b7dc50f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-actors-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/app': patch
---

feat: support generator api to inject code in server entry
9 changes: 7 additions & 2 deletions packages/ice/src/createService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
addEntryCode: (callback: (originalCode: string) => string) => {
entryCode = callback(entryCode);
},
addEntryImportAhead: (declarationData: Pick<DeclarationData, 'source'>) => {
generator.addDeclaration('entry', declarationData);
addEntryImportAhead: (declarationData: Pick<DeclarationData, 'source'>, type = 'client') => {
if (type === 'both' || type === 'server') {
generator.addDeclaration('entryServer', declarationData);
}
if (type === 'both' || type === 'client') {
generator.addDeclaration('entry', declarationData);
}
},
modifyRenderData: generator.modifyRenderData,
addDataLoaderImport: (declarationData: DeclarationData) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ice/src/service/runtimeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default class Generator {
this.rerender = false;
this.renderTemplates = [];
this.renderDataRegistration = [];
this.contentTypes = ['framework', 'frameworkTypes', 'routeConfigTypes', 'dataLoaderImport', 'runtimeOptions', 'entry'];
this.contentTypes = ['framework', 'frameworkTypes', 'routeConfigTypes', 'dataLoaderImport', 'runtimeOptions', 'entry', 'entryServer'];
// empty .ice before render
fse.emptyDirSync(path.join(rootDir, targetDir));
// add initial templates
Expand Down
2 changes: 1 addition & 1 deletion packages/ice/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type { CreateLoggerReturnType } from '../utils/logger.js';

type AddExport = (exportData: DeclarationData) => void;
type AddEntryCode = (callback: (code: string) => string) => void;
type AddEntryImportAhead = (exportData: Pick<DeclarationData, 'source'>) => void;
type AddEntryImportAhead = (exportData: Pick<DeclarationData, 'source'>, type?: string) => void;
type RemoveExport = (removeSource: string | string[]) => void;
type EventName = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
type GetExportList = (key: string, target?: string) => DeclarationData[];
Expand Down
1 change: 1 addition & 0 deletions packages/ice/templates/core/entry.server.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './env.server';
<%- entryServer.imports %>
import * as runtime from '@ice/runtime/server';
<% if (hydrate) {-%>
import { commons, statics } from './runtime-modules';
Expand Down
9 changes: 4 additions & 5 deletions packages/plugin-intl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ const plugin: Plugin = () => ({
renderLocaleEntry(files);
}
}]);

runtimeSource = './locales';

generator.addEntryImportAhead({
source: runtimeSource,
}, 'both');
}
generator.addRuntimeOptions({
source: runtimeSource,
specifier: 'localeMessages',
});
} else {
logger.warn('No locale files found, please check the `src/locales` folder.');
}
Expand Down
25 changes: 17 additions & 8 deletions packages/plugin-intl/src/runtime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ import type { IntlShape } from 'react-intl';
import type { RuntimePlugin } from '@ice/runtime/types';
import type { LocaleConfig } from './types.js';

interface RuntimeOptons {
localeMessages?: Record<string, Record<string, string>>;
}

const EXPORT_NAME = 'locale';
const cache = createIntlCache();
let intl: IntlShape = null;

const getDefaultLocale = () => {
return (typeof navigator !== 'undefined' && navigator.language) || 'zh-CN';
};
const runtime: RuntimePlugin<RuntimeOptons> = async ({

const getLocaleMessages = () => {
return (typeof window !== 'undefined'

Check warning on line 15 in packages/plugin-intl/src/runtime.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

Unexpected negated condition

Check warning on line 15 in packages/plugin-intl/src/runtime.tsx

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

Unexpected negated condition

Check warning on line 15 in packages/plugin-intl/src/runtime.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

Unexpected negated condition

Check warning on line 15 in packages/plugin-intl/src/runtime.tsx

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

Unexpected negated condition
// @ts-ignore
? window.__ICE_LOCALE_MESSAGES__
: global.__ICE_LOCALE_MESSAGES__) || {};
};

const defaultLocale = getDefaultLocale();
let intl: IntlShape = createIntl({
locale: defaultLocale,
messages: getLocaleMessages()?.[defaultLocale] || {},
});

const runtime: RuntimePlugin = async ({
addProvider,
appContext,
}, runtimeOptions) => {
}) => {
const { appExport } = appContext;
const exported = appExport[EXPORT_NAME];
const localeConfig: LocaleConfig = (typeof exported === 'function' ? await exported() : exported) || {};
Expand All @@ -27,7 +36,7 @@ const runtime: RuntimePlugin<RuntimeOptons> = async ({

intl = createIntl({
...l,
messages: runtimeOptions.localeMessages?.[locale] || {},
messages: getLocaleMessages()?.[locale] || {},
locale: getLocale ? getLocale() : getDefaultLocale(),
}, cache);
addProvider(({ children }) => {
Expand Down
10 changes: 9 additions & 1 deletion packages/plugin-intl/templates/locales.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<%- localeImport %>

export default {
const localeMessages = {
<%- localeExport %>
};
const LOCALE_KEY = '__ICE_LOCALE_MESSAGES__';
if (typeof window !== 'undefined') {
window[LOCALE_KEY] = localeMessages;
} else {
global[LOCALE_KEY]= localeMessages;
}

export default localeMessages;

0 comments on commit b7dc50f

Please sign in to comment.