Skip to content

Commit d6e2aa4

Browse files
committed
ignoreErrors option
1 parent 292229f commit d6e2aa4

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ See [extensions](#extensions) section for more info.
113113

114114
Include [source maps][], for easier debugging. Source maps work very well in WebKit and Chrome's web inspector. Firefox's Firebug however has some [issues][firebug issue].
115115

116+
##### ignoreErrors `boolean`
117+
118+
Ignore not parsable require paths (e.g. `require('./lang/' + lang)`) if any.
119+
Dynamic paths in require calls are considered a bad practice and won't be possible with upcoming _ES6 modules_ standard. Still if we deal with modules that do that, we can workaround it by turning this option on, and including missing modules with [`include`](https://github.com/medikoo/modules-webmake/edit/master/README.md#include-string) option.
120+
116121
##### cache `boolean` _programmatical usage only_
117122

118123
Cache files content and its calculated dependencies. On repeated request only modified files are re-read and parsed.

bin/webmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ var count = require('es5-ext/lib/Object/count')
2323
string: true,
2424
description: "Optional extensions"
2525
},
26+
'ignore-errors': {
27+
boolean: true,
28+
description: "Ignore unparsable require calls"
29+
},
2630
include: {
2731
string: true,
2832
description: "Additional module(s) that should be included (and are" +
@@ -57,6 +61,7 @@ if (argv.include) {
5761

5862
options.ext = argv.ext;
5963
options.sourceMap = argv.sourcemap;
64+
options.ignoreErrors = argv['ignore-errors'];
6065
options.output = argv._[1];
6166

6267
var time = now();

lib/parser.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var commonLeft = require('es5-ext/lib/Array/prototype/common-left')
4+
, compact = require('es5-ext/lib/Array/prototype/compact')
45
, copy = require('es5-ext/lib/Array/prototype/copy')
56
, last = require('es5-ext/lib/Array/prototype/last')
67
, CustomError = require('es5-ext/lib/Error/custom')
@@ -36,12 +37,18 @@ if (sep == null) {
3637
}
3738
dirEndMatch = new RegExp('(?:^|\\' + sep + ')\\.*$');
3839

39-
parseDependencies = function (text, filename) {
40+
parseDependencies = function (text, filename, ignoreErrors) {
4041
return findRequires(text, { raw: true }).map(function (node) {
4142
var path = node.value;
4243
if (!path) {
43-
throw new TypeError("Not supported require call: " + node.raw +
44-
" at " + filename + ":" + node.line);
44+
if (!ignoreErrors) {
45+
throw new CustomError("Not parsable require call: `" + node.raw +
46+
"` at " + filename + ":" + node.line + "\n You may" +
47+
" ignore such errors with ignoreErrors option ('ignore-errors'" +
48+
" when running from command line)", 'DYNAMIC_REQUIRE');
49+
}
50+
console.warn("Not parsable require call (ignored): `" + node.raw +
51+
"` at " + filename + ":" + node.line);
4552
}
4653
return path;
4754
});
@@ -96,7 +103,8 @@ readFileData = function (filename, parser, localFilename) {
96103
}
97104

98105
if (sLast.call(code) !== '\n') code += '\n';
99-
deps = noDeps ? [] : parseDependencies(code, filename);
106+
deps = noDeps ? [] :
107+
compact.call(parseDependencies(code, filename, parser.ignoreErrors));
100108

101109
if (parser.sourceMap) {
102110
code = 'eval(' + stringify(code + '//@ sourceURL=' +
@@ -426,6 +434,7 @@ module.exports = exports = function (/* options */) {
426434
packages: d({}),
427435
modulesFiles: d([]),
428436
sourceMap: d(Boolean(options.sourceMap)),
437+
ignoreErrors: d(Boolean(options.ignoreErrors)),
429438
cache: d(Boolean(options.cache)),
430439
ext: d(ext),
431440
extNames: d(keys(ext))

test/__playground/lib/dynamic.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var lang = 'pl';
4+
5+
try {
6+
require('./raz/dwa/' + lang);
7+
} catch (e) {}
8+
9+
exports.foo = 'bar';

test/webmake.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ module.exports = {
8585
a(program.getZ().name, 'z', "External name");
8686
}).end(d);
8787
},
88+
"Dynamic": {
89+
"Error": function (t, a, d) {
90+
var input = pg + '/lib/dynamic.js';
91+
t(input)(a.never, function (e) { a(e.code, 'DYNAMIC_REQUIRE'); })
92+
.done(d, d);
93+
},
94+
"Ignored": function (t, a, d) {
95+
var input = pg + '/lib/dynamic.js';
96+
t(input, { ignoreErrors: true })(function (result) {
97+
var program = runInNewContext(result, {}, input);
98+
a(program.foo, 'bar');
99+
}, a.never).done(d, d);
100+
}
101+
},
88102
"Error on native": function (t, a, d) {
89103
t(pg + '/require-native.js', function (err) {
90104
a.ok(startsWith.call(err.message, "Cannot require"));

0 commit comments

Comments
 (0)