-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
39 lines (34 loc) · 1.09 KB
/
utils.js
File metadata and controls
39 lines (34 loc) · 1.09 KB
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
const path = require('path');
const tsconfig = require('../tsconfig.json');
module.exports = {
root,
mapTypescriptAliasToWebpackAlias,
};
const _root = path.resolve(__dirname, '..');
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [_root].concat(args));
}
function mapTypescriptAliasToWebpackAlias(alias = {}) {
const webpackAliases = { ...alias };
if (!tsconfig.compilerOptions.paths) {
return webpackAliases;
}
Object.entries(tsconfig.compilerOptions.paths)
.filter(([key, value]) => {
// use Typescript alias in Webpack only if this has value
return !!value.length;
})
.map(([key, value]) => {
// if Typescript alias ends with /* then remove this for Webpack
const regexToReplace = /\/\*$/;
const aliasKey = key.replace(regexToReplace, '');
const aliasValue = value[0].replace(regexToReplace, '');
return [aliasKey, root(aliasValue)];
})
.reduce((aliases, [key, value]) => {
aliases[key] = value;
return aliases;
}, webpackAliases);
return webpackAliases;
}