Skip to content

Commit

Permalink
Support absolute path to custom extension through haste package (jest…
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored and cpojer committed May 11, 2017
1 parent 7dfcb70 commit 3412be4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions integration_tests/resolve/__tests__/resolve-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ test('should resolve filename.<platform>.js', () => {
expect(platform.extension).toBe('android.js');
});

test('should resolve filename.<platform>.js from haste package', () => {
expect(testRequire('custom-resolve/test1')).not.toThrow();
expect(platform.extension).toBe('android.js');
});

test('should resolve filename.native.js', () => {
expect(testRequire('../test2')).not.toThrow();
expect(platform.extension).toBe('native.js');
Expand Down
1 change: 1 addition & 0 deletions integration_tests/resolve/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "custom-resolve",
"jest": {
"haste": {
"platforms": ["native"],
Expand Down
23 changes: 16 additions & 7 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,19 @@ class Resolver {
const skipResolution =
options && options.skipNodeResolution && !moduleName.includes(path.sep);

if (!skipResolution) {
module = Resolver.findNodeModule(moduleName, {
const resolveNodeModule = name => {
return Resolver.findNodeModule(name, {
basedir: dirname,
browser: this._options.browser,
extensions,
moduleDirectory,
paths,
resolver: this._options.resolver,
});
};

if (!skipResolution) {
module = resolveNodeModule(moduleName);

if (module) {
return (this._moduleNameCache[key] = module);
Expand All @@ -156,12 +160,17 @@ class Resolver {
// 3. Resolve "haste packages" which are `package.json` files outside of
// `node_modules` folders anywhere in the file system.
const parts = moduleName.split('/');
module = this.getPackage(parts.shift());
if (module) {
const hastePackage = this.getPackage(parts.shift());
if (hastePackage) {
try {
return (this._moduleNameCache[key] = require.resolve(
path.join.apply(path, [path.dirname(module)].concat(parts)),
));
const module = path.join.apply(
path,
[path.dirname(hastePackage)].concat(parts),
);
// try resolving with custom resolver first to support extensions,
// then fallback to require.resolve
return (this._moduleNameCache[key] =
resolveNodeModule(module) || require.resolve(module));
} catch (ignoredError) {}
}

Expand Down

0 comments on commit 3412be4

Please sign in to comment.