From 60fb550e0f84ace0f689317c7400bd16bbff158a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Wed, 5 Feb 2020 13:32:46 +0100 Subject: [PATCH] fix(): remove node modules from path (plugin) --- lib/plugin/utils/plugin-utils.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/plugin/utils/plugin-utils.ts b/lib/plugin/utils/plugin-utils.ts index 0eab5bedd..a2f07f16a 100644 --- a/lib/plugin/utils/plugin-utils.ts +++ b/lib/plugin/utils/plugin-utils.ts @@ -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'); }