Skip to content

Commit

Permalink
Add named exports for createInstance (#1681)
Browse files Browse the repository at this point in the history
* Add named exports for createInstance

* Add hybrid module support

* createInstance is a static method

* Make createInstance a static property

* Fix

* Fix lint

* Slight improvement

* Update

* Swap esm module with package.json

* Update imports mjs -> js

* Swap index.esm.js with index.cjs.js

* Remove exports default option

* Bind all instance member functions
  • Loading branch information
perrin4869 committed Nov 15, 2021
1 parent 9ad73d5 commit e401175
Show file tree
Hide file tree
Showing 8 changed files with 2,614 additions and 919 deletions.
210 changes: 121 additions & 89 deletions i18next.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

3,262 changes: 2,450 additions & 812 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 15 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
"main": "./dist/cjs/i18next.js",
"module": "./dist/esm/i18next.js",
"types": "./index.d.ts",
"exports": {
".": {
"import": "./dist/esm/i18next.js",
"require": "./dist/cjs/i18next.js",
"types": "./index.d.ts"
}
},
"keywords": [
"i18next",
"internationalization",
Expand All @@ -25,15 +32,14 @@
"@babel/runtime": "^7.12.0"
},
"devDependencies": {
"@babel/core": "^7.10.1",
"@babel/plugin-proposal-async-generator-functions": "^7.2.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/core": "^7.16.0",
"@babel/plugin-proposal-async-generator-functions": "^7.16.0",
"@babel/plugin-transform-modules-commonjs": "^7.16.0",
"@babel/plugin-transform-runtime": "^7.16.0",
"@babel/polyfill": "^7.2.5",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"@babel/preset-env": "^7.16.0",
"@babel/preset-react": "^7.16.0",
"@babel/register": "^7.16.0",
"@rollup/plugin-babel": "^5.2.2",
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.0",
Expand Down Expand Up @@ -89,7 +95,7 @@
"test:typescript:noninterop": "tslint --project tsconfig.nonEsModuleInterop.json",
"tdd": "karma start karma.conf.js",
"tdd:compat": "karma start karma.backward.conf.js",
"build": "rimraf dist && rollup -c && cpy \"./dist/umd/*.js\" ./",
"build": "rimraf dist && rollup -c && echo '{\"type\":\"module\"}' > dist/esm/package.json && cpy \"./dist/umd/*.js\" ./",
"preversion": "npm run test && npm run build && git push",
"postversion": "git push && git push --tags && npm run release",
"prettier": "prettier --write \"{,**/}*.{ts,tsx,js,json,md}\"",
Expand Down
7 changes: 4 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ const getBabelOptions = ({ useESModules }) => ({
});

const input = './src/index.js';
const inputCjs = './src/index.cjs.js';
const name = 'i18next';
// check relative and absolute paths for windows and unix
const external = (id) => !id.startsWith('.') && !id.startsWith('/') && !id.includes(':');

export default [
{
input,
input: inputCjs,
output: { format: 'cjs', file: pkg.main },
external,
plugins: [babel(getBabelOptions({ useESModules: false }))],
Expand All @@ -44,12 +45,12 @@ export default [
},

{
input,
input: inputCjs,
output: { format: 'umd', name, file: `dist/umd/${name}.js` },
plugins: [babel(getBabelOptions({ useESModules: true })), nodeResolve()],
},
{
input,
input: inputCjs,
output: { format: 'umd', name, file: `dist/umd/${name}.min.js` },
plugins: [babel(getBabelOptions({ useESModules: true })), nodeResolve(), terser()],
},
Expand Down
10 changes: 5 additions & 5 deletions src/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,7 @@ class I18n extends EventEmitter {
: 'ltr';
}

/* eslint class-methods-use-this: 0 */
createInstance(options = {}, callback) {
return new I18n(options, callback);
}
static createInstance = (options = {}, callback) => new I18n(options, callback)

cloneInstance(options = {}, callback = noop) {
const mergedOptions = { ...this.options, ...options, ...{ isClone: true } };
Expand Down Expand Up @@ -599,4 +596,7 @@ class I18n extends EventEmitter {
}
}

export default new I18n();
const instance = I18n.createInstance();
instance.createInstance = I18n.createInstance;

export default instance;
3 changes: 3 additions & 0 deletions src/index.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import i18next from './i18next.js';

export default i18next;
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import i18next from './i18next.js';

export default i18next;

export const createInstance = i18next.createInstance;

export const init = i18next.init;
export const loadResources = i18next.loadResources;
export const reloadResources = i18next.reloadResources;
export const use = i18next.use;
export const changeLanguage = i18next.changeLanguage;
export const getFixedT = i18next.getFixedT;
export const t = i18next.t;
export const exists = i18next.exists;
export const setDefaultNamespace = i18next.setDefaultNamespace;
export const hasLoadedNamespace = i18next.hasLoadedNamespace;
export const loadNamespaces = i18next.loadNamespaces;
export const loadLanguages = i18next.loadLanguages;

0 comments on commit e401175

Please sign in to comment.