Skip to content

Commit 0253f0a

Browse files
committed
feat: pass changed files list to i18next getter, #118
1 parent ed603da commit 0253f0a

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

__tests__/client-hmr.spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,25 @@ describe('client-hmr', () => {
118118
expect(i18nMock.changeLanguage).toHaveBeenCalledWith('en');
119119
});
120120

121-
it('should trigger reload when lng-country combination file changed', async () => {
121+
it('should pass changed filed to the i18next getter', () => {
122+
i18nMock.options = { backend: {}, ns: ['name-space'] };
123+
i18nMock.language = 'en';
124+
const getter = jest.fn().mockImplementation(() => i18nMock);
125+
const changedFiles = ['en/name-space'];
126+
127+
applyClientHMR(getter);
128+
whenHotTriggeredWith(changedFiles);
129+
130+
expect(getter).toHaveBeenCalledWith({ changedFiles });
131+
});
132+
133+
it('should trigger reload when lng-country combination file changed', () => {
122134
i18nMock.options = { backend: {}, ns: ['name-space'] };
123135
i18nMock.language = 'en-US';
124136

125137
applyClientHMR(i18nMock);
126138

127-
await whenHotTriggeredWith(['en-US/name-space']);
139+
whenHotTriggeredWith(['en-US/name-space']);
128140

129141
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
130142
['en-US'],

__tests__/server-hmr.spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,12 @@ describe('server-hmr', () => {
401401
accept: jest.fn(),
402402
},
403403
};
404-
405-
applyServerHMR(() => i18nMock);
406404
});
407405

408406
it('should reload resources on updated lang, ns', () => {
409407
const update = { lang: 'en', ns: 'name-space' };
408+
applyServerHMR(() => i18nMock);
409+
410410
whenNativeHMRTriggeredWith([`${update.lang}/${update.ns}`]);
411411

412412
expect(i18nMock.reloadResources).toHaveBeenCalledWith(
@@ -415,5 +415,17 @@ describe('server-hmr', () => {
415415
expect.any(Function)
416416
);
417417
});
418+
419+
it('should pass changed filed to the i18next getter', () => {
420+
const update = { lang: 'en', ns: 'name-space' };
421+
422+
const getter = jest.fn().mockImplementation(() => i18nMock);
423+
const changedFiles = [`${update.lang}/${update.ns}`];
424+
applyServerHMR(getter);
425+
426+
whenNativeHMRTriggeredWith(changedFiles);
427+
428+
expect(getter).toHaveBeenCalledWith({ changedFiles });
429+
});
418430
});
419431
});

lib/client-hmr.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module.exports = function applyClientHMR(i18nOrGetter) {
4949

5050
module.hot.accept('./trigger.js', () => {
5151
const { changedFiles } = require('./trigger.js');
52-
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
52+
const i18nInstance =
53+
typeof i18nOrGetter === 'function' ? i18nOrGetter({ changedFiles }) : i18nOrGetter;
5354

5455
const list = extractList(changedFiles, i18nInstance);
5556

lib/server-hmr.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ module.exports = function applyServerHMR(i18nOrGetter) {
99
const logOnce = createLoggerOnce(log);
1010

1111
function reloadServerTranslation({ changedFiles }) {
12-
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
12+
const i18nInstance =
13+
typeof i18nOrGetter === 'function' ? i18nOrGetter({ changedFiles }) : i18nOrGetter;
1314

1415
const list = extractList(changedFiles, i18nInstance);
1516

0 commit comments

Comments
 (0)