Skip to content

Commit 7034417

Browse files
committed
Now defaults to throwing an Error when a required module is not found
`options.excludeNodeModules` can now be set to `true` or to an Array containing modules to be excluded from the project `options.allowUnresolvedModules` can now be set to `true` to ignore modules that could not be found. `stats.unresolvedModules` contains information about modules that could not be found. Only relevant when `options.allowUnresolvedModules` is `true`. Better normalization of `options` in stream constructor Push to 2.1.0
1 parent c76788c commit 7034417

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# 2.1.0
2+
3+
- Now defaults to throwing an Error when a required module is not found, but
4+
this behavior can be changed by setting `options.allowUnresolvedModules`
5+
- `options.excludeNodeModules` can now be set to `true` or to an Array
6+
containing modules to be excluded from the project
7+
- `stats.unresolvedModules` contains information about modules that could not
8+
be found. Only relevant when `options.allowUnresolvedModules` is `true`.
9+
110
# 2.0.0
211

312
Almost a complete re-write of the original project.

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ of the concatenated project.
5050

5151
Note: These `require` statements should probably be wrapped with a
5252
conditional or a try/catch block to prevent uncaught exceptions.
53-
- `excludeNodeModules` - Set to `true` if modules loaded from
54-
`node_modules` folders should be excluded from the project.
53+
- `excludeNodeModules` - (boolean or Array) Set to `true` if all modules
54+
loaded from `node_modules` folders should be excluded from the project.
55+
Alternatively, set to an Array of module names to be excluded from the
56+
project.
57+
58+
For example, `require("foobar")` will not be replaced if
59+
`excludeNodeModules` is set to an Array containing `"foobar"` or if
60+
`excludeNodeModules` is set to `true`.
5561
- `extensions` - An Array of extensions that will be appended to the
5662
required module path to search for the module in the file system.
5763
Defaults to `[".js", ".json"]`.
@@ -95,6 +101,10 @@ of the concatenated project.
95101
`node_modules`, the `browser` field in the `package.json` file (if
96102
found) is used to determine which file to actually include in the
97103
project.
104+
- `allowUnresolvedModules` - Set to `true` to prevent unresolved modules
105+
from throwing an Error; instead, the `require(...)` expression will not
106+
be replaced, and the unresolved module will be added to
107+
`stats.unresolvedModules` (see below). Defaults to `false`.
98108
- Any [option supported by resolve.sync]
99109
(https://github.com/substack/node-resolve#resolvesyncid-opts) except
100110
`basedir` and `packageFilter`, which can be overwritten.
@@ -109,6 +119,11 @@ is no more data to consume. Properties include:
109119
- `files` - An Array of files included in the project
110120
- `addonsExcluded` - An Array of files excluded from the project because
111121
they are native C/C++ add-ons.
122+
- `unresolvedModules` - An Array of modules that could not be included in the
123+
project because they could not be found. Each element in the Array is an
124+
Object containing these properties:
125+
- `parent` - the full path of the file containing the require expression
126+
- `module` - the name or path to the module that could not be found
112127

113128
**`modConcat(entryModule, outputPath, [options, cb])`**
114129

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ if(require.main === module) {
5353
}
5454
modConcat(process.argv[2], process.argv[3], function(err, stats) {
5555
if(err) {
56-
console.error("Error", err.stack);
56+
console.error("An error occurred:\n", err.stack);
5757
process.exit(1);
5858
} else {
5959
console.log(stats.files.length + " files written to " +
6060
process.argv[3] + ".");
61+
if(stats.unresolvedModules.length) {
62+
console.log("The following modules could not be resolved:\n",
63+
stats.unresolvedModules);
64+
}
6165
console.log("Completed successfully.");
6266
}
6367
});

lib/moduleConcatStream.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,19 @@ class ModuleConcatStream extends Readable {
7878
super(options);
7979
// Save options
8080
let opts = this._options = options || {};
81-
// Ensure that all paths have been resolved
82-
if(opts.excludeFiles) {
83-
for(var i = 0; i < opts.excludeFiles.length; i++) {
84-
opts.excludeFiles[i] = path.resolve(opts.excludeFiles[i]);
85-
}
81+
// Ensure that `opts.excludeFiles` is an Array
82+
if(!Array.isArray(opts.excludeFiles) ) {
83+
opts.excludeFiles = [];
84+
}
85+
// Ensure all excluded file paths are resolved
86+
for(var i = 0; i < opts.excludeFiles.length; i++) {
87+
opts.excludeFiles[i] = path.resolve(opts.excludeFiles[i]);
88+
}
89+
// Ensure `opts.excludeNodeModules` is either falsy, `true`, or an Array
90+
if(opts.excludeNodeModules && opts.excludeNodeModules !== true &&
91+
!Array.isArray(opts.excludeNodeModules) )
92+
{
93+
opts.excludeNodeModules = [opts.excludeNodeModules];
8694
}
8795
// Configure default file extensions
8896
if(!Array.isArray(opts.extensions) ) {
@@ -113,6 +121,8 @@ class ModuleConcatStream extends Readable {
113121
this._fileIndex = 0;
114122
// List of native C/C++ add-ons found that were excluded from the output
115123
this._addonsExcluded = [];
124+
// Object containing modules that could not be resolved
125+
this._unresolvedModules = [];
116126
// Flag indicating that the header has been written
117127
this._headerWritten = false;
118128
}
@@ -181,7 +191,14 @@ class ModuleConcatStream extends Readable {
181191
with "./" or "../" or "/"
182192
*/
183193
if(this._options.excludeNodeModules &&
184-
modulePath.match(/^\.?\.?\//) == null)
194+
modulePath.match(/^\.?\.?\//) == null && (
195+
/* If `excludeNodeModules` is `true`, exclude all
196+
node_modules */
197+
this._options.excludeNodeModules === true ||
198+
/* Otherwise, only exclude the module if it appears in
199+
the Array of excluded modules */
200+
this._options.excludeNodeModules.indexOf(modulePath) >= 0
201+
) )
185202
{
186203
return match;
187204
}
@@ -205,8 +222,7 @@ class ModuleConcatStream extends Readable {
205222
var index = this._files.indexOf(modulePath);
206223
if(index < 0) {
207224
// Not added to project yet; try to add it
208-
if(!this._options.excludeFiles ||
209-
this._options.excludeFiles.indexOf(modulePath) < 0)
225+
if(this._options.excludeFiles.indexOf(modulePath) < 0)
210226
{
211227
// Not excluded, so we're good to go!
212228
index = this._files.push(modulePath) - 1;
@@ -220,8 +236,17 @@ class ModuleConcatStream extends Readable {
220236
var parentIndex = this._files.indexOf(filePath);
221237
return "__require(" + index + "," + parentIndex + ")";
222238
} catch(e) {
223-
// Could not resolve module path; do not replace
224-
return match;
239+
// Could not resolve module path
240+
if(this._options.allowUnresolvedModules) {
241+
this._unresolvedModules.push({
242+
"parent": filePath,
243+
"module": modulePath
244+
});
245+
return match;
246+
} else {
247+
// Unresolved modules are not allowed; throw the Error
248+
throw e;
249+
}
225250
}
226251
});
227252
// Handle `__dirname` and `__filename` replacement
@@ -261,7 +286,8 @@ class ModuleConcatStream extends Readable {
261286
} else {
262287
return {
263288
"files": this._files,
264-
"addonsExcluded": this._addonsExcluded
289+
"addonsExcluded": this._addonsExcluded,
290+
"unresolvedModules": this._unresolvedModules
265291
};
266292
}
267293
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-module-concat",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Lightweight CommonJS module concatenation tool",
55
"main": "index.js",
66
"dependencies": {

0 commit comments

Comments
 (0)