Skip to content

Commit acf1047

Browse files
committed
Bring TraceKit inside the same closure as Raven.
Shaves off ~1KB with minification, fixes #161
1 parent 5b9d456 commit acf1047

File tree

5 files changed

+56
-100
lines changed

5 files changed

+56
-100
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module.exports = function(grunt) {
55
var path = require('path');
66

77
var coreFiles = [
8-
'vendor/**/*.js',
98
'template/_header.js',
9+
'vendor/**/*.js',
1010
'src/**/*.js',
1111
'template/_footer.js'
1212
];

src/raven.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ var _Raven = window.Raven,
2121
extra: {}
2222
};
2323

24-
var TK = TraceKit.noConflict();
25-
26-
// Disable Tracekit's remote fetching by default
27-
TK.remoteFetching = false;
28-
2924
/*
3025
* The core Raven singleton
3126
*
@@ -35,7 +30,7 @@ var Raven = {
3530
VERSION: '<%= pkg.version %>',
3631

3732
// Expose TraceKit to the Raven namespace
38-
TraceKit: TK,
33+
TraceKit: TraceKit,
3934

4035
/*
4136
* Allow Raven to be configured as soon as it is loaded
@@ -104,14 +99,14 @@ var Raven = {
10499
}
105100

106101
if (globalOptions.fetchContext) {
107-
TK.remoteFetching = true;
102+
TraceKit.remoteFetching = true;
108103
}
109104

110105
if (globalOptions.linesOfContext) {
111-
TK.linesOfContext = globalOptions.linesOfContext;
106+
TraceKit.linesOfContext = globalOptions.linesOfContext;
112107
}
113108

114-
TK.collectWindowErrors = !!globalOptions.collectWindowErrors;
109+
TraceKit.collectWindowErrors = !!globalOptions.collectWindowErrors;
115110

116111
// return for chaining
117112
return Raven;
@@ -127,7 +122,7 @@ var Raven = {
127122
*/
128123
install: function() {
129124
if (isSetup()) {
130-
TK.report.subscribe(handleStackInfo);
125+
TraceKit.report.subscribe(handleStackInfo);
131126
}
132127

133128
return Raven;
@@ -216,7 +211,7 @@ var Raven = {
216211
* @return {Raven}
217212
*/
218213
uninstall: function() {
219-
TK.report.unsubscribe(handleStackInfo);
214+
TraceKit.report.unsubscribe(handleStackInfo);
220215

221216
return Raven;
222217
},
@@ -241,7 +236,7 @@ var Raven = {
241236
// raises an exception different from the one we asked to
242237
// report on.
243238
try {
244-
TK.report(ex, options);
239+
TraceKit.report(ex, options);
245240
} catch(ex1) {
246241
if(ex !== ex1) {
247242
throw ex1;
@@ -367,6 +362,17 @@ function isEmptyObject(what) {
367362
return true;
368363
}
369364

365+
/**
366+
* hasKey, a better form of hasOwnProperty
367+
* Example: hasKey(MainHostObject, property) === true/false
368+
*
369+
* @param {Object} host object to check property
370+
* @param {string} key to check
371+
*/
372+
function hasKey(object, key) {
373+
return Object.prototype.hasOwnProperty.call(object, key);
374+
}
375+
370376
function each(obj, callback) {
371377
var i, j;
372378

template/_header.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
;(function(window, undefined){
2+
'use strict';

test/raven.test.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ describe('globals', function() {
4545
flushRavenState();
4646
});
4747

48-
it('should not have TraceKit on window', function() {
49-
assert.isUndefined(window.TraceKit);
50-
});
51-
52-
it('should have a local TK', function() {
53-
assert.isObject(TK);
54-
});
55-
5648
describe('getHttpData', function() {
5749
var data = getHttpData();
5850

@@ -873,7 +865,7 @@ describe('globals', function() {
873865
'a', 'b', 'a.b', /d/, /[0-9]/
874866
]).source, 'a|b|a\\.b|d|[0-9]');
875867
});
876-
868+
877869
it('should not process empty or undefined variables', function() {
878870
assert.equal(joinRegExp([
879871
'a', 'b', null, undefined
@@ -907,7 +899,7 @@ describe('Raven (public API)', function() {
907899
};
908900

909901
this.sinon.stub(window, 'isSetup').returns(false);
910-
this.sinon.stub(TK.report, 'subscribe');
902+
this.sinon.stub(TraceKit.report, 'subscribe');
911903

912904
Raven.afterLoad();
913905

@@ -919,7 +911,7 @@ describe('Raven (public API)', function() {
919911
assert.equal(globalProject, 2);
920912

921913
assert.isTrue(window.isSetup.calledOnce);
922-
assert.isFalse(TK.report.subscribe.calledOnce);
914+
assert.isFalse(TraceKit.report.subscribe.calledOnce);
923915

924916
delete window.RavenConfig;
925917
});
@@ -984,42 +976,42 @@ describe('Raven (public API)', function() {
984976
describe('collectWindowErrors', function() {
985977
it('should be true by default', function() {
986978
Raven.config(SENTRY_DSN);
987-
assert.isTrue(TK.collectWindowErrors);
979+
assert.isTrue(TraceKit.collectWindowErrors);
988980
});
989981

990982
it('should be true if set to true', function() {
991983
Raven.config(SENTRY_DSN, {
992984
collectWindowErrors: true
993985
});
994986

995-
assert.isTrue(TK.collectWindowErrors);
987+
assert.isTrue(TraceKit.collectWindowErrors);
996988
});
997989

998990
it('should be false if set to false', function() {
999991
Raven.config(SENTRY_DSN, {
1000992
collectWindowErrors: false
1001993
});
1002994

1003-
assert.isFalse(TK.collectWindowErrors);
995+
assert.isFalse(TraceKit.collectWindowErrors);
1004996
});
1005997
});
1006998
});
1007999

10081000
describe('.install', function() {
10091001
it('should check `isSetup`', function() {
10101002
this.sinon.stub(window, 'isSetup').returns(false);
1011-
this.sinon.stub(TK.report, 'subscribe');
1003+
this.sinon.stub(TraceKit.report, 'subscribe');
10121004
Raven.install();
10131005
assert.isTrue(window.isSetup.calledOnce);
1014-
assert.isFalse(TK.report.subscribe.calledOnce);
1006+
assert.isFalse(TraceKit.report.subscribe.calledOnce);
10151007
});
10161008

10171009
it('should register itself with TraceKit', function() {
10181010
this.sinon.stub(window, 'isSetup').returns(true);
1019-
this.sinon.stub(TK.report, 'subscribe');
1011+
this.sinon.stub(TraceKit.report, 'subscribe');
10201012
assert.equal(Raven, Raven.install());
1021-
assert.isTrue(TK.report.subscribe.calledOnce);
1022-
assert.equal(TK.report.subscribe.lastCall.args[0], handleStackInfo);
1013+
assert.isTrue(TraceKit.report.subscribe.calledOnce);
1014+
assert.equal(TraceKit.report.subscribe.lastCall.args[0], handleStackInfo);
10231015
});
10241016
});
10251017

@@ -1160,10 +1152,10 @@ describe('Raven (public API)', function() {
11601152

11611153
describe('.uninstall', function() {
11621154
it('should unsubscribe from TraceKit', function() {
1163-
this.sinon.stub(TK.report, 'unsubscribe');
1155+
this.sinon.stub(TraceKit.report, 'unsubscribe');
11641156
Raven.uninstall();
1165-
assert.isTrue(TK.report.unsubscribe.calledOnce);
1166-
assert.equal(TK.report.unsubscribe.lastCall.args[0], handleStackInfo);
1157+
assert.isTrue(TraceKit.report.unsubscribe.calledOnce);
1158+
assert.equal(TraceKit.report.unsubscribe.lastCall.args[0], handleStackInfo);
11671159
});
11681160
});
11691161

@@ -1203,32 +1195,32 @@ describe('Raven (public API)', function() {
12031195
});
12041196

12051197
describe('.captureException', function() {
1206-
it('should call TK.report', function() {
1198+
it('should call TraceKit.report', function() {
12071199
var error = new Error('crap');
1208-
this.sinon.stub(TK, 'report');
1200+
this.sinon.stub(TraceKit, 'report');
12091201
Raven.captureException(error, {foo: 'bar'});
1210-
assert.isTrue(TK.report.calledOnce);
1211-
assert.deepEqual(TK.report.lastCall.args, [error, {foo: 'bar'}]);
1202+
assert.isTrue(TraceKit.report.calledOnce);
1203+
assert.deepEqual(TraceKit.report.lastCall.args, [error, {foo: 'bar'}]);
12121204
});
12131205

12141206
it('should store the last exception', function() {
12151207
var error = new Error('crap');
1216-
this.sinon.stub(TK, 'report');
1208+
this.sinon.stub(TraceKit, 'report');
12171209
Raven.captureException(error);
12181210
assert.equal(Raven.lastException(), error);
12191211
});
12201212

12211213
it('shouldn\'t reraise the if the error is the same error', function() {
12221214
var error = new Error('crap');
1223-
this.sinon.stub(TK, 'report').throws(error);
1215+
this.sinon.stub(TraceKit, 'report').throws(error);
12241216
// this would raise if the errors didn't match
12251217
Raven.captureException(error, {foo: 'bar'});
1226-
assert.isTrue(TK.report.calledOnce);
1218+
assert.isTrue(TraceKit.report.calledOnce);
12271219
});
12281220

12291221
it('should reraise a different error', function() {
12301222
var error = new Error('crap1');
1231-
this.sinon.stub(TK, 'report').throws(error);
1223+
this.sinon.stub(TraceKit, 'report').throws(error);
12321224
try {
12331225
Raven.captureException(new Error('crap2'));
12341226
} catch(e) {
@@ -1240,10 +1232,10 @@ describe('Raven (public API)', function() {
12401232

12411233
it('should capture as a normal message if a string is passed', function() {
12421234
this.sinon.stub(Raven, 'captureMessage');
1243-
this.sinon.stub(TK, 'report');
1235+
this.sinon.stub(TraceKit, 'report');
12441236
Raven.captureException('derp');
12451237
assert.equal(Raven.captureMessage.lastCall.args[0], 'derp');
1246-
assert.isFalse(TK.report.called);
1238+
assert.isFalse(TraceKit.report.called);
12471239
});
12481240
});
12491241
});

vendor/TraceKit/tracekit.js

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,18 @@
33
MIT license
44
*/
55

6-
(function(window, undefined) {
7-
8-
9-
var TraceKit = {};
10-
var _oldTraceKit = window.TraceKit;
6+
var TraceKit = {
7+
remoteFetching: false,
8+
collectWindowErrors: true,
9+
// 3 lines before, the offending line, 3 lines after
10+
linesOfContext: 7
11+
};
1112

1213
// global reference to slice
1314
var _slice = [].slice;
1415
var UNKNOWN_FUNCTION = '?';
1516

1617

17-
/**
18-
* _has, a better form of hasOwnProperty
19-
* Example: _has(MainHostObject, property) === true/false
20-
*
21-
* @param {Object} host object to check property
22-
* @param {string} key to check
23-
*/
24-
function _has(object, key) {
25-
return Object.prototype.hasOwnProperty.call(object, key);
26-
}
27-
28-
function _isUndefined(what) {
29-
return typeof what === 'undefined';
30-
}
31-
32-
/**
33-
* TraceKit.noConflict: Export TraceKit out to another variable
34-
* Example: var TK = TraceKit.noConflict()
35-
*/
36-
TraceKit.noConflict = function noConflict() {
37-
window.TraceKit = _oldTraceKit;
38-
return TraceKit;
39-
};
40-
4118
/**
4219
* TraceKit.wrap: Wrap any function in a TraceKit reporter
4320
* Example: func = TraceKit.wrap(func);
@@ -132,7 +109,7 @@ TraceKit.report = (function reportModuleWrapper() {
132109
return;
133110
}
134111
for (var i in handlers) {
135-
if (_has(handlers, i)) {
112+
if (hasKey(handlers, i)) {
136113
try {
137114
handlers[i].apply(null, [stack].concat(_slice.call(arguments, 2)));
138115
} catch (inner) {
@@ -162,7 +139,7 @@ TraceKit.report = (function reportModuleWrapper() {
162139
function traceKitWindowOnError(message, url, lineNo, colNo, ex) {
163140
var stack = null, skipNotify = false;
164141

165-
if (!_isUndefined(ex)) {
142+
if (!isUndefined(ex)) {
166143
// New chrome and blink send along a real error object
167144
// Let's just report that like a normal error.
168145
// See: https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror
@@ -363,7 +340,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
363340
* @return {Array.<string>} Source contents.
364341
*/
365342
function getSource(url) {
366-
if (!_has(sourceCache, url)) {
343+
if (!hasKey(sourceCache, url)) {
367344
// URL needs to be able to fetched within the acceptable domain. Otherwise,
368345
// cross-domain errors will be triggered.
369346
var source = '';
@@ -401,7 +378,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
401378
for (var i = 0; i < maxLines; ++i) {
402379
line = source[lineNo - i] + line;
403380

404-
if (!_isUndefined(line)) {
381+
if (!isUndefined(line)) {
405382
if ((m = reGuessFunction.exec(line))) {
406383
return m[1];
407384
} else if ((m = reFunctionArgNames.exec(line))) {
@@ -440,7 +417,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
440417
line -= 1; // convert to 0-based index
441418

442419
for (var i = start; i < end; ++i) {
443-
if (!_isUndefined(source[i])) {
420+
if (!isUndefined(source[i])) {
444421
context.push(source[i]);
445422
}
446423
}
@@ -791,7 +768,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
791768
source;
792769

793770
for (i in scripts) {
794-
if (_has(scripts, i) && !scripts[i].src) {
771+
if (hasKey(scripts, i) && !scripts[i].src) {
795772
inlineScriptBlocks.push(scripts[i]);
796773
}
797774
}
@@ -1079,23 +1056,3 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
10791056

10801057
return computeStackTrace;
10811058
}());
1082-
1083-
1084-
//Default options:
1085-
if (!TraceKit.remoteFetching) {
1086-
TraceKit.remoteFetching = true;
1087-
}
1088-
if (!TraceKit.collectWindowErrors) {
1089-
TraceKit.collectWindowErrors = true;
1090-
}
1091-
if (!TraceKit.linesOfContext || TraceKit.linesOfContext < 1) {
1092-
// 3 lines before, the offending line, 3 lines after
1093-
TraceKit.linesOfContext = 7;
1094-
}
1095-
1096-
1097-
1098-
// Export to global object
1099-
window.TraceKit = TraceKit;
1100-
1101-
}(window));

0 commit comments

Comments
 (0)