Skip to content

Commit

Permalink
feat: support scoped packages from DefinitelyTyped
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jun 30, 2019
1 parent 74de3d9 commit b4e72a5
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ function resolveFile(source, file, config) {
// iff path is neither absolute nor relative
if (
/\.jsx?$/.test(foundNodePath) &&
!/^@types[\/\\]/.test(source) &&
!/^@types[/\\]/.test(source) &&
!path.isAbsolute(source) &&
source[0] !== '.'
) {
const definitely = resolveFile('@types/' + source, file, config);
if (definitely.found) {
return definitely;
const definitelyTyped = resolveFile(
'@types' + path.sep + mangleScopedPackage(source),
file,
config,
);
if (definitelyTyped.found) {
return definitelyTyped;
}
}

Expand All @@ -91,12 +95,29 @@ function resolveFile(source, file, config) {
found: false,
};
}

function packageFilter(pkg) {
pkg.main =
pkg.types || pkg.typings || pkg.module || pkg['jsnext:main'] || pkg.main;
return pkg;
}

/**
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
*
* @param {string} moduleName
* @returns {string}
*/
function mangleScopedPackage(moduleName) {
if (moduleName[0] === '@') {
const replaceSlash = moduleName.replace(path.sep, '__');
if (replaceSlash !== moduleName) {
return replaceSlash.slice(1); // Take off the "@"
}
}
return moduleName;
}

module.exports = {
interfaceVersion: 2,
resolve: resolveFile,
Expand Down

0 comments on commit b4e72a5

Please sign in to comment.