Skip to content

Commit

Permalink
fix(): remove node modules from path (plugin)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 5, 2020
1 parent 6c04be9 commit 60fb550
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,30 @@ export function replaceImportPath(typeReference: string, fileName: string) {

let relativePath = posix.relative(dirname(fileName), importPath);
relativePath = relativePath[0] !== '.' ? './' + relativePath : relativePath;
typeReference = typeReference.replace(importPath, relativePath);

const nodeModulesText = 'node_modules';
const nodeModulePos = relativePath.indexOf(nodeModulesText);
if (nodeModulePos >= 0) {
relativePath = relativePath.slice(
nodeModulePos + nodeModulesText.length + 1 // slash
);

const typesText = '@types';
const typesPos = relativePath.indexOf(typesText);
if (typesPos >= 0) {
relativePath = relativePath.slice(
typesPos + typesText.length + 1 //slash
);
}

const indexText = '/index';
const indexPos = relativePath.indexOf(indexText);
if (indexPos >= 0) {
relativePath = relativePath.slice(indexPos + indexText.length);
}
}

typeReference = typeReference.replace(importPath, relativePath);
return typeReference.replace('import', 'require');
}

Expand Down

0 comments on commit 60fb550

Please sign in to comment.