Created
October 20, 2017 00:03
-
-
Save loklaan/9fe7768576466c5a31c2e7af4cfdecd0 to your computer and use it in GitHub Desktop.
Jest Resolver w/ module field support
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
/* | |
|------------------------------------------------------------------------------- | |
| Custom 'module resolver' for Jest | |
|------------------------------------------------------------------------------- | |
| | |
| Forked from Jest, this is the default 'resolver' with the added benefit of | |
| remapping the "main" field to the value of the "module" field, when present. | |
| | |
*/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const resolve = require('resolve'); | |
const browserResolve = require('browser-resolve'); | |
function defaultResolver (path, options) { | |
const resv = options.browser ? browserResolve : resolve; | |
return resv.sync(path, { | |
basedir: options.basedir, | |
extensions: options.extensions, | |
moduleDirectory: options.moduleDirectory, | |
paths: options.paths, | |
packageFilter: mapModuleFieldToMain | |
}); | |
} | |
module.exports = defaultResolver; | |
/* | |
|------------------------------------------------------------------------------- | |
| Utils | |
*/ | |
function mapModuleFieldToMain (pkg, pkgDir) { | |
const moduleSrcPath = pkg['module']; | |
const isModuleFieldAvailable = moduleSrcPath && | |
fs.existsSync(path.resolve(pkgDir, moduleSrcPath)); | |
if (isModuleFieldAvailable) { | |
return Object.assign({}, pkg, { main: moduleSrcPath }); | |
} else { | |
return pkg; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment