Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

i18next-hmr

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18next-hmr - npm Package Compare versions

Comparing version 1.7.8 to 1.8.0

11

CHANGELOG.md

@@ -7,7 +7,16 @@ ### Changelog

#### [v1.8.0](https://github.com/felixmosh/i18next-hmr/compare/v1.7.8...v1.8.0)
- Add next-i18next v11 example, closes #32 [`#32`](https://github.com/felixmosh/i18next-hmr/issues/32)
- Update examples [`e171edc`](https://github.com/felixmosh/i18next-hmr/commit/e171edcc4c0288fb0c6c4ffa84303e7f6111b513)
- Remove webpack 5 example [`0c02c24`](https://github.com/felixmosh/i18next-hmr/commit/0c02c246e483ccb4fcccc2be1269989b0db69f41)
- feat: add the ability to pass a getter for i18n instance [`574deab`](https://github.com/felixmosh/i18next-hmr/commit/574deabdd45d129ec76858cd443e9da16b46401d)
#### [v1.7.8](https://github.com/felixmosh/i18next-hmr/compare/v1.7.7...v1.7.8)
> 19 July 2022
- Bump deps [`ed7c5eb`](https://github.com/felixmosh/i18next-hmr/commit/ed7c5eb5a92ad1651b3e9b59ca60d5da7ea64c11)
- feat: Add support for WatchIgnorePlugin [`1d9ce13`](https://github.com/felixmosh/i18next-hmr/commit/1d9ce13906e4af20e89c362b1ed6a03fdd621098)
- Bump eventsource from 1.1.0 to 1.1.1 in /examples/react-i18next [`101b9e1`](https://github.com/felixmosh/i18next-hmr/commit/101b9e1a815f109a197c6bc3fa5c7ad7c2f5c478)
- Release 1.7.8 [`8949044`](https://github.com/felixmosh/i18next-hmr/commit/8949044cfe083ddb65790dfc22d9bc858cafe3ae)

@@ -14,0 +23,0 @@ #### [v1.7.7](https://github.com/felixmosh/i18next-hmr/compare/v1.7.6...v1.7.7)

2

client.d.ts
import i18next from 'i18next';
declare const client: {
applyClientHMR(i18n: i18next.i18n): void;
applyClientHMR(i18nOrGetter: i18next.i18n | (() => i18next.i18n)): void;
};
export = client;

@@ -1,4 +0,4 @@

const { extractLangAndNS, printList } = require('./utils');
const { extractList, printList, makeUniqueList, createLoggerOnce } = require('./utils');
module.exports = function applyClientHMR(i18n) {
module.exports = function applyClientHMR(i18nOrGetter) {
if (module.hot) {

@@ -9,2 +9,4 @@ function log(msg, type = 'log') {

const logOnce = createLoggerOnce(log);
const { changedFile } = require('./trigger.js');

@@ -17,16 +19,18 @@

let backendOptions = { queryStringParams: {} };
try {
backendOptions = i18n.options.backend || i18n.services.backendConnector.backend.options;
backendOptions.queryStringParams = backendOptions.queryStringParams || {};
} catch (e) {
log('Client i18next-backend not found, hmr may not work', 'warn');
}
async function reloadTranslations(list, i18nInstance) {
let backendOptions = { queryStringParams: {} };
try {
backendOptions =
i18nInstance.options.backend || i18nInstance.services.backendConnector.backend.options;
backendOptions.queryStringParams = backendOptions.queryStringParams || {};
} catch (e) {
logOnce('Client i18next-http-backend not found, hmr may not work', 'warn');
}
async function reloadTranslations(list) {
backendOptions.queryStringParams._ = new Date().getTime(); // cache killer
const langs = [...new Set(list.map((item) => item.lang))];
const namespaces = [...new Set(list.map((item) => item.ns))];
await i18n.reloadResources(langs, namespaces, (error) => {
const langs = makeUniqueList(list.map((item) => item.lang));
const namespaces = makeUniqueList(list.map((item) => item.ns));
await i18nInstance.reloadResources(langs, namespaces, (error) => {
if (error) {

@@ -37,6 +41,6 @@ log(error, 'error');

const currentLang = i18n.language;
const currentLang = i18nInstance.language;
if (langs.includes(currentLang)) {
i18n.changeLanguage(currentLang);
i18nInstance.changeLanguage(currentLang);
log(`Update applied successfully`);

@@ -51,6 +55,6 @@ } else {

const { changedFiles } = require('./trigger.js');
const list = changedFiles
.map((changedFile) => extractLangAndNS(changedFile, i18n.options.ns))
.filter(({ lang, ns }) => Boolean(lang) && Boolean(ns));
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
const list = extractList(changedFiles, i18nInstance.options.ns);
if (!list.length) {

@@ -62,5 +66,5 @@ return;

return reloadTranslations(list);
return reloadTranslations(list, i18nInstance);
});
}
};

@@ -1,15 +0,16 @@

const { extractLangAndNS, printList } = require('./utils');
const { extractList, printList, makeUniqueList, createLoggerOnce } = require('./utils');
module.exports = function applyServerHMR(i18n) {
module.exports = function applyServerHMR(i18nOrGetter) {
const pluginName = `\x1b[35m\x1b[1m${'I18NextHMR'}\x1b[0m\x1b[39m`;
function log(message) {
console.log(`[ ${pluginName} ] ${message}`);
function log(message, type = 'log') {
console[type](`[ ${pluginName} ] ${message}`);
}
const logOnce = createLoggerOnce(log);
function reloadServerTranslation({ changedFiles }) {
const list = changedFiles
.map((changedFile) => extractLangAndNS(changedFile, i18n.options.ns))
.filter(({ lang, ns }) => Boolean(lang) && Boolean(ns));
const i18nInstance = typeof i18nOrGetter === 'function' ? i18nOrGetter() : i18nOrGetter;
const list = extractList(changedFiles, i18nInstance.options.ns);
if (list.length === 0) {

@@ -21,6 +22,6 @@ return;

const langs = [...new Set(list.map((item) => item.lang))];
const namespaces = [...new Set(list.map((item) => item.ns))];
const langs = makeUniqueList(list.map((item) => item.lang));
const namespaces = makeUniqueList(list.map((item) => item.ns));
i18n.reloadResources(langs, namespaces, (error) => {
i18nInstance.reloadResources(langs, namespaces, (error) => {
if (error) {

@@ -39,3 +40,3 @@ log(`\x1b[31m\x1b[1m${error}\x1b[0m\x1b[39m`);

// We must use the required variable
log(`Server HMR has started`);
logOnce(`Server HMR has started`);
}

@@ -48,3 +49,3 @@

} else {
log(`Server HMR has started`);
logOnce(`Server HMR has started - callback mode`);

@@ -51,0 +52,0 @@ const HMRPlugin = require('./plugin');

@@ -1,29 +0,58 @@

module.exports = {
extractLangAndNS: function (changedFile, currentNSList) {
const changedFileParts = changedFile.replace(/\\/g, '/').split('/');
function extractLangAndNS(changedFile, currentNSList) {
const changedFileParts = changedFile.replace(/\\/g, '/').split('/');
const firstLongestNSMatchParts = []
.concat(currentNSList)
.map((ns) => ns.split('/'))
.sort((a, b) => b.length - a.length)
.find((optionalNS) =>
optionalNS.every((optionalNSPart) => changedFileParts.includes(optionalNSPart))
);
const firstLongestNSMatchParts = []
.concat(currentNSList)
.map((ns) => ns.split('/'))
.sort((a, b) => b.length - a.length)
.find((optionalNS) =>
optionalNS.every((optionalNSPart) => changedFileParts.includes(optionalNSPart))
);
if (!firstLongestNSMatchParts) {
return { lang: null, ns: null };
if (!firstLongestNSMatchParts) {
return { lang: null, ns: null };
}
const lang = changedFileParts
.filter((part) => !firstLongestNSMatchParts.includes(part))
.join('/');
return {
lang,
ns: firstLongestNSMatchParts.join('/'),
};
}
function printList(list) {
return list.map((item) => `${item.lang}/${item.ns}`).join(', ');
}
function extractList(changedFiles, currentNSList) {
return changedFiles
.map((changedFile) => extractLangAndNS(changedFile, currentNSList))
.filter(({ lang, ns }) => Boolean(lang) && Boolean(ns));
}
function makeUniqueList(list) {
return [...new Set(list)];
}
function createLoggerOnce(logger) {
const msgCount = new Map();
return (msg, type = 'log') => {
const count = msgCount.has(msg) ? msgCount.get(msg) : 0;
if (count > 0) {
return;
}
const lang = changedFileParts
.filter((part) => !firstLongestNSMatchParts.includes(part))
.join('/');
logger(msg, type);
msgCount.set(msg, count + 1);
};
}
return {
lang,
ns: firstLongestNSMatchParts.join('/'),
};
},
printList: function (list) {
return list.map(item => `${item.lang}/${item.ns}`).join(', ')
}
module.exports = {
printList: printList,
extractList: extractList,
makeUniqueList: makeUniqueList,
createLoggerOnce: createLoggerOnce,
};
{
"name": "i18next-hmr",
"version": "1.7.8",
"version": "1.8.0",
"description": "I18Next HMR webpack plugin that allows reloading translation resources on the client & the server.",

@@ -5,0 +5,0 @@ "keywords": [

import i18next from 'i18next';
declare const server: {
applyServerHMR(i18n: i18next.i18n): void;
applyServerHMR(i18nOrGetter: i18next.i18n | (() => i18next.i18n)): void;
};
export = server;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc