-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (58 loc) · 2.57 KB
/
index.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
const fs = require('fs');
const path = require('path');
const flatten = require('lodash.flatten');
const transform = require('transform-jest-deps');
/**
* Creates a new preprocessor, based on options given by the user.
* @param {Object} config - Configuration object containing resolve rules. Can be an imported `webpack.config.js`.
* @param {Object} config.resolve - A required property of config.
* @param {string} [config.resolve.root] - The objective path to the root directory of your project. You can use `process.cwd()` to get the current working directory. This is used for the original Webpack configurations.
* @param {string[]} [config.resolve.modules] - An array of paths providing context for resolving paths. By default contains only the `node_modules` directory, you should add some appropriate directory in your project in which aliases are contained. This directory can be the same that you would use for `config.resolve.root`. This property appears in Webpack 2 configurations.
* @param {Object} config.resolve.alias - An object containing alias rules as string properties, where the key is an alias to be substituted, and the value is a path to the aliased file or directory. Each path has to be relative to the `config.resolve.root` property.
* @returns {Object} - An object containing a `process` property, used as a preprocessor.
*/
function preprocessorFactory(config) {
const roots = config.resolve.root
? [config.resolve.root]
: config.resolve.modules;
const aliases = flatten(
roots.map(root => Object.keys(config.resolve.alias)
.map(key => {
const value = path.join(root, config.resolve.alias[key]);
try {
fs.statSync(value);
return {
key,
value,
};
}
catch (e) {
return undefined;
}
})
.filter(Boolean)
)
);
/**
* A callback function for transform-jest-deps.
* @private
* @param {string} require - Content of a `require()` statement. Automatically passed by `transform-jest-deps`.
* @returns {string} - Value passed in, either modifed by the function, or not.
*/
function resolve(require) {
for (let i = 0; i < aliases.length; i++) {
const alias = aliases[i];
const regex = new RegExp(`^${alias.key}$|^${alias.key}(\\/)`);
if (regex.test(require)) {
return path.normalize(require.replace(regex, `${alias.value}$1`)).replace(/\\/g,'/');
}
}
return require;
}
return {
process(src) {
return transform(src, resolve);
},
};
}
module.exports = preprocessorFactory;