import-local
Advanced tools
Comparing version 3.0.3 to 3.1.0
14
index.js
'use strict'; | ||
const path = require('path'); | ||
const {fileURLToPath} = require('url'); | ||
const resolveCwd = require('resolve-cwd'); | ||
@@ -7,4 +8,5 @@ const pkgDir = require('pkg-dir'); | ||
module.exports = filename => { | ||
const globalDir = pkgDir.sync(path.dirname(filename)); | ||
const relativePath = path.relative(globalDir, filename); | ||
const normalizedFilename = filename.startsWith('file://') ? fileURLToPath(filename) : filename; | ||
const globalDir = pkgDir.sync(path.dirname(normalizedFilename)); | ||
const relativePath = path.relative(globalDir, normalizedFilename); | ||
const pkg = require(path.join(globalDir, 'package.json')); | ||
@@ -14,5 +16,5 @@ const localFile = resolveCwd.silent(path.join(pkg.name, relativePath)); | ||
const filenameInLocalNodeModules = !path.relative(localNodeModules, filename).startsWith('..') && | ||
// On Windows, if `localNodeModules` and `filename` are on different partitions, `path.relative()` returns the value of `filename`, resulting in `filenameInLocalNodeModules` incorrectly becoming `true`. | ||
path.parse(localNodeModules).root === path.parse(filename).root; | ||
const filenameInLocalNodeModules = !path.relative(localNodeModules, normalizedFilename).startsWith('..') && | ||
// On Windows, if `localNodeModules` and `normalizedFilename` are on different partitions, `path.relative()` returns the value of `normalizedFilename`, resulting in `filenameInLocalNodeModules` incorrectly becoming `true`. | ||
path.parse(localNodeModules).root === path.parse(normalizedFilename).root; | ||
@@ -23,3 +25,3 @@ // Use `path.relative()` to detect local package installation, | ||
// See https://github.com/nodejs/node/issues/6624 | ||
return !filenameInLocalNodeModules && localFile && path.relative(localFile, filename) !== '' && require(localFile); | ||
return !filenameInLocalNodeModules && localFile && path.relative(localFile, normalizedFilename) !== '' && require(localFile); | ||
}; |
{ | ||
"name": "import-local", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "Let a globally installed package use a locally installed version of itself if available", | ||
"license": "MIT", | ||
"repository": "sindresorhus/import-local", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
@@ -12,0 +13,0 @@ "bin": { |
@@ -7,16 +7,14 @@ # import-local | ||
## Install | ||
```sh | ||
npm install import-local | ||
``` | ||
$ npm install import-local | ||
``` | ||
## Usage | ||
```js | ||
const importLocal = require('import-local'); | ||
import importLocal from 'import-local'; | ||
if (importLocal(__filename)) { | ||
if (importLocal(import.meta.url)) { | ||
console.log('Using local version of this package'); | ||
@@ -28,2 +26,3 @@ } else { | ||
You can also pass in `__filename` when used in a CommonJS context. | ||
@@ -30,0 +29,0 @@ --- |
4658
27
38