-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
webpack.rules.js
118 lines (115 loc) · 4.39 KB
/
webpack.rules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* eslint-disable security/detect-unsafe-regex */
/* eslint-disable unicorn/prefer-module */
/* eslint-disable unicorn/no-null */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/restrict-plus-operands */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-unsafe-return */
const tsImportPluginFactory = require('ts-import-plugin');
const styledComponentsTransformerFactory = require('typescript-plugin-styled-components').default;
const fs = require('fs');
const JSON5 = require('json5');
const isTest = process.env.NODE_ENV === 'test';
const isDevelopmentOrTest = process.env.NODE_ENV === 'development' || isTest;
module.exports = [
{
// used to load css from npm package, we use styled-components
test: /\.css$/,
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
},
// TODO: until I have time to add https://github.com/linjiajian999/esbuild-plugin-import , only do this in development mode
// eslint-disable-next-line no-constant-condition
isDevelopmentOrTest
? {
test: /\.(t|j)sx?$/,
exclude: /(node_modules|\.webpack)/,
use: {
loader: 'esbuild-loader',
options: {
loader: 'tsx', // Or 'ts' if you don't need tsx
/* ES2022/ESNEXT work well with inversifyjs, Wait until https://github.com/inversify/InversifyJS/pull/1499 fixed */
target: 'ES2021',
// tsconfigRaw: ts.readConfigFile('tsconfig.json', ts.sys.readFile.bind(ts.sys)),
tsconfigRaw: JSON5.parse(fs.readFileSync('./tsconfig.json')),
},
},
}
: {
test: /\.(t|j)sx?$/,
exclude: /(node_modules|\.webpack)/,
use: {
loader: 'ts-loader',
options: {
transpileOnly: true,
getCustomTransformers: () => ({
before: [
styledComponentsTransformerFactory(),
// lodash
tsImportPluginFactory({
style: false,
libraryName: 'lodash',
libraryDirectory: null,
camel2DashComponentName: false,
}),
// material-ui
tsImportPluginFactory({
libraryName: '@mui/material',
libraryDirectory: '',
camel2DashComponentName: false,
}),
// svg-icons
// FIXME: will cause `FolderIcon is not defined`, which cannot reproduce in MacOS and dev mode https://github.com/tiddly-gittly/TidGi-Desktop/issues/88
// tsImportPluginFactory({
// libraryDirectory: (importName) => {
// const stringVec = importName
// .split(/([A-Z][a-z]+|\d*)/)
// .filter((s) => s.length)
// .map((s) => s.toLocaleLowerCase());
// return stringVec.reduce((accumulator, current, index) => {
// if (index > 1) {
// return `${accumulator}-${current}`;
// } else if (index === 1) {
// return `${accumulator}/${current}`;
// }
// return accumulator + current;
// }, '');
// },
// libraryName: '@mui/icons-material',
// style: false,
// camel2DashComponentName: false,
// }),
// RXJS
tsImportPluginFactory([
{
libraryDirectory: '../_esm5/internal/operators',
libraryName: 'rxjs/operators',
camel2DashComponentName: false,
transformToDefaultImport: false,
},
{
libraryDirectory: '../_esm5/internal/observable',
libraryName: 'rxjs',
camel2DashComponentName: false,
transformToDefaultImport: false,
},
]),
],
}),
compilerOptions: {
module: 'esnext',
},
},
},
},
{
test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
type: 'asset',
},
{
test: /\.(png|jpe?g|gif|svg)$/,
type: 'asset',
},
];