Skip to content

Commit a9d1c5f

Browse files
SimenBcpojer
authored andcommitted
haste-map: Support extracting dynamic imports (#5883)
* cleanup type import in haste map * Support extracting dynamic `import`s * update changelog * fix regexp
1 parent 815c8bd commit a9d1c5f

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
* `[jest-haste-map]` Support extracting dynamic `import`s
6+
([#5883](https://github.com/facebook/jest/pull/5883))
57
* `[expect]` Improve output format for mismatchedArgs in mock/spy calls.
68
([#5846](https://github.com/facebook/jest/pull/5846))
79
* `[jest-cli]` Add support for using `--coverage` in combination with watch

packages/jest-haste-map/src/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import crypto from 'crypto';
1414
import EventEmitter from 'events';
1515
import getMockName from './get_mock_name';
1616
import getPlatformExtension from './lib/get_platform_extension';
17-
// eslint-disable-next-line import/no-duplicates
1817
import H from './constants';
1918
import HasteFS from './haste_fs';
2019
import HasteModuleMap from './module_map';
@@ -41,8 +40,7 @@ import type {
4140
MockData,
4241
} from 'types/HasteMap';
4342

44-
// eslint-disable-next-line import/no-duplicates
45-
import typeof HType from './constants';
43+
type HType = typeof H;
4644

4745
type Options = {
4846
cacheDirectory?: string,

packages/jest-haste-map/src/lib/__tests__/extract_requires.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ it('extracts both requires and imports from code', () => {
1515
const code = `
1616
import module1 from 'module1';
1717
const module2 = require('module2');
18+
import('module3').then(module3 => {})';
1819
`;
1920

20-
expect(extractRequires(code)).toEqual(['module1', 'module2']);
21+
expect(extractRequires(code)).toEqual(['module1', 'module2', 'module3']);
2122
});
2223

2324
it('extracts requires in order', () => {

packages/jest-haste-map/src/lib/extract_requires.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const blockCommentRe = /\/\*[^]*?\*\//g;
1111
const lineCommentRe = /\/\/.*/g;
1212

1313
const replacePatterns = {
14+
DYNAMIC_IMPORT_RE: /(?:^|[^.]\s*)(\bimport\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
1415
EXPORT_RE: /(\bexport\s+(?!type )(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g,
1516
IMPORT_RE: /(\bimport\s+(?!type )(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g,
1617
REQUIRE_EXTENSIONS_PATTERN: /(?:^|[^.]\s*)(\b(?:require\s*?\.\s*?(?:requireActual|requireMock)|jest\s*?\.\s*?(?:requireActual|requireMock|genMockFromModule))\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
@@ -30,7 +31,8 @@ export default function extractRequires(code: string): Array<string> {
3031
.replace(replacePatterns.EXPORT_RE, addDependency)
3132
.replace(replacePatterns.IMPORT_RE, addDependency)
3233
.replace(replacePatterns.REQUIRE_EXTENSIONS_PATTERN, addDependency)
33-
.replace(replacePatterns.REQUIRE_RE, addDependency);
34+
.replace(replacePatterns.REQUIRE_RE, addDependency)
35+
.replace(replacePatterns.DYNAMIC_IMPORT_RE, addDependency);
3436

3537
return Array.from(dependencies);
3638
}

0 commit comments

Comments
 (0)