forked from axisj/react-multi-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-app-rewire-typescript-hmr.js
58 lines (48 loc) · 1.31 KB
/
react-app-rewire-typescript-hmr.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
const path = require('path');
const tsLoaderMatcher = function(rule) {
// return rule.loader && rule.loader.indexOf(`ts-loader${path.sep}`) !== -1;
return rule.test && rule.test.toString() === /\.(ts|tsx)$/.toString();
};
const getLoader = function(rules, matcher) {
let loader;
rules.some(rule => {
return (loader = matcher(rule)
? rule
: getLoader(rule.use || rule.oneOf || [], matcher));
});
return loader;
};
const getTsLoader = function(rules) {
return getLoader(rules, tsLoaderMatcher);
};
module.exports = (config, env, babelPlugins = []) => {
if (env === 'production') return config;
const tsLoader = getTsLoader(config.module.rules);
if (!tsLoader) {
console.log('ts-loader not found');
return config;
}
if (tsLoader.loader) {
if (!tsLoader.use) tsLoader.use = [];
tsLoader.use.push(tsLoader.loader);
delete tsLoader.loader;
}
// Replace loader with array of loaders with use: []
tsLoader.use = [
{
loader: require.resolve('babel-loader'),
options: {
cacheDirectory: true,
plugins: ['react-hot-loader/babel', ...babelPlugins],
},
},
{
loader: require.resolve('ts-loader'),
options: {
transpileOnly: true,
configFile: 'tsconfig.dev.json',
},
},
];
return config;
};