Debian Codegraph @

ncjsm/README.md

raw
1 [![Build status][build-image]][build-url]
2 [![Tests coverage][cov-image]][cov-url]
3 [![npm version][npm-image]][npm-url]
4
5 # ncjsm
6
7 ## (Node.js) CJS modules resolver
8
9 Environment agnostic (Node.js) CJS modules resolver.
10 It implements a _strict_ version of Node.js modules resolution logic, differences are as follows:
11
12 - [Loading from global folders](https://nodejs.org/api/all.html#all_loading_from_the_global_folders) is not supported
13 - Only Unix path separators (`/`) are supported in require's _path_ arguments (_Background: even though Node.js internally seems to follow Windows path separator in Windows environment, it won't work in \*nix environments, and even in Window env it's [not reliable](https://github.com/nodejs/node/issues/6049) so by all means should be avoided_)
14 - There's no awareness of node.js [core modules](https://nodejs.org/api/all.html#all_core_modules)
15 e.g. `resolve(dir, 'fs')` will naturally result with _null_
16
17 ### Installation
18
19 $ npm install ncjsm
20
21 ### API
22
23 #### getResolver(extensions, confirmFile, resolvePackageMain)
24
25 For provided configuration, returns a CJS modules resolver:
26
27 - **extensions** - List of supported file extensions in order of singificance, e.g. for Node.js it would be `['.js', '.json', '.node']`
28 - **confirmFile** - a `confirmFile(filepath)` function. Confirms whether there's a module at provided (not normalized, absolute) file path. Returns promise-like object which resolves with either normalized full path of a module or null (if there's no module for given path).
29 Although result is expected to be a promise-like object, resolution can be synchronous.
30 - **resolvePackageMain** - a `resolvePackageMain(dirpath)` function. Returns value of _package.json_'s `main` property for given path. Returns promise-like object which resolves with either resolved value, or null, when either `package.json` file was not found, or it didn't have _main_ property.
31 Same as with `confirmFile` resolution can be synchronous.
32
33 #### resolve(dir, path)
34
35 _Node.js resolver_
36
37 Asynchronously resolves module path against provided directory path.
38 Returns promise.
39 If module is found, then promise resolves with an object, containing two properties:
40
41 - `targetPath` - A path at which module was resolved
42 - `realPath` - Real path of resolved module (if targetPath involves symlinks then realPath will be different)
43
44 If no matching module was found, promise is rejected with `MODULE_NOT_FOUND` error (unless `silent: true` is passed with options (passed as third argument), then it resolves with `null`)
45
46 ```javascript
47 const resolve = require("ncjsm/resolve");
48
49 // Asynchronously resolve path for 'foo' module against current path
50 resolve(__dirname, "foo").then(
51 function (pathData) {
52 // 'foo' module found at fooModulePath
53 },
54 function (error) {
55 if (error.code === "MODULE_NOT_FOUND") {
56 // 'foo' module doesn't exist
57 }
58 }
59 );
60
61 // `silent` option, prevents module not found rejections:
62 resolve(__dirname, "foo", { silent: true }).then(function (pathData) {
63 if (pathData) {
64 // 'foo' module found at fooModulePath
65 } else {
66 // 'foo' module doesn't exist
67 }
68 });
69 ```
70
71 #### resolveSync(dir, path)
72
73 _Node.js resolver_
74
75 Synchronously resolves module path against provided directory path. Otherwise works same as `resolve`
76
77 ```javascript
78 const resolveSync = require("ncjsm/resolve/sync");
79
80 // Synchronously resolve path for 'foo' module against current path
81 let fooModulePathData;
82 try {
83 fooModulePathData = resolveSync(__dirname, "foo");
84 // 'foo' module found at fooModulePath
85 } catch (error) {
86 if (error.code === "MODULE_NOT_FOUND") {
87 // 'foo' module doesn't exist
88 }
89 }
90
91 fooModulePathData = resolveSync(__dirname, "foo", { silent: true });
92 if (fooModulePathData) {
93 // 'foo' module found
94 } else {
95 // 'foo' module doesn't exist
96 }
97 ```
98
99 #### requireUnached([moduleIds, ]callback)
100
101 Create temporary environment where `require` of specific modules will not resolved the eventually cached verions
102
103 ```javascript
104 var requireUncached = require("ncjsm/require-uncached");
105
106 const firstCopyOfModule1 = require("./module1");
107
108 var secondCopyOfModule2 = requireUnached([require.resolve("./module1")], function () {
109 return require("./module1");
110 });
111
112 console.log(firstCopyOfModule1 === secondCopyOfModule2); // false
113 ```
114
115 Alternatively we may resolve callback in completely cleared require cache, for that `moduleIds` argument should be skipped
116
117 ```javascript
118 var requireUncached = require("ncjsm/require-uncached");
119
120 const firstCopyOfModule1 = require("./module1");
121
122 var secondCopyOfModule2 = requireUnached(function () { return require("./module1"); });
123
124 console.log(firstCopyOfModule1 === secondCopyOfModule2); // false
125 ```
126
127 #### isPackageRoot(dirPath)
128
129 Whether provided path is a root of a package
130
131 ```javascript
132 var isPackageRoot = require("ncjsm/is-package-root");
133
134 isPackageRoot(dirPath).done(function (isRoot) {
135 if (isRoot) {
136 // Provided path is package root
137 }
138 });
139 ```
140
141 #### resolvePackageRoot(dirPath)
142
143 Resolve package root path for provided path. It is about resolution of first upper package root
144
145 ```javascript
146 var resolvePackageRoot = require("ncjsm/resolve-package-root");
147
148 resolvePackageRoot(dirPath).done(function (root) {
149 if (!root) {
150 // Provided path is not located in any package
151 }
152 });
153 ```
154
155 #### resolveProjectRoot(dirPath)
156
157 Resolve project root path for provided path. It is about resolution of topmost package root for given path
158
159 ```javascript
160 var resolveProjectRoot = require("ncjsm/resolve-project-root");
161
162 resolveProjectRoot(dirPath).done(function (root) {
163 if (!root) {
164 // Provided path is not located in any project
165 }
166 });
167 ```
168
169 #### getDependecies(modulePath, options = { ... })
170
171 Resolve all module dependencies. Returns promise that resolves with an array of paths, that includes path to input module and paths to all its dependencies (it includes deep dependencies, so also dependencies of the dependencies).
172
173 Paths to native Node.js modules are ignored.
174
175 ```javascript
176 var getDependencies = require("ncjsm/get-dependencies");
177
178 getDependencies(modulePath).done(function (deps) {
179 console.log(deps); // e.g. [pathToModulePath, pathToDep1, pathToDep2, ...pathToDepn]
180 });
181 ```
182
183 ##### Supported `options`
184
185 ###### `ignoreMissing: false`
186
187 If file for given module cannot be found then error is thrown. Set this to `true` to simply ignore not found modules
188
189 ###### `ignoreExternal: false`
190
191 By default all paths to all required modules are resolved. Resolution scope may be narrowed only to modules from same package (referenced via relative path), by settung this option to `true`
192
193 ## Tests [![Build Status](https://travis-ci.org/medikoo/ncjsm.svg)](https://travis-ci.org/medikoo/ncjsm)
194
195 $ npm test
196
197 [build-image]: https://github.com/medikoo/ncjsm/workflows/Integrate/badge.svg
198 [build-url]: https://github.com/medikoo/ncjsm/actions?query=workflow%3AIntegrate
199 [cov-image]: https://img.shields.io/codecov/c/github/medikoo/ncjsm.svg
200 [cov-url]: https://codecov.io/gh/medikoo/ncjsm
201 [npm-image]: https://img.shields.io/npm/v/ncjsm.svg
202 [npm-url]: https://www.npmjs.com/package/ncjsm