Skip to content

Commit 3844fb6

Browse files
committed
Merge pull request #430 from getsentry/modules
Refactor raven-js to use CommonJS modules, class instances (2.x)
2 parents 08f64a4 + dc1dfef commit 3844fb6

23 files changed

+1728
-1600
lines changed

.jshintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"globalstrict": true,
44
"browser": true,
55
"predef": [
6-
"TraceKit",
76
"console",
8-
"_slice"
7+
"module",
8+
"require"
99
]
1010
}

Gruntfile.js

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1+
var proxyquire = require('proxyquireify');
2+
13
module.exports = function(grunt) {
24
"use strict";
35

46
var _ = require('lodash');
57
var path = require('path');
6-
7-
var coreFiles = [
8-
'template/_header.js',
9-
'vendor/**/*.js',
10-
'src/**/*.js',
11-
'template/_footer.js'
12-
];
8+
var through = require('through2');
139

1410
var excludedPlugins = [
1511
'react-native'
@@ -26,6 +22,21 @@ module.exports = function(grunt) {
2622
return path;
2723
});
2824

25+
// custom browserify transformer to re-write plugins to
26+
// self-register with Raven via addPlugin
27+
function AddPluginBrowserifyTransformer() {
28+
return function (file) {
29+
return through(function (buf, enc, next) {
30+
buf = buf.toString('utf8');
31+
if (/plugins/.test(file)) {
32+
buf += "\nrequire('../src/singleton').addPlugin(module.exports);";
33+
}
34+
this.push(buf);
35+
next();
36+
});
37+
};
38+
}
39+
2940
// Taken from http://dzone.com/snippets/calculate-all-combinations
3041
var combine = function (a) {
3142
var fn = function (n, src, got, all) {
@@ -65,7 +76,7 @@ module.exports = function(grunt) {
6576
key.sort();
6677

6778
var dest = path.join('build/', key.join(','), '/raven.js');
68-
dict[dest] = coreFiles.concat(comb);
79+
dict[dest] = ['src/singleton.js'].concat(comb);
6980

7081
return dict;
7182
}, {});
@@ -75,18 +86,35 @@ module.exports = function(grunt) {
7586
aws: grunt.file.exists('aws.json') ? grunt.file.readJSON('aws.json'): {},
7687

7788
clean: ['build'],
78-
concat: {
89+
90+
browserify: {
7991
options: {
80-
separator: '\n',
81-
banner: grunt.file.read('template/_copyright.js'),
82-
process: true
92+
browserifyOptions: {
93+
banner: grunt.file.read('template/_copyright.js'),
94+
standalone: 'Raven' // umd
95+
}
8396
},
8497
core: {
85-
src: coreFiles.concat(plugins),
98+
src: 'src/singleton.js',
8699
dest: 'build/raven.js'
87100
},
88-
all: {
89-
files: pluginConcatFiles
101+
plugins: {
102+
files: pluginConcatFiles,
103+
options: {
104+
transform: [
105+
[ new AddPluginBrowserifyTransformer() ]
106+
]
107+
}
108+
},
109+
test: {
110+
src: 'test/**/*.test.js',
111+
dest: 'build/raven.test.js',
112+
options: {
113+
browserifyOptions: {
114+
debug: true // source maps
115+
},
116+
plugin: [proxyquire.plugin]
117+
}
90118
}
91119
},
92120

@@ -100,7 +128,13 @@ module.exports = function(grunt) {
100128
sourceMappingURL: function (dest) {
101129
return path.basename(dest, '.js') + '.map';
102130
},
103-
preserveComments: 'some'
131+
preserveComments: 'some',
132+
compress: {
133+
dead_code: true,
134+
global_defs: {
135+
"TEST": false
136+
}
137+
}
104138
},
105139
dist: {
106140
src: ['build/**/*.js'],
@@ -251,13 +285,13 @@ module.exports = function(grunt) {
251285

252286
// Grunt contrib tasks
253287
grunt.loadNpmTasks('grunt-contrib-uglify');
254-
grunt.loadNpmTasks('grunt-contrib-concat');
255288
grunt.loadNpmTasks('grunt-contrib-clean');
256289
grunt.loadNpmTasks('grunt-contrib-jshint');
257290
grunt.loadNpmTasks('grunt-contrib-connect');
258291
grunt.loadNpmTasks('grunt-contrib-copy');
259292

260293
// 3rd party Grunt tasks
294+
grunt.loadNpmTasks('grunt-browserify');
261295
grunt.loadNpmTasks('grunt-mocha');
262296
grunt.loadNpmTasks('grunt-release');
263297
grunt.loadNpmTasks('grunt-s3');
@@ -266,15 +300,16 @@ module.exports = function(grunt) {
266300

267301
// Build tasks
268302
grunt.registerTask('_prep', ['clean', 'gitinfo', 'version']);
269-
grunt.registerTask('concat.core', ['_prep', 'concat:core']);
270-
grunt.registerTask('concat.all', ['_prep', 'concat:all']);
271-
grunt.registerTask('build.core', ['concat.core', 'uglify', 'fixSourceMaps', 'sri:dist']);
272-
grunt.registerTask('build.all', ['concat.all', 'uglify', 'fixSourceMaps', 'sri:dist', 'sri:build']);
303+
grunt.registerTask('browserify.core', ['_prep', 'browserify:core']);
304+
grunt.registerTask('browserify.plugins', ['_prep', 'browserify:plugins']);
305+
grunt.registerTask('build.test', ['_prep', 'browserify:test']);
306+
grunt.registerTask('build.core', ['browserify.core', 'uglify', 'fixSourceMaps', 'sri:dist']);
307+
grunt.registerTask('build.all', ['browserify.plugins', 'uglify', 'fixSourceMaps', 'sri:dist', 'sri:build']);
273308
grunt.registerTask('build', ['build.all']);
274309
grunt.registerTask('dist', ['build.core', 'copy:dist']);
275310

276311
// Test task
277-
grunt.registerTask('test', ['jshint', 'mocha']);
312+
grunt.registerTask('test', ['jshint', 'browserify.core', 'browserify:test', 'mocha']);
278313

279314
// Webserver tasks
280315
grunt.registerTask('run:test', ['connect:test']);

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
"type": "git",
1212
"url": "git://github.com/getsentry/raven-js.git"
1313
},
14-
"main": "dist/raven.js",
14+
"main": "src/singleton.js",
1515
"devDependencies": {
1616
"chai": "~1.8.1",
1717
"grunt": "^0.4.5",
18+
"grunt-browserify": "^4.0.1",
1819
"grunt-cli": "~0.1.9",
1920
"grunt-contrib-clean": "~0.4.0",
2021
"grunt-contrib-concat": "~0.3.0",
@@ -29,7 +30,9 @@
2930
"grunt-sri": "mattrobenolt/grunt-sri#pretty",
3031
"jquery": "^2.1.4",
3132
"lodash": "~2.4.0",
32-
"sinon": "~1.7.3"
33+
"proxyquireify": "^3.0.0",
34+
"sinon": "~1.7.3",
35+
"through2": "^2.0.0"
3336
},
3437
"keywords": [
3538
"exceptions",

plugins/angular.js

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,58 @@
33
*
44
* Provides an $exceptionHandler for Angular.js
55
*/
6-
;(function(window) {
76
'use strict';
87

9-
var angular = window.angular,
10-
Raven = window.Raven;
8+
// See https://github.com/angular/angular.js/blob/v1.4.7/src/minErr.js
9+
var angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.+?)\n(\S+)$/;
1110

12-
// quit if angular isn't on the page
13-
if (!(angular && Raven)) return;
11+
function angularPlugin(Raven, angular) {
12+
/*jshint validthis:true*/
13+
angular = angular || window.angular;
1414

15-
function RavenProvider() {
16-
this.$get = ['$window', function($window, $log) {
17-
return $window.Raven;
18-
}];
19-
}
15+
if (!angular) return;
2016

21-
function ExceptionHandlerProvider($provide) {
22-
$provide.decorator('$exceptionHandler',
23-
['Raven', '$delegate', exceptionHandler]);
24-
}
17+
function RavenProvider() {
18+
this.$get = ['$window', function($window) {
19+
return Raven;
20+
}];
21+
}
2522

26-
function exceptionHandler(Raven, $delegate) {
27-
return function (ex, cause) {
28-
Raven.captureException(ex, {
29-
extra: { cause: cause }
30-
});
31-
$delegate(ex, cause);
32-
};
33-
}
23+
function ExceptionHandlerProvider($provide) {
24+
$provide.decorator('$exceptionHandler',
25+
['Raven', '$delegate', exceptionHandler]);
26+
}
3427

35-
// See https://github.com/angular/angular.js/blob/v1.4.7/src/minErr.js
36-
var angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.+?)\n(\S+)$/;
28+
function exceptionHandler(Raven, $delegate) {
29+
return function (ex, cause) {
30+
Raven.captureException(ex, {
31+
extra: { cause: cause }
32+
});
33+
$delegate(ex, cause);
34+
};
35+
}
3736

38-
Raven.addPlugin(function () {
3937
angular.module('ngRaven', [])
4038
.provider('Raven', RavenProvider)
4139
.config(['$provide', ExceptionHandlerProvider]);
42-
});
43-
44-
Raven.setDataCallback(function(data) {
45-
// We only care about mutating an exception
46-
var exception = data.exception;
47-
if (exception) {
48-
exception = exception.values[0];
49-
var matches = angularPattern.exec(exception.value);
50-
51-
if (matches) {
52-
// This type now becomes something like: $rootScope:inprog
53-
exception.type = matches[1];
54-
exception.value = matches[2];
55-
data.message = exception.type + ': ' + exception.value;
56-
// auto set a new tag specifically for the angular error url
57-
data.extra.angularDocs = matches[3].substr(0, 250);
40+
41+
Raven.setDataCallback(function(data) {
42+
// We only care about mutating an exception
43+
var exception = data.exception;
44+
if (exception) {
45+
exception = exception.values[0];
46+
var matches = angularPattern.exec(exception.value);
47+
48+
if (matches) {
49+
// This type now becomes something like: $rootScope:inprog
50+
exception.type = matches[1];
51+
exception.value = matches[2];
52+
data.message = exception.type + ': ' + exception.value;
53+
// auto set a new tag specifically for the angular error url
54+
data.extra.angularDocs = matches[3].substr(0, 250);
55+
}
5856
}
59-
}
60-
});
57+
});
58+
}
6159

62-
}(typeof window !== 'undefined' ? window : this));
60+
module.exports = angularPlugin;

plugins/backbone.js

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,54 @@
33
*
44
* Patches Backbone.Events callbacks.
55
*/
6-
;(function(window) {
76
'use strict';
87

9-
if (window.Raven) Raven.addPlugin(function backbonePlugin() {
8+
function backbonePlugin(Raven, Backbone) {
9+
Backbone = Backbone || window.Backbone;
1010

11-
var Backbone = window.Backbone;
11+
// quit if Backbone isn't on the page
12+
if (!Backbone) return;
1213

13-
// quit if Backbone isn't on the page
14-
if (!Backbone) return;
15-
16-
function makeBackboneEventsOn(oldOn) {
17-
return function BackboneEventsOn(name, callback, context) {
18-
var wrapCallback = function (cb) {
19-
if (Object.prototype.toString.call(cb) === '[object Function]') {
20-
var _callback = cb._callback || cb;
21-
cb = Raven.wrap(cb);
22-
cb._callback = _callback;
23-
}
24-
return cb;
25-
};
26-
if (Object.prototype.toString.call(name) === '[object Object]') {
27-
// Handle event maps.
28-
for (var key in name) {
29-
if (name.hasOwnProperty(key)) {
30-
name[key] = wrapCallback(name[key]);
14+
function makeBackboneEventsOn(oldOn) {
15+
return function BackboneEventsOn(name, callback, context) {
16+
var wrapCallback = function (cb) {
17+
if (Object.prototype.toString.call(cb) === '[object Function]') {
18+
var _callback = cb._callback || cb;
19+
cb = Raven.wrap(cb);
20+
cb._callback = _callback;
3121
}
22+
return cb;
23+
};
24+
if (Object.prototype.toString.call(name) === '[object Object]') {
25+
// Handle event maps.
26+
for (var key in name) {
27+
if (name.hasOwnProperty(key)) {
28+
name[key] = wrapCallback(name[key]);
29+
}
30+
}
31+
} else {
32+
callback = wrapCallback(callback);
3233
}
33-
} else {
34-
callback = wrapCallback(callback);
35-
}
36-
return oldOn.call(this, name, callback, context);
37-
};
38-
}
39-
40-
// We're too late to catch all of these by simply patching Backbone.Events.on
41-
var affectedObjects = [
42-
Backbone.Events,
43-
Backbone,
44-
Backbone.Model.prototype,
45-
Backbone.Collection.prototype,
46-
Backbone.View.prototype,
47-
Backbone.Router.prototype,
48-
Backbone.History.prototype
49-
], i = 0, l = affectedObjects.length;
50-
51-
for (; i < l; i++) {
52-
var affected = affectedObjects[i];
53-
affected.on = makeBackboneEventsOn(affected.on);
54-
affected.bind = affected.on;
34+
return oldOn.call(this, name, callback, context);
35+
};
36+
}
37+
38+
// We're too late to catch all of these by simply patching Backbone.Events.on
39+
var affectedObjects = [
40+
Backbone.Events,
41+
Backbone,
42+
Backbone.Model.prototype,
43+
Backbone.Collection.prototype,
44+
Backbone.View.prototype,
45+
Backbone.Router.prototype,
46+
Backbone.History.prototype
47+
], i = 0, l = affectedObjects.length;
48+
49+
for (; i < l; i++) {
50+
var affected = affectedObjects[i];
51+
affected.on = makeBackboneEventsOn(affected.on);
52+
affected.bind = affected.on;
53+
}
5554
}
5655

57-
});
58-
59-
}(typeof window !== 'undefined' ? window : this));
56+
module.exports = backbonePlugin;

0 commit comments

Comments
 (0)