Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix rethrow in browser #756

Closed
wants to merge 2 commits into from

2 participants

@ixti

These patches surrounds node-specific part of context builder in runtime.rethrow
with // if node ... // end comments. Also they improve browserified runtime
compilation with support/compile_runtime.js hich respects conditional comments.

ixti added some commits
@ixti ixti Make runtime.rethrow more browser-friendly
This patch removes `fs`-dependent logic of error context building out of
browserified version of runtime.
0626522
@ixti ixti Refactor browserified runtime compilation cf6eb69
@ixti

Sorry. My problem was not related to the fix I provided :))

@ixti ixti closed this
@ixti

In fact this fix does not makes any sense (as I just figured out).
But it removes potentionally problematic place in browserified version.

I'm reopening pull-request, but feel free to close it, if you don't need/like it :))

@ixti ixti reopened this
@ForbesLindesay

Closing in favor of #985

@ixti ixti deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 30, 2012
  1. @ixti

    Make runtime.rethrow more browser-friendly

    ixti authored
    This patch removes `fs`-dependent logic of error context building out of
    browserified version of runtime.
  2. @ixti
This page is out of date. Refresh to see the latest.
View
2  Makefile
@@ -28,7 +28,7 @@ jade.min.js: jade.js
&& du jade.js
runtime.js: lib/runtime.js
- @cat support/head.js $< support/foot.js > $@
+ @node support/compile_runtime.js
runtime.min.js: runtime.js
@$(UGLIFY) $(UGLIFY_FLAGS) $< > $@ \
View
28 lib/runtime.js
@@ -151,24 +151,24 @@ exports.escape = function escape(html){
exports.rethrow = function rethrow(err, filename, lineno){
if (!filename) throw err;
- var context = 3
- , str = require('fs').readFileSync(filename, 'utf8')
+ var context = '';
+
+ // if node
+ var str = require('fs').readFileSync(filename, 'utf8')
, lines = str.split('\n')
- , start = Math.max(lineno - context, 0)
- , end = Math.min(lines.length, lineno + context);
+ , start = Math.max(lineno - 3, 0) // 3 lines before error line
+ , end = Math.min(lines.length, lineno + 3); // 3 lines after error line
- // Error context
- var context = lines.slice(start, end).map(function(line, i){
+ // Build error context
+ context = lines.slice(start, end).map(function(line, i){
var curr = i + start + 1;
- return (curr == lineno ? ' > ' : ' ')
- + curr
- + '| '
- + line;
- }).join('\n');
+ return (curr == lineno ? ' > ' : ' ') + curr + '| ' + line + '\n';
+ }).join('');
+ // end
// Alter exception message
- err.path = filename;
- err.message = (filename || 'Jade') + ':' + lineno
- + '\n' + context + '\n\n' + err.message;
+ err.path = filename;
+ err.message = filename + ':' + lineno + '\n' + context + '\n' + err.message;
+
throw err;
};
View
50 support/compile_runtime.js
@@ -0,0 +1,50 @@
+/**
+ * Module dependencies.
+ */
+
+var fs = require('fs');
+
+/**
+ * Arguments.
+ */
+
+console.log('');
+
+// parse arguments
+
+fs.readFile('lib/runtime.js', 'utf8', function(err, js){
+ if (err) throw err;
+
+ var lines = js.split('\n')
+ , buffer = true
+ , browser = false
+ , buf = ''
+ , line
+ , cond;
+
+ buf += 'jade = (function (exports) {\n';
+
+ for (var i = 0, l = lines.length; i < l; ++i) {
+ line = lines[i];
+
+ if (/^ *\/\/ *if *(node|browser)/gm.exec(line)) {
+ cond = RegExp.$1;
+ buffer = browser = 'browser' == cond;
+ } else if (/^ *\/\/ *end/.test(line)) {
+ buffer = true;
+ browser = false;
+ } else if (browser) {
+ buf += line.replace(/^( *)\/\//, '$1') + '\n';
+ } else if (buffer) {
+ buf += line + '\n';
+ }
+ }
+
+ buf += '\nreturn exports;\n})({});';
+
+ fs.writeFile('runtime.js', buf, function(err){
+ if (err) throw err;
+ console.log(' \033[90m create : \033[0m\033[36m%s\033[0m', 'runtime.js');
+ console.log();
+ });
+});
View
4 support/foot.js
@@ -1,4 +0,0 @@
-
- return exports;
-
-})({});
View
2  support/head.js
@@ -1,2 +0,0 @@
-
-jade = (function(exports){
Something went wrong with that request. Please try again.