i18next-hmr
Advanced tools
Comparing version
@@ -7,6 +7,15 @@ ### Changelog | ||
#### [v3.0.0](https://github.com/felixmosh/i18next-hmr/compare/v2.0.1...v3.0.0) | ||
- Bump deps [`0b74d59`](https://github.com/felixmosh/i18next-hmr/commit/0b74d595a0fabf128767a33be776301f311bba84) | ||
- Add Vite tests [`62afadd`](https://github.com/felixmosh/i18next-hmr/commit/62afaddb2a24b9f848a858df45de8d6488c2136e) | ||
- Initial support for Vite [`d86229f`](https://github.com/felixmosh/i18next-hmr/commit/d86229faa5a879d290ba7bae872cd2644f8e3cbb) | ||
#### [v2.0.1](https://github.com/felixmosh/i18next-hmr/compare/v2.0.0...v2.0.1) | ||
> 22 May 2023 | ||
- fix: make d.ts files express the correct type [`92d0b19`](https://github.com/felixmosh/i18next-hmr/commit/92d0b19addcc7220c04f3e7bb28180013d9d58fe) | ||
- fix: ignore if passed an empty i18n instance [`7221a35`](https://github.com/felixmosh/i18next-hmr/commit/7221a3528c7771751b1ecf0988338114495093dd) | ||
- Release 2.0.1 [`4bff183`](https://github.com/felixmosh/i18next-hmr/commit/4bff183bb3037be2d193b0f677b91d661dcbb6de) | ||
@@ -13,0 +22,0 @@ ### [v2.0.0](https://github.com/felixmosh/i18next-hmr/compare/v1.11.4...v2.0.0) |
@@ -5,10 +5,14 @@ class HMRPlugin { | ||
if (hmrOptions.client && typeof window !== 'undefined') { | ||
const applyClientHMR = require('./client-hmr'); | ||
const webpack = hmrOptions.webpack || {}; | ||
const vite = hmrOptions.vite || {}; | ||
if (webpack.client && typeof window !== 'undefined') { | ||
const applyClientHMR = require('./webpack/client-hmr'); | ||
applyClientHMR(() => this.i18nInstance); | ||
} else if (hmrOptions.server && typeof window === 'undefined') { | ||
const applyServerHMR = require('./server-hmr'); | ||
applyServerHMR(() => { | ||
return this.i18nInstance; | ||
}); | ||
} else if (webpack.server && typeof window === 'undefined') { | ||
const applyServerHMR = require('./webpack/server-hmr'); | ||
applyServerHMR(() => this.i18nInstance); | ||
} else if (vite.client && typeof window !== 'undefined') { | ||
const { applyViteClientHMR } = require('./vite/client-hmr'); | ||
applyViteClientHMR(() => this.i18nInstance); | ||
} | ||
@@ -15,0 +19,0 @@ } |
@@ -73,7 +73,47 @@ function extractLangAndNS(changedFile, currentConfig) { | ||
function log(msg, type = 'log') { | ||
console[type](`[%cI18NextHMR%c] ${msg}`, 'color:#bc93b6', ''); | ||
} | ||
const logOnce = createLoggerOnce(log); | ||
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'); | ||
} | ||
backendOptions.queryStringParams._ = new Date().getTime(); // cache killer | ||
const langs = uniqueList(list.map((item) => item.lang)); | ||
const namespaces = uniqueList(list.map((item) => item.ns)); | ||
await i18nInstance.reloadResources(langs, namespaces, (error) => { | ||
if (error) { | ||
log(error, 'error'); | ||
return; | ||
} | ||
const currentLang = i18nInstance.language; | ||
if (langs.includes(currentLang)) { | ||
i18nInstance.changeLanguage(currentLang); | ||
log(`Update applied successfully`); | ||
} else { | ||
log(`Resources of '${printList(list)}' were reloaded successfully`); | ||
} | ||
}); | ||
} | ||
module.exports = { | ||
printList: printList, | ||
extractList: extractList, | ||
log: log, | ||
createLoggerOnce: createLoggerOnce, | ||
uniqueList: uniqueList, | ||
createLoggerOnce: createLoggerOnce, | ||
reloadTranslations: reloadTranslations, | ||
}; |
{ | ||
"name": "i18next-hmr", | ||
"version": "2.0.1", | ||
"description": "I18Next HMR webpack plugin that allows reloading translation resources on the client & the server.", | ||
"version": "3.0.0", | ||
"description": "I18Next HMR🔥 webpack / vite plugin that allows reload translation resources instantly on the client & the server.", | ||
"keywords": [ | ||
@@ -9,2 +9,3 @@ "i18next", | ||
"webpack", | ||
"vite", | ||
"plugin" | ||
@@ -20,4 +21,7 @@ ], | ||
"author": "felixmosh", | ||
"main": "client.js", | ||
"types": "client.d.ts", | ||
"exports": { | ||
"./plugin": "./lib/plugin.js", | ||
"./webpack": "./lib/webpack/plugin.js", | ||
"./vite": "./lib/vite/plugin.js" | ||
}, | ||
"scripts": { | ||
@@ -45,5 +49,5 @@ "test": "jest", | ||
"auto-changelog": "^2.4.0", | ||
"jest": "^28.1.3", | ||
"prettier": "^2.6.2", | ||
"release-it": "^15.1.2" | ||
"jest": "^29.7.0", | ||
"prettier": "^3.0.3", | ||
"release-it": "^16.1.5" | ||
}, | ||
@@ -50,0 +54,0 @@ "release-it": { |
@@ -6,3 +6,3 @@ # i18next-hmr | ||
I18Next HMR🔥 webpack plugin that allows reloading translation resources on the client & the server | ||
I18Next HMR🔥 webpack / vite plugin that allows reload translation resources instantly on the client & the server. | ||
@@ -12,3 +12,4 @@ ## Requirements | ||
- Node.js v10 or above | ||
- Webpack 4.x - 5.x | ||
- Webpack v4.x - v5.x | ||
- Vite v3 | ||
@@ -56,5 +57,10 @@ | ||
if (process.env.NODE_ENV !== 'production') { | ||
instance.use(new HMRPlugin({ | ||
client: typeof window !== 'undefined', // enabled client side HMR | ||
server: typeof window === 'undefined' // enabled server side HMR | ||
instance.use(new HMRPlugin({ | ||
webpack: { | ||
client: typeof window !== 'undefined', // enabled client side HMR in webpack | ||
server: typeof window === 'undefined' // enabled server side HMR in webpack | ||
}, | ||
vite: { | ||
client: typeof window !== 'undefined', // enabled client side HMR in Vite | ||
} | ||
})); | ||
@@ -89,3 +95,3 @@ } | ||
The lib will trigger [`i18n.reloadResources([lang], [ns])`](https://www.i18next.com/overview/api#reloadresources) on the server side with `lang` & `namespace` extracted from the translation filename that was changed. | ||
This lib will trigger [`i18n.reloadResources([lang], [ns])`](https://www.i18next.com/overview/api#reloadresources) on the server side with `lang` & `namespace` extracted from the translation filename that was changed. | ||
@@ -107,3 +113,3 @@ ⚠️ If your server side is bundled using Webpack, the lib will use the native HMR (if enabled), for it to work properly the lib must be **bundled**, therefore, you should specify the lib as not [external](https://webpack.js.org/configuration/externals/). | ||
The lib will invoke Webpack's HMR to update client side, that will re-fetch (with cache killer) the updated translation files and trigger [`i18n.changelanguage(lang)`](https://www.i18next.com/overview/api#changelanguage) to trigger listeners (which in React apps it will update the UI). | ||
The lib will invoke Webpack's / Vite HMR to update client side, that will re-fetch (with cache killer) the updated translation files and trigger [`i18n.changelanguage(lang)`](https://www.i18next.com/overview/api#changelanguage) to trigger listeners (which in React apps it will update the UI). | ||
@@ -110,0 +116,0 @@ ## Example |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40672
5.98%347
13.77%118
5.36%16
-15.79%1
Infinity%