Skip to content

Commit 76c9ed2

Browse files
author
Daniil Yankouski
committed
Fixed react-svg config
1 parent 8a0726d commit 76c9ed2

File tree

14 files changed

+281
-263
lines changed

14 files changed

+281
-263
lines changed

configs/vite/plugins.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import importToCDN, { autoComplete } from 'vite-plugin-cdn-import';
22
import viteCompression from 'vite-plugin-compression';
3-
import reactSvgPlugin from 'vite-plugin-react-svg';
3+
import reactSvgPlugin from './plugins/react-svg';
44
import tsconfigPaths from 'vite-tsconfig-paths';
55
import react from '@vitejs/plugin-react'
66

@@ -12,15 +12,13 @@ const BasePlugins = [
1212
reactSvgPlugin({
1313
defaultExport: 'component',
1414
expandProps: 'end',
15-
memo: false,
16-
ref: false,
15+
memo: true,
16+
ref: true,
1717
replaceAttrValues: null,
1818
svgProps: null,
1919
svgo: true,
2020
svgoConfig: {
21-
plugins: {
22-
removeViewBox: false,
23-
},
21+
removeViewBox: false,
2422
},
2523
titleProp: false,
2624
}),
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
3+
const { transform } = require('@svgr/core');
4+
const { readFileSync } = require('fs');
5+
6+
async function compileSvg(source, id, options) {
7+
const code = await transform(
8+
source,
9+
{
10+
...options,
11+
runtimeConfig: false,
12+
plugins: ['@svgr/plugin-svgo', '@svgr/plugin-jsx'],
13+
jsx: {
14+
babelConfig: {
15+
plugins: [
16+
[
17+
'@babel/plugin-transform-react-jsx',
18+
{
19+
useBuiltIns: true,
20+
},
21+
],
22+
],
23+
},
24+
},
25+
},
26+
{
27+
filePath: id,
28+
},
29+
);
30+
31+
return code;
32+
}
33+
34+
module.exports = (options = {}) => {
35+
const {
36+
defaultExport = 'url',
37+
svgoConfig,
38+
expandProps,
39+
svgo,
40+
ref,
41+
memo,
42+
replaceAttrValues,
43+
svgProps,
44+
titleProp,
45+
} = options;
46+
47+
const cache = new Map();
48+
const svgRegex = /\.svg(?:\?(component|url))?$/;
49+
50+
return {
51+
name: 'react-svg',
52+
async transform(source, id, isBuild) {
53+
const result = id.match(svgRegex);
54+
55+
if (result) {
56+
const type = result[1];
57+
58+
if (
59+
(defaultExport === 'url' && typeof type === 'undefined') ||
60+
type === 'url'
61+
) {
62+
return source;
63+
}
64+
65+
if (
66+
(defaultExport === 'component' && typeof type === 'undefined') ||
67+
type === 'component'
68+
) {
69+
const idWithoutQuery = id.replace('.svg?component', '.svg');
70+
let result = cache.get(idWithoutQuery);
71+
72+
if (!result) {
73+
const code = readFileSync(idWithoutQuery);
74+
75+
result = await compileSvg(code, idWithoutQuery, {
76+
svgoConfig,
77+
expandProps,
78+
svgo,
79+
ref,
80+
memo,
81+
replaceAttrValues,
82+
svgProps,
83+
titleProp,
84+
});
85+
86+
if (isBuild) {
87+
cache.set(idWithoutQuery, result);
88+
}
89+
}
90+
91+
return result;
92+
}
93+
}
94+
},
95+
};
96+
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
"@babel/preset-env": "7.16.11",
4848
"@babel/preset-typescript": "7.16.7",
4949
"@babel/register": "7.17.0",
50+
"@svgr/core": "^6.2.1",
51+
"@svgr/plugin-jsx": "^6.2.1",
52+
"@svgr/plugin-svgo": "^6.2.0",
5053
"@testing-library/react": "12.1.3",
5154
"@types/jest": "27.4.1",
5255
"@typescript-eslint/eslint-plugin": "5.13.0",
@@ -70,7 +73,6 @@
7073
"vite": "2.8.6",
7174
"vite-plugin-cdn-import": "0.3.5",
7275
"vite-plugin-compression": "0.5.1",
73-
"vite-plugin-react-svg": "0.2.0",
7476
"vite-tsconfig-paths": "3.4.1"
7577
},
7678
"prettier": {

src/core/adapters/i18next/i18next.adapter.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class I18nextAdapter implements I18n<i18next> {
1111

1212
@observable private _loading = true;
1313

14-
@observable private _changeLanguage = false;
14+
@observable private _language: I18nLanguages = I18nLanguages.en;
1515

1616
constructor() {
1717
makeAutoObservable(this);
@@ -22,8 +22,10 @@ export class I18nextAdapter implements I18n<i18next> {
2222
this._loading = value;
2323
};
2424

25-
@action private _setChangeLanguage = (value: boolean): void => {
26-
this._changeLanguage = value;
25+
@action private _setLanguage = async (value: I18nLanguages): Promise<void> => {
26+
await this._instance.changeLanguage(value);
27+
await this._instance.reloadResources();
28+
this._language = value;
2729
};
2830

2931
private initialize = async (): Promise<void> => {
@@ -84,29 +86,23 @@ export class I18nextAdapter implements I18n<i18next> {
8486
return this._loading;
8587
};
8688

87-
@computed isChanged = (): boolean => {
88-
return this._changeLanguage;
89-
};
90-
9189
getInstance = (): i18next => {
9290
return this._instance;
9391
};
9492

95-
getLanguage = (): I18nLanguages => {
96-
return this._instance.language as I18nLanguages;
93+
@computed getLanguage = (): I18nLanguages => {
94+
return this._language || (this._instance.language as I18nLanguages);
9795
};
9896

9997
changeLanguage = async (language: I18nLanguages): Promise<boolean> => {
10098
try {
101-
this._setChangeLanguage(true);
102-
await this._instance.changeLanguage(language);
103-
await this._instance.reloadResources();
99+
await this._setLanguage(language);
104100

105101
return true;
106102
} catch (error) {
103+
await this._setLanguage(I18nLanguages.en);
104+
107105
return false;
108-
} finally {
109-
this._setChangeLanguage(false);
110106
}
111107
};
112108
}

src/core/i18n/i18n.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export interface I18n<InstanceType = any> {
44
getInstance(): InstanceType;
55
isInitialized(): boolean;
66
isLoading(): boolean;
7-
isChanged(): boolean;
87
changeLanguage(language: I18nLanguages): Promise<boolean>;
98
getLanguage(): I18nLanguages;
109
}

src/presentation/web/components/common/button/button.component.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import isFunction from 'lodash/isFunction';
22
import React, { useCallback } from 'react';
3-
import DotsLoader from 'assets/images/dots-loader.svg';
3+
// import DotsLoader from 'assets/images/dots-loader.svg';
44
import { ButtonWrapper, LoaderWrapper } from './button.styles';
55
import { ButtonProps } from './button.typings';
66

@@ -17,11 +17,7 @@ export function Button(props: ButtonProps): React.ReactElement {
1717

1818
return (
1919
<ButtonWrapper {...rest} disabled={disabled} isLoading={loading} onClick={handleClick}>
20-
{loading && (
21-
<LoaderWrapper>
22-
<DotsLoader />
23-
</LoaderWrapper>
24-
)}
20+
{loading && <LoaderWrapper>{/* <DotsLoader /> */}</LoaderWrapper>}
2521
{children}
2622
</ButtonWrapper>
2723
);

src/presentation/web/components/i18n/i18n.hoc.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { observer } from 'mobx-react';
22
import React, { FunctionComponent, ReactElement } from 'react';
3-
import DotsLoader from 'assets/images/dots-loader.svg';
3+
// import DotsLoader from 'assets/images/dots-loader.svg';
44
import { getDisplayName } from 'presentation/web/hoc/hoc.helpers';
55
import { useTranslation } from './i18n.hook';
66

@@ -9,7 +9,7 @@ export function withI18n<T>(WrappedComponent: FunctionComponent<T>): FunctionCom
99
const { adapter } = useTranslation();
1010

1111
if (adapter.isLoading()) {
12-
return <DotsLoader fill="#ff9922" />;
12+
return null;
1313
}
1414

1515
return (
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { i18n as i18next } from 'i18next';
2-
import { I18n, I18nType } from 'core/i18n';
2+
import { I18n, I18nLanguages, I18nType } from 'core/i18n';
33
import { useInjection } from '..';
44

55
export function useTranslation(): {
66
t: i18next['t'];
77
i18n: i18next;
88
adapter: I18n;
9-
isChanged: boolean;
9+
language: I18nLanguages;
1010
} {
1111
const i18next: I18n<i18next> = useInjection(I18nType);
1212
const instance = i18next.getInstance();
1313

1414
return {
1515
adapter: i18next,
1616
i18n: instance,
17-
isChanged: i18next.isChanged(),
17+
language: i18next.getLanguage(),
1818
t: instance.t,
1919
};
2020
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { IoC, useInjection } from './ioc';
2-
export { Route } from './route';

src/presentation/web/components/route/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)