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.11.4 to 2.0.0

lib/webpack/loader.js

7

CHANGELOG.md

@@ -7,4 +7,11 @@ ### Changelog

#### [v2.0.0](https://github.com/felixmosh/i18next-hmr/compare/v1.11.4...v2.0.0)
- feat: use i18next plugin system to get the i18next instance. [`2c60c1f`](https://github.com/felixmosh/i18next-hmr/commit/2c60c1f5779e38de687304aaf5cf483c974aa47d)
#### [v1.11.4](https://github.com/felixmosh/i18next-hmr/compare/v1.11.3...v1.11.4)
> 22 May 2023
- Release 1.11.4 [`d3863d9`](https://github.com/felixmosh/i18next-hmr/commit/d3863d9d739c56d7be5537a53216fbae8e484ef5)
- fix: fix plugin options type [`6fe639b`](https://github.com/felixmosh/i18next-hmr/commit/6fe639b144921d7e7b3e67422e0592b053c9e31a)

@@ -11,0 +18,0 @@

109

lib/plugin.js

@@ -1,96 +0,29 @@

const path = require('path');
const fs = require('fs');
class HMRPlugin {
constructor(hmrOptions = {}) {
this.type = '3rdParty';
const pluginName = 'I18nextHMRPlugin';
if (hmrOptions.client && typeof window !== 'undefined') {
const applyClientHMR = require('./client-hmr');
applyClientHMR(() => this.i18nInstance);
} else if (hmrOptions.server && typeof window === 'undefined') {
const applyServerHMR = require('./server-hmr');
applyServerHMR(() => {
return this.i18nInstance;
});
}
}
const DEFAULT_OPTIONS = {
localesDir: '',
localesDirs: [],
};
init(i18nInstance) {
this.i18nInstance = i18nInstance;
}
class I18nextHMRPlugin {
constructor(options) {
this.options = { ...DEFAULT_OPTIONS, ...options };
this.options.localesDirs = []
.concat(this.options.localesDirs, this.options.localesDir)
.filter(Boolean);
this.lastUpdate = { changedFiles: [] };
toJSON() {
return null;
}
apply(compiler) {
const isWebpack5 = compiler.webpack
? +compiler.webpack.version.split('.').reverse().pop() === 5
: false;
compiler.hooks.beforeCompile.tapAsync(pluginName, (params, callback) => {
const noneExistsDirs = this.options.localesDirs.filter((dir) => !fs.existsSync(dir));
if (noneExistsDirs.length === 0) {
return callback();
}
throw new Error(
`i18next-hmr: \n'${noneExistsDirs.join(`',\n'`)}'${
noneExistsDirs.length > 1 ? '\nare' : ''
} not found`
);
});
compiler.hooks.watchRun.tap(pluginName, (compiler) => {
const watcher = (compiler.watchFileSystem.wfs || compiler.watchFileSystem).watcher;
const changedTimes = isWebpack5 ? watcher.getTimes() : watcher.mtimes;
const { startTime = 0 } = watcher || {};
const files = Object.keys(changedTimes).filter((file) => {
const fileExt = path.extname(file);
return (
this.options.localesDirs.some((dir) => file.startsWith(dir)) &&
!!fileExt &&
changedTimes[file] > startTime
);
});
if (!files.length) {
return;
}
const changedFiles = files.map((file) => {
const fileExt = path.extname(file);
const dir = this.options.localesDirs.find((dir) => file.startsWith(dir));
return path.relative(dir, file).slice(0, -1 * fileExt.length || undefined); // keep all when fileExt.length === 0
});
this.lastUpdate = { changedFiles };
I18nextHMRPlugin.callbacks.forEach((cb) => cb({ changedFiles }));
});
compiler.hooks.normalModuleFactory.tap(pluginName, (nmf) => {
nmf.hooks.createModule.tap(pluginName, (module) => {
const triggerPath = path.resolve(__dirname, 'trigger.js');
if (module.resource !== triggerPath) {
return;
}
module.loaders.push({
loader: path.resolve(__dirname, 'loader.js'), // Path to loader
options: {
localesDirs: this.options.localesDirs,
getChangedLang: () => ({ ...this.lastUpdate }),
},
});
});
});
toString() {
return 'HMRPlugin';
}
}
I18nextHMRPlugin.callbacks = [];
I18nextHMRPlugin.addListener = function (cb) {
I18nextHMRPlugin.callbacks.length = 0;
I18nextHMRPlugin.callbacks.push(cb);
};
module.exports = I18nextHMRPlugin;
module.exports.HMRPlugin = HMRPlugin;

@@ -50,5 +50,5 @@ const { extractList, printList, uniqueList, createLoggerOnce } = require('./utils');

const HMRPlugin = require('./plugin');
const HMRPlugin = require('./webpack/plugin');
HMRPlugin.addListener(reloadServerTranslation);
}
};
{
"name": "i18next-hmr",
"version": "1.11.4",
"version": "2.0.0",
"description": "I18Next HMR webpack plugin that allows reloading translation resources on the client & the server.",

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

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

export class I18NextHMRPlugin {
static addListener(cb: (data: { lang: string; ns: string }) => void): void;
constructor(options: { localesDir: string; } | { localesDirs: string[] });
export class HMRPlugin {
constructor(options: Partial<{ client: boolean; server: boolean }>);
init(i18n);
}

@@ -1,3 +0,1 @@

module.exports = {
I18NextHMRPlugin: require('./lib/plugin'),
};
module.exports = require('./lib/plugin');

@@ -22,3 +22,3 @@ # i18next-hmr

Add the plugin to your webpack config (or nextjs).
Add the plugin to your webpack config (or next.config.js).

@@ -29,3 +29,3 @@ <!-- prettier-ignore-start -->

// webpack.config.js
const { I18NextHMRPlugin } = require('i18next-hmr/plugin');
const { I18NextHMRPlugin } = require('i18next-hmr/webpack');

@@ -49,8 +49,18 @@ module.exports = {

// i18next.config.js
const Backend = require('i18next-http-backend');
const i18next = require('i18next');
i18next.init(options, callback);
const { HMRPlugin } = require('i18next-hmr/plugin');
const instance = i18next.use(Backend); // http-backend is required for client side reloading
if (process.env.NODE_ENV !== 'production') {
const { applyClientHMR } = require('i18next-hmr/client');
applyClientHMR(i18next);
instance.use(new HMRPlugin({
client: typeof window !== 'undefined', // enabled client side HMR
server: typeof window === 'undefined' // enabled server side HMR
}));
}
instance.init(options, callback);
module.exports = instance;
```

@@ -64,9 +74,2 @@

const i18n = require('./i18n');
if (process.env.NODE_ENV !== 'production') {
const { applyServerHMR } = require('i18next-hmr/server');
applyServerHMR(i18n);
}
const port = process.env.PORT || 3000;

@@ -92,4 +95,4 @@

1. if you are using [webpack-node-externals](https://github.com/liady/webpack-node-externals) specify `i18next-hmr` in the [`whitelist`](https://github.com/liady/webpack-node-externals#optionswhitelist-).
2. use a relative path to `node_modules`, something like:
1. If you are using [webpack-node-externals](https://github.com/liady/webpack-node-externals) specify `i18next-hmr` in the [`whitelist`](https://github.com/liady/webpack-node-externals#optionswhitelist-).
2. (deprecated method) use a relative path to `node_modules`, something like:
```js

@@ -96,0 +99,0 @@ // server.entry.js

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