Skip to content

Commit 0080c33

Browse files
BendingBendersindresorhus
authored andcommittedApr 27, 2019
Require Node.js 8, return undefined instead of null, add TypeScript definition (#7)
1 parent b44e539 commit 0080c33

11 files changed

+110
-75
lines changed
 

‎.gitattributes

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
yarn.lock

‎.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

‎.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: node_js
22
node_js:
3-
- '6'
4-
- '4'
3+
- '12'
4+
- '10'
5+
- '8'

‎index.d.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
declare const importFrom: {
2+
/**
3+
Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path.
4+
5+
@param fromDirectory - Directory to import from.
6+
@param moduleId - What you would use in `require()`.
7+
@throws Like `require()`, throws when the module can't be found.
8+
9+
@example
10+
```
11+
import importFrom = require('import-from');
12+
13+
try {
14+
const bar = importFrom('foo', './bar');
15+
// do something with `bar`
16+
} catch (error) {
17+
// `error` is thrown when `./bar` can't be found
18+
}
19+
```
20+
*/
21+
(fromDirectory: string, moduleId: string): unknown;
22+
23+
/**
24+
Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path.
25+
26+
@param fromDirectory - Directory to import from.
27+
@param moduleId - What you would use in `require()`.
28+
@returns `undefined` instead of throwing when the module can't be found.
29+
30+
@example
31+
```
32+
import importFrom = require('import-from');
33+
34+
const bar = importFrom.silent('foo', './bar');
35+
// do something with `bar`, may be `undefined` when `./bar` can't be found
36+
```
37+
*/
38+
silent(fromDirectory: string, moduleId: string): unknown;
39+
};
40+
41+
export = importFrom;

‎index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use strict';
22
const resolveFrom = require('resolve-from');
33

4-
module.exports = (fromDir, moduleId) => require(resolveFrom(fromDir, moduleId));
4+
module.exports = (fromDirectory, moduleId) => require(resolveFrom(fromDirectory, moduleId));
55

6-
module.exports.silent = (fromDir, moduleId) => {
6+
module.exports.silent = (fromDirectory, moduleId) => {
77
try {
8-
return require(resolveFrom(fromDir, moduleId));
9-
} catch (err) {
10-
return null;
11-
}
8+
return require(resolveFrom(fromDirectory, moduleId));
9+
} catch (_) {}
1210
};

‎index.test-d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import importFrom = require('.');
2+
3+
importFrom('foo', './bar');
4+
importFrom.silent('foo', './bar');

‎license

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
The MIT License (MIT)
1+
MIT License
22

33
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
116

12-
The above copyright notice and this permission notice shall be included in
13-
all copies or substantial portions of the Software.
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
148

15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
THE SOFTWARE.
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎package.json

+38-36
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
{
2-
"name": "import-from",
3-
"version": "2.1.0",
4-
"description": "Import a module like with `require()` but from a given path",
5-
"license": "MIT",
6-
"repository": "sindresorhus/import-from",
7-
"author": {
8-
"name": "Sindre Sorhus",
9-
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11-
},
12-
"engines": {
13-
"node": ">=4"
14-
},
15-
"scripts": {
16-
"test": "xo && ava"
17-
},
18-
"files": [
19-
"index.js"
20-
],
21-
"keywords": [
22-
"require",
23-
"resolve",
24-
"path",
25-
"module",
26-
"from",
27-
"like",
28-
"import",
29-
"path"
30-
],
31-
"dependencies": {
32-
"resolve-from": "^3.0.0"
33-
},
34-
"devDependencies": {
35-
"ava": "*",
36-
"xo": "*"
37-
}
2+
"name": "import-from",
3+
"version": "2.1.0",
4+
"description": "Import a module like with `require()` but from a given path",
5+
"license": "MIT",
6+
"repository": "sindresorhus/import-from",
7+
"author": {
8+
"name": "Sindre Sorhus",
9+
"email": "sindresorhus@gmail.com",
10+
"url": "sindresorhus.com"
11+
},
12+
"engines": {
13+
"node": ">=8"
14+
},
15+
"scripts": {
16+
"test": "xo && ava && tsd"
17+
},
18+
"files": [
19+
"index.js",
20+
"index.d.ts"
21+
],
22+
"keywords": [
23+
"require",
24+
"resolve",
25+
"path",
26+
"module",
27+
"from",
28+
"like",
29+
"import",
30+
"path"
31+
],
32+
"dependencies": {
33+
"resolve-from": "^5.0.0"
34+
},
35+
"devDependencies": {
36+
"ava": "^1.4.1",
37+
"tsd": "^0.7.2",
38+
"xo": "^0.24.0"
39+
}
3840
}

‎readme.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# import-from [![Build Status](https://travis-ci.org/sindresorhus/import-from.svg?branch=master)](https://travis-ci.org/sindresorhus/import-from)
22

3-
> Import a module like with [`require()`](https://nodejs.org/api/globals.html#globals_require) but from a given path
3+
> Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path
44
55

66
## Install
77

88
```
9-
$ npm install --save import-from
9+
$ npm install import-from
1010
```
1111

1212

@@ -23,15 +23,15 @@ importFrom('foo', './bar');
2323

2424
## API
2525

26-
### importFrom(fromDir, moduleId)
26+
### importFrom(fromDirectory, moduleId)
2727

2828
Like `require()`, throws when the module can't be found.
2929

30-
### importFrom.silent(fromDir, moduleId)
30+
### importFrom.silent(fromDirectory, moduleId)
3131

32-
Returns `null` instead of throwing when the module can't be found.
32+
Returns `undefined` instead of throwing when the module can't be found.
3333

34-
#### fromDir
34+
#### fromDirectory
3535

3636
Type: `string`
3737

‎test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import test from 'ava';
2-
import m from '.';
2+
import importFrom from '.';
33

44
test('importFrom()', t => {
5-
t.is(m('fixture', './fixture'), 'unicorn');
6-
t.throws(() => m('fixture', './nonexistent'));
5+
t.is(importFrom('fixture', './fixture'), 'unicorn');
6+
t.throws(() => importFrom('fixture', './nonexistent'));
77

88
const moduleNotFoundError = t.throws(() => {
9-
m('fixture', './nonexistent');
9+
importFrom('fixture', './nonexistent');
1010
}, Error);
1111
t.is(moduleNotFoundError.code, 'MODULE_NOT_FOUND');
12-
t.is(moduleNotFoundError.message, 'Cannot find module \'./nonexistent\'');
12+
t.regex(moduleNotFoundError.message, /^Cannot find module '.\/nonexistent'/);
1313
});
1414

1515
test('importFrom.silent()', t => {
16-
t.is(m.silent('fixture', './fixture'), 'unicorn');
17-
t.is(m.silent('fixture', './nonexistent'), null);
16+
t.is(importFrom.silent('fixture', './fixture'), 'unicorn');
17+
t.is(importFrom.silent('fixture', './nonexistent'), undefined);
1818
});

0 commit comments

Comments
 (0)
Please sign in to comment.