-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpackConfigUtils.js
47 lines (40 loc) · 1.23 KB
/
webpackConfigUtils.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
/* eslint-disable one-var */
const path = require('path');
const { readdirSync, statSync } = require('fs');
const tsConfig = require('./tsconfig.json');
const generateWebpackAliasesFromTSConfig = () => {
const paths = tsConfig.compilerOptions.paths || {};
const baseURL = tsConfig.compilerOptions.baseUrl || '.';
const fromPaths = Object.keys(paths).reduce((aliases, key) => {
const targets = paths[key];
if (targets.length !== 1) {
throw new Error(`Multiple aliases for ${key}`);
}
const alias = key.replace(/\/\*$/, '');
const targetPath = path.resolve(__dirname, baseURL, targets[0]).replace(/\/\*$/, '');
return {
...aliases,
[alias]: targetPath,
};
}, {});
const fromDirs = baseURL === '.'
? {}
: readdirSync(path.resolve(__dirname, baseURL)).reduce(
(aliases, dir) => {
const target = path.resolve(__dirname, baseURL, dir);
const stat = statSync(target);
if (stat.isDirectory()) {
return {
...aliases,
[dir]: target,
};
}
return aliases;
},
{}
);
return {...fromPaths, ...fromDirs};
};
module.exports = {
generateWebpackAliasesFromTSConfig,
};