Skip to content

Commit 0b85995

Browse files
author
Evan Jacobs
committed
Add support for the "module" package.json field
This is used in modern es module codebases to designate the entry point for code that makes use of import/export instead of commonjs.
1 parent f8cdd23 commit 0b85995

File tree

13 files changed

+80
-2
lines changed

13 files changed

+80
-2
lines changed

examples/module-field/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE file in the root directory of this source tree. An additional grant
5+
* of patent rights can be found in the PATENTS file in the same directory.
6+
*/
7+
import Contents from '../sample';
8+
9+
describe('module field resolution', () => {
10+
it('should return the correct file exports', () => {
11+
expect(Contents).toBe('👏');
12+
});
13+
});

examples/module-field/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"devDependencies": {
3+
"babel-jest": "*",
4+
"babel-preset-es2015": "*",
5+
"jest": "*"
6+
},
7+
"jest": {
8+
"module": true
9+
},
10+
"scripts": {
11+
"test": "jest"
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE file in the root directory of this source tree. An additional grant
5+
* of patent rights can be found in the PATENTS file in the same directory.
6+
*/
7+
export default '👏';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"main": "wrong.js",
3+
"module": "correct.js"
4+
}

examples/module-field/sample/wrong.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
3+
* This source code is licensed under the BSD-style license found in the
4+
* LICENSE file in the root directory of this source tree. An additional grant
5+
* of patent rights can be found in the PATENTS file in the same directory.
6+
*/
7+
'use strict';
8+
9+
module.exports = '😒';

packages/jest-config/src/__tests__/normalize-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,17 @@ describe('testPathIgnorePatterns', () => {
358358
});
359359
});
360360

361+
describe('module', () => {
362+
it('falsy module is not overwritten', () => {
363+
const config = normalize({
364+
module: true,
365+
rootDir: '/root/path/foo',
366+
});
367+
368+
expect(config.module).toBe(true);
369+
});
370+
});
371+
361372
describe('modulePathIgnorePatterns', () => {
362373
it('does not normalize paths relative to rootDir', () => {
363374
// This is a list of patterns, so we can't assume any of them are

packages/jest-config/src/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = ({
3232
providesModuleNodeModules: [],
3333
},
3434
mocksPattern: '__mocks__',
35+
module: false,
3536
moduleDirectories: ['node_modules'],
3637
moduleFileExtensions: [
3738
'js',

packages/jest-config/src/normalize.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ function normalize(config: InitialConfig, argv: Object = {}) {
331331
case 'haste':
332332
case 'logHeapUsage':
333333
case 'logTransformErrors':
334+
case 'module':
334335
case 'mocksPattern':
335336
case 'moduleDirectories':
336337
case 'moduleFileExtensions':

packages/jest-config/src/validConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = ({
4545
logHeapUsage: true,
4646
logTransformErrors: true,
4747
mocksPattern: '__mocks__',
48+
module: false,
4849
moduleDirectories: ['node_modules'],
4950
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
5051
moduleLoader: '<rootDir>',

0 commit comments

Comments
 (0)