-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pass
conditions
when resolving modules (#11859)
- Loading branch information
Showing
17 changed files
with
262 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {resolve} from 'path'; | ||
import {onNodeVersions} from '@jest/test-utils'; | ||
import {runYarnInstall} from '../Utils'; | ||
import runJest from '../runJest'; | ||
|
||
const dir = resolve(__dirname, '..', 'resolve-conditions'); | ||
|
||
beforeAll(() => { | ||
runYarnInstall(dir); | ||
}); | ||
|
||
// The versions where vm.Module exists and commonjs with "exports" is not broken | ||
onNodeVersions('^12.16.0 || >=13.7.0', () => { | ||
test('resolves package exports correctly with custom resolver', () => { | ||
// run multiple times to ensure there are no caching errors | ||
for (let i = 0; i < 5; i++) { | ||
const {exitCode} = runJest(dir, [], { | ||
nodeOptions: '--experimental-vm-modules', | ||
}); | ||
try { | ||
expect(exitCode).toBe(0); | ||
} catch (error) { | ||
console.log(`Test failed on iteration ${i + 1}`); | ||
throw error; | ||
} | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
const {fn} = require('../fake-dep'); | ||
|
||
test('returns correct message', () => { | ||
expect(fn()).toEqual('hello from CJS'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {fn} from '../fake-dep'; | ||
|
||
test('returns correct message', () => { | ||
expect(fn()).toEqual('hello from ESM'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
module.exports = { | ||
fn: () => 'hello from CJS', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export function fn() { | ||
return 'hello from ESM'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "fake-dep", | ||
"version": "1.0.0", | ||
"exports": { | ||
".": { | ||
"import": "./module.mjs", | ||
"require": "./module.cjs" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"jest": { | ||
"moduleFileExtensions": [ | ||
"js", | ||
"cjs", | ||
"mjs", | ||
"json" | ||
], | ||
"resolver": "<rootDir>/resolver.js", | ||
"testMatch": [ | ||
"<rootDir>/**/*.test.*" | ||
], | ||
"transform": {} | ||
}, | ||
"dependencies": { | ||
"resolve.exports": "^1.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const {resolve: resolveExports} = require('resolve.exports'); | ||
|
||
module.exports = (path, options) => { | ||
return options.defaultResolver(path, { | ||
...options, | ||
pathFilter: options.conditions | ||
? createPathFilter(options.conditions) | ||
: undefined, | ||
}); | ||
}; | ||
|
||
function createPathFilter(conditions) { | ||
return function pathFilter(pkg, _path, relativePath) { | ||
// this `index` thing can backfire, but `resolve` adds it: https://github.com/browserify/resolve/blob/f1b51848ecb7f56f77bfb823511d032489a13eab/lib/sync.js#L192 | ||
const path = relativePath === 'index' ? '.' : relativePath; | ||
|
||
return ( | ||
resolveExports(pkg, path, { | ||
conditions, | ||
// `resolve.exports adds `import` unless `require` is `false`, so let's add this ugly thing | ||
require: !conditions.includes('import'), | ||
}) || relativePath | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# This file is generated by running "yarn install" inside your project. | ||
# Manual changes might be lost - proceed with caution! | ||
|
||
__metadata: | ||
version: 4 | ||
cacheKey: 7 | ||
|
||
"resolve.exports@npm:^1.0.2": | ||
version: 1.0.2 | ||
resolution: "resolve.exports@npm:1.0.2" | ||
checksum: 012a46e3ae41c53762abf5b50ea1b4adf2de617bbea1dbc7bf6e609c1ceaedee7782acbc92d443951d5dd0c3a8fb1090ce73285a9ccc24b530e33b5e09ae196f | ||
languageName: node | ||
linkType: hard | ||
|
||
"root-workspace-0b6124@workspace:.": | ||
version: 0.0.0-use.local | ||
resolution: "root-workspace-0b6124@workspace:." | ||
dependencies: | ||
resolve.exports: ^1.0.2 | ||
languageName: unknown | ||
linkType: soft |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.