Skip to content

Commit

Permalink
fix(plugin): exclude native nodejs libs
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 19, 2021
1 parent 86ee0a0 commit e05fd6a
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export function hasPropertyKey(
.some((item) => item.name.getText() === key);
}

const NATIVE_NODEJS_LIBRARIES = 'stream';

export function replaceImportPath(typeReference: string, fileName: string) {
if (!typeReference.includes('import')) {
return typeReference;
Expand All @@ -122,33 +124,37 @@ export function replaceImportPath(typeReference: string, fileName: string) {
importPath = convertPath(importPath);
importPath = importPath.slice(2, importPath.length - 1);

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

const nodeModulesText = 'node_modules';
const nodeModulePos = relativePath.indexOf(nodeModulesText);
if (nodeModulePos >= 0) {
relativePath = relativePath.slice(
nodeModulePos + nodeModulesText.length + 1 // slash
);
if (NATIVE_NODEJS_LIBRARIES.includes(importPath)) {
return typeReference.replace('import', 'require');
} else {
let relativePath = posix.relative(posix.dirname(fileName), importPath);
relativePath = relativePath[0] !== '.' ? './' + relativePath : relativePath;

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

const indexText = '/index';
const indexPos = relativePath.indexOf(indexText);
if (indexPos >= 0) {
relativePath = relativePath.slice(0, indexPos);
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(0, indexPos);
}
}
}

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

export function isDynamicallyAdded(identifier: ts.Node) {
Expand Down

0 comments on commit e05fd6a

Please sign in to comment.