-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
23 lines (18 loc) · 865 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const fs = require("fs");
const path = require("path");
function isNodeModulesImport(filePath, importPath) {
const normalizedImportPath = path.normalize(importPath);
const fileDirname = path.dirname(filePath);
const importedFilePath = path.resolve(fileDirname, normalizedImportPath);
const nodeModulesPaths = module.paths.filter((nodeModulePath) => {
return path.dirname(importedFilePath).startsWith(path.dirname(nodeModulePath));
});
return nodeModulesPaths.some((nodeModulesPath) => {
const nodeModulesImportResolvedPath = path.resolve(nodeModulesPath, importPath);
return (
(fs.existsSync(importedFilePath) && importedFilePath.startsWith(nodeModulesPath)) ||
(fs.existsSync(nodeModulesImportResolvedPath) && nodeModulesImportResolvedPath.includes("node_modules"))
);
});
}
module.exports = { isNodeModulesImport };