Skip to content

Commit

Permalink
haste-map: Support extracting dynamic imports (#5883)
Browse files Browse the repository at this point in the history
* cleanup type import in haste map

* Support extracting dynamic `import`s

* update changelog

* fix regexp
  • Loading branch information
SimenB authored and cpojer committed Mar 28, 2018
1 parent 815c8bd commit a9d1c5f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

* `[jest-haste-map]` Support extracting dynamic `import`s
([#5883](https://github.com/facebook/jest/pull/5883))
* `[expect]` Improve output format for mismatchedArgs in mock/spy calls.
([#5846](https://github.com/facebook/jest/pull/5846))
* `[jest-cli]` Add support for using `--coverage` in combination with watch
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import crypto from 'crypto';
import EventEmitter from 'events';
import getMockName from './get_mock_name';
import getPlatformExtension from './lib/get_platform_extension';
// eslint-disable-next-line import/no-duplicates
import H from './constants';
import HasteFS from './haste_fs';
import HasteModuleMap from './module_map';
Expand All @@ -41,8 +40,7 @@ import type {
MockData,
} from 'types/HasteMap';

// eslint-disable-next-line import/no-duplicates
import typeof HType from './constants';
type HType = typeof H;

type Options = {
cacheDirectory?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ it('extracts both requires and imports from code', () => {
const code = `
import module1 from 'module1';
const module2 = require('module2');
import('module3').then(module3 => {})';
`;

expect(extractRequires(code)).toEqual(['module1', 'module2']);
expect(extractRequires(code)).toEqual(['module1', 'module2', 'module3']);
});

it('extracts requires in order', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-haste-map/src/lib/extract_requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const blockCommentRe = /\/\*[^]*?\*\//g;
const lineCommentRe = /\/\/.*/g;

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

return Array.from(dependencies);
}

0 comments on commit a9d1c5f

Please sign in to comment.