Skip to content

Commit 7432dd8

Browse files
committed
improve code config
- use @svgr version 6 - use constants env - mock route for testing - edit setting files loader and copy plugin
1 parent c7c95ff commit 7432dd8

File tree

10 files changed

+1198
-228
lines changed

10 files changed

+1198
-228
lines changed

config/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const dotenvFiles = [
3030
// since normally you expect tests to produce the same
3131
// results for everyone
3232
// NODE_ENV !== 'test' && `${paths.dotenv}.local`,
33-
`${paths.dotenv}.${NODE_ENV}`,
33+
`${paths.dotenv}`,
3434
// paths.dotenv,
3535
].filter(Boolean);
3636

config/jest/routeMock.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const fs = require('fs');
2+
const path = require('../paths');
3+
4+
const { appSrc } = path;
5+
const featuresDir = `${appSrc}/features`;
6+
7+
const routeFileName = 'route.ts';
8+
9+
const RouteModules = [];
10+
function readFolder(dir) {
11+
const files = fs.readdirSync(dir);
12+
files.forEach(file => {
13+
const filePath = `${dir}/${file}`;
14+
const stat = fs.statSync(filePath);
15+
if (stat.isDirectory()) {
16+
readFolder(filePath);
17+
} else {
18+
if (file === routeFileName) {
19+
const route = require(filePath);
20+
RouteModules.push(route);
21+
}
22+
}
23+
});
24+
}
25+
26+
readFolder(featuresDir);
27+
28+
const appRoutes = RouteModules.reduce((prev, module) => {
29+
prev.push(...module.default);
30+
return prev;
31+
}, []);
32+
33+
module.exports = appRoutes;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@babel/plugin-transform-runtime": "^7.13.10",
4242
"@babel/preset-env": "^7.13.9",
4343
"@babel/preset-react": "^7.12.13",
44-
"@svgr/webpack": "^5.5.0",
44+
"@svgr/webpack": "^6.2.1",
4545
"@testing-library/jest-dom": "^5.11.10",
4646
"@testing-library/react": "^12.0.0",
4747
"@types/enzyme": "^3.10.9",

src/constants/env.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { NODE_ENV, API_SERVER_URL, PORT } = process.env;
2+
3+
export default {
4+
NodeEnv: NODE_ENV,
5+
ApiServerUrl: API_SERVER_URL,
6+
Port: PORT,
7+
};

src/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const config = {
1717
'^.+\\.(css|less|scss)$': '<rootDir>/config/jest/cssMock.js',
1818
'^@/static/(.*)$': '<rootDir>/public/static/$1',
1919
'^@/(.*)$': '<rootDir>/src/$1',
20+
'^.+\\/features/\\*\\*/route.ts$': '<rootDir>/config/jest/routeMock.js',
2021
},
2122
};
2223

src/locales/i18n.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import i18n from 'i18next';
22
import { initReactI18next } from 'react-i18next';
3+
import env from '@/constants/env';
34

45
import resources, { langs } from './resources';
5-
const isProduction = process.env.NODE_ENV === 'production';
6+
const isProduction = env.NodeEnv === 'production';
67

78
i18n.use(initReactI18next).init({
89
resources,

src/services/axios-base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import env from '@/constants/env';
12
import { getHeaders } from '@/utils';
23

34
import axios from 'axios';
45
// Make an 'instance' of axios
56
const instance = axios.create({
67
// .. where we make our configurations
7-
baseURL: process.env.API_SERVER_URL,
8+
baseURL: env.ApiServerUrl,
89
});
910

1011
// Where you would set stuff like your 'Authorization' header, etc ...

webpack/webpack.common.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = {
5252
},
5353
{
5454
test: /\.svg$/,
55+
issuer: /\.[jt]sx?$/,
5556
use: ['@svgr/webpack'],
5657
},
5758
{
@@ -69,10 +70,23 @@ module.exports = {
6970
{
7071
test: /\.(woff|woff2|eot|ttf|otf)$/i,
7172
type: 'asset/resource',
73+
generator: {
74+
filename: 'static/fonts/[hash][ext][query]',
75+
},
76+
},
77+
{
78+
test: /\.(png|jpg|jpeg)$/i,
79+
type: 'asset/resource',
80+
generator: {
81+
filename: 'static/images/[hash][ext][query]',
82+
},
7283
},
7384
{
74-
test: /\.(png|jpg|jpeg|gif)$/i,
85+
test: /\.(gif)$/i,
7586
type: 'asset/resource',
87+
generator: {
88+
filename: 'static/gifs/[hash][ext][query]',
89+
},
7690
},
7791
],
7892
},

webpack/webpack.prod.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const ImageminWebpack = require('image-minimizer-webpack-plugin');
1313
const paths = require('../config/paths');
1414
const { appBuild, appPublic, appHtml } = paths;
1515

16+
const imageTypeIgnoreCopy = ['.png', '.jpg', '.jpeg', '.gif', '.svg'];
1617
// Source maps are resource heavy and can cause out of memory issue for large source files.
1718

1819
module.exports = merge(common, {
@@ -29,6 +30,12 @@ module.exports = merge(common, {
2930
{
3031
from: path.resolve(appPublic, 'static'),
3132
to: path.resolve(appBuild, 'static'),
33+
globOptions: {
34+
ignore: [
35+
...imageTypeIgnoreCopy.map(ext => `**/images/*/*${ext}`),
36+
'**/fonts/**',
37+
],
38+
},
3239
toType: 'dir',
3340
},
3441
],
@@ -67,6 +74,7 @@ module.exports = merge(common, {
6774
filename: 'static/js/[name].[chunkhash].js',
6875
// There are also additional JS chunk files if you use code splitting.
6976
chunkFilename: 'static/js/[name].[chunkhash].chunk.js',
77+
clean: true,
7078
},
7179
optimization: {
7280
nodeEnv: 'production',

0 commit comments

Comments
 (0)