forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
352 lines (304 loc) · 8.84 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
'use strict'; // eslint-disable-line
module.exports = function(grunt) {
var path = require('path');
var os = require('os');
var through = require('through2');
var proxyquire = require('proxyquireify');
var versionify = require('browserify-versionify');
var derequire = require('derequire/plugin');
var collapser = require('bundle-collapser/plugin');
var excludedPlugins = ['react-native'];
var plugins = grunt.option('plugins');
// Create plugin paths and verify they exist
plugins = (plugins ? plugins.split(',') : []).map(function(plugin) {
var p = 'plugins/' + plugin + '.js';
if (!grunt.file.exists(p))
throw new Error("Plugin '" + plugin + "' not found in plugins directory.");
return p;
});
// custom browserify transformer to re-write plugins to
// self-register with Raven via addPlugin
function AddPluginBrowserifyTransformer() {
return function(file) {
return through(function(buf, enc, next) {
buf = buf.toString('utf8');
if (/plugins/.test(file)) {
buf += "\nrequire('../src/singleton').addPlugin(module.exports);";
}
this.push(buf);
next();
});
};
}
// Taken from http://dzone.com/snippets/calculate-all-combinations
var combine = function(a) {
var fn = function(n, src, got, all) {
if (n === 0) {
all.push(got);
return;
}
for (var j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
}
};
var excluded = excludedPlugins.map(function(plugin) {
return 'plugins/' + plugin + '.js';
});
// Remove the plugins that we don't want to build
a = a.filter(function(n) {
return excluded.indexOf(n) === -1;
});
var all = [a];
for (var i = 0; i < a.length; i++) {
fn(i, a, [], all);
}
return all;
};
var plugins = grunt.file.expand('plugins/*.js');
var cleanedPlugins = plugins.filter(function(plugin) {
var pluginName = path.basename(plugin, '.js');
return excludedPlugins.indexOf(pluginName) === -1;
});
var pluginSingleFiles = cleanedPlugins.map(function(plugin) {
var filename = path.basename(plugin);
var file = {};
file.src = plugin;
file.dest = path.join('build', 'plugins', filename);
return file;
});
var pluginCombinations = combine(plugins);
var pluginConcatFiles = pluginCombinations.reduce(function(dict, comb) {
var key = comb.map(function(plugin) {
return path.basename(plugin, '.js');
});
key.sort();
var dest = path.join('build/', key.join(','), '/raven.js');
dict[dest] = ['src/singleton.js'].concat(comb);
return dict;
}, {});
var browserifyConfig = {
options: {
banner: grunt.file.read('template/_copyright.js'),
browserifyOptions: {
standalone: 'Raven' // umd
},
transform: [versionify],
plugin: [derequire, collapser]
},
core: {
src: 'src/singleton.js',
dest: 'build/raven.js'
},
'plugins-combined': {
files: pluginConcatFiles,
options: {
transform: [[versionify], [new AddPluginBrowserifyTransformer()]]
}
},
test: {
src: 'test/**/*.test.js',
dest: 'build/raven.test.js',
options: {
browserifyOptions: {
debug: false // source maps
},
ignore: ['react-native'],
plugin: [proxyquire.plugin]
}
}
};
// Create a dedicated entry in browserify config for
// each individual plugin (each needs a unique `standalone`
// config)
var browserifyPluginTaskNames = [];
pluginSingleFiles.forEach(function(item) {
var name = item.src
.replace(/.*\//, '') // everything before slash
.replace('.js', ''); // extension
var capsName = name.charAt(0).toUpperCase() + name.slice(1);
var config = {
src: item.src,
dest: item.dest,
options: {
browserifyOptions: {
// e.g. Raven.Plugins.Angular
standalone: 'Raven.Plugins.' + capsName
}
}
};
browserifyConfig[name] = config;
browserifyPluginTaskNames.push('browserify:' + name);
});
var awsConfigPath = path.join(os.homedir(), '.aws', 'raven-js.json');
var gruntConfig = {
pkg: grunt.file.readJSON('package.json'),
aws: grunt.file.exists(awsConfigPath) ? grunt.file.readJSON(awsConfigPath) : {},
clean: ['build'],
browserify: browserifyConfig,
uglify: {
options: {
sourceMap: true,
// Only preserve comments that start with (!)
preserveComments: /^!/,
// Minify object properties that begin with _ ("private"
// methods and values)
mangleProperties: {
regex: /^_/
},
compress: {
booleans: true,
conditionals: true,
dead_code: true,
join_vars: true,
pure_getters: true,
sequences: true,
unused: true,
global_defs: {
__DEV__: false
}
}
},
dist: {
src: ['build/**/*.js'],
ext: '.min.js',
expand: true
}
},
release: {
options: {
npm: false,
commitMessage: 'Release <%= version %>'
}
},
s3: {
options: {
key: '<%= aws.key %>',
secret: '<%= aws.secret %>',
bucket: '<%= aws.bucket %>',
access: 'public-read',
// Limit concurrency
maxOperations: 20,
headers: {
// Surrogate-Key header for Fastly to purge by release
'x-amz-meta-surrogate-key': '<%= pkg.release %>'
}
},
all: {
upload: [
{
src: 'build/**/*',
dest: '<%= pkg.release %>/',
rel: 'build/'
}
]
}
},
connect: {
test: {
options: {
port: 8000,
debug: true,
keepalive: true
}
},
docs: {
options: {
port: 8000,
debug: true,
base: 'docs/_build/html',
keepalive: true
}
}
},
copy: {
dist: {
expand: true,
flatten: false,
cwd: 'build/',
src: '**',
dest: 'dist/'
}
},
sri: {
dist: {
src: ['dist/*.js'],
options: {
dest: 'dist/sri.json',
pretty: true
}
},
build: {
src: ['build/**/*.js'],
options: {
dest: 'build/sri.json',
pretty: true
}
}
}
};
grunt.initConfig(gruntConfig);
// Custom Grunt tasks
grunt.registerTask('version', function() {
var pkg = grunt.config.get('pkg');
// Verify version string in source code matches what's in package.json
var Raven = require('./src/raven');
if (Raven.prototype.VERSION !== pkg.version) {
return grunt.util.error(
'Mismatched version in src/raven.js: ' +
Raven.prototype.VERSION +
' (should be ' +
pkg.version +
')'
);
}
if (grunt.option('dev')) {
pkg.release = 'dev';
} else {
pkg.release = pkg.version;
}
grunt.config.set('pkg', pkg);
});
grunt.registerTask('config:ci', 'Verify CI config', function() {
if (!process.env.SAUCE_USERNAME)
console.warn('No SAUCE_USERNAME env variable defined.');
if (!process.env.SAUCE_ACCESS_KEY)
console.warn('No SAUCE_ACCESS_KEY env variable defined.');
if (!process.env.SAUCE_USERNAME || !process.env.SAUCE_ACCESS_KEY) process.exit(1);
});
// Grunt contrib tasks
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-copy');
// 3rd party Grunt tasks
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-release');
grunt.loadNpmTasks('grunt-s3');
grunt.loadNpmTasks('grunt-gitinfo');
grunt.loadNpmTasks('grunt-sri');
// Build tasks
grunt.registerTask('_prep', ['clean', 'gitinfo', 'version']);
grunt.registerTask(
'browserify.core',
['_prep', 'browserify:core'].concat(browserifyPluginTaskNames)
);
grunt.registerTask('browserify.plugins-combined', [
'_prep',
'browserify:plugins-combined'
]);
grunt.registerTask('build.test', ['_prep', 'browserify.core', 'browserify:test']);
grunt.registerTask('build.core', ['browserify.core', 'uglify', 'sri:dist']);
grunt.registerTask('build.plugins-combined', [
'browserify.plugins-combined',
'uglify',
'sri:dist',
'sri:build'
]);
grunt.registerTask('build', ['build.plugins-combined']);
grunt.registerTask('dist', ['build.core', 'copy:dist']);
grunt.registerTask('test:ci', ['config:ci', 'build.test']);
// Webserver tasks
grunt.registerTask('run:test', ['build.test', 'connect:test']);
grunt.registerTask('run:docs', ['connect:docs']);
grunt.registerTask('publish', ['build.plugins-combined', 's3']);
};