-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.js
78 lines (69 loc) · 2.37 KB
/
resolver.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
78
// @flow
const fs = require('fs');
const path = require('path');
const resolveFrom = require('resolve-from');
/*
These modules should not have their imports resolved differently
as their src is in root of the package.
*/
const blockedFromMultiEntryPointsModuleList = [
'build-utils',
'polyfills',
'webdriver-runner',
'visual-regression',
];
/** This file is used to resolve imports in jest.
* This is used to make sure that packages resolve using the same algorithm as our webpack config
* (checking for "atlaskit:src", etc) meaning that we dont need the old root index.js hack anymore
*/
// This is the resolver used by webpack, which we configure similarly
// to AK website (see ./website/webpack.config.js - "resolve" field)
const wpResolver = require('enhanced-resolve').ResolverFactory.createResolver({
fileSystem: fs,
useSyncFileSystemCalls: true,
mainFields: ['atlaskit:src', 'browser', 'main'],
extensions: ['.js', '.ts', '.tsx', '.json'],
});
module.exports = function resolver(
modulePath /*: string */,
params /*: any */,
) {
// If resolving relative paths, make sure we use resolveFrom and not resolve
if (modulePath.startsWith('.') || modulePath.startsWith(path.sep)) {
try {
return resolveFrom(params.basedir, modulePath);
} catch (e) {} // eslint-disable-line
}
/*
The alternative entry files are only created at publish time. So for jest we resolve
import { N0 } from '@atlaskit/theme/colors'
to
import { N0 } from '@atlaskit/theme/src/colors'
*/
let alternativeEntryModulePath;
const paths = modulePath.split('/');
const [, moduleName, entryPoint] = paths;
if (
modulePath.startsWith('@atlaskit/') &&
paths.length === 3 &&
typeof entryPoint === 'string' &&
entryPoint.trim().length > 0
) {
if (blockedFromMultiEntryPointsModuleList.indexOf(moduleName) === -1) {
alternativeEntryModulePath = `@atlaskit/${moduleName}/src/${entryPoint}`;
}
}
// Otherwise try to resolve to source files of AK packages using webpack resolver
let result = wpResolver.resolveSync(
{},
params.basedir,
alternativeEntryModulePath || modulePath,
);
if (result) {
// Dereference symlinks to ensure we don't create a separate
// module instance depending on how it was referenced.
// @link https://github.com/facebook/jest/pull/4761
result = fs.realpathSync(result);
}
return result;
};