-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule-name-mapper-helper.js
77 lines (66 loc) · 2.65 KB
/
module-name-mapper-helper.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
const path = require('path');
/**
* Resolve a Sass @import or @use rule.
*
* @param {String} to - The path to the current file
* @param {String} importPath - The path to resolve
* @param {String} fileType - The filetype of the current file
*/
function resolveSass(to, importPath, fileType) {
// Mimic Sass-loader's `~` syntax for bare imports.
const matchModuleImport = /^~/;
if (path.isAbsolute(importPath)) {
return importPath;
} if (matchModuleImport.test(importPath)) {
const dirname = path.dirname(importPath).replace(matchModuleImport, '');
const basename = path.basename(importPath);
const filenames = [];
if (!/\.(sc|sa|c)ss/.test(basename)) {
const extensions = ['scss', 'sass', 'css'].filter((e) => e !== fileType);
extensions.unshift(fileType);
extensions.forEach((ext) => {
filenames.push(`${basename}.${ext}`, `_${basename}.${ext}`);
});
} else {
filenames.push(basename, `_${basename}`);
}
for (const filename of filenames) {
try {
return require.resolve(path.join(dirname, filename));
} catch (_) {}
}
}
return path.join(path.dirname(to), importPath);
}
/**
* Applies the moduleNameMapper substitution from the jest config
*
* @param {String} source - the original string
* @param {String} filePath - the path of the current file (where the source originates)
* @param {Object} jestConfig - the jestConfig holding the moduleNameMapper settings
* @param {Object} fileType - extn of the file to be resolved
* @returns {String} path - the final path to import (including replacements via moduleNameMapper)
*/
module.exports = function applyModuleNameMapper(
source,
filePath,
jestConfig = {},
fileType = '',
) {
if (!jestConfig.moduleNameMapper) return source;
// Extract the moduleNameMapper settings from the jest config. TODO: In case of development via babel@7, somehow the jestConfig.moduleNameMapper might end up being an Array. After a proper upgrade to babel@7 we should probably fix this.
const module = Array.isArray(jestConfig.moduleNameMapper) ?
jestConfig.moduleNameMapper :
Object.entries(jestConfig.moduleNameMapper);
const importPath = module.reduce((acc, [regex, replacement]) => {
const matches = acc.match(regex);
if (matches === null) {
return acc;
}
return replacement.replace(
/\$([0-9]+)/g,
(_, index) => matches[parseInt(index, 10)],
);
}, source);
return resolveSass(filePath, importPath, fileType);
};