Skip to content

Commit 746da29

Browse files
jaylinskikamilogorek
authored andcommitted
build: Replace Browserify with an ES compatible Rollup build (#1152)
Related to #1031, #1130
1 parent 919051a commit 746da29

File tree

9 files changed

+253
-167
lines changed

9 files changed

+253
-167
lines changed

.babelrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"presets": [
3+
["@babel/preset-env", {
4+
"modules": false,
5+
"targets": {
6+
"browsers": [
7+
"last 1 version",
8+
"android >= 4.4",
9+
"ie >= 10"
10+
]
11+
}
12+
}]
13+
]
14+
}

.eslintrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
"browser": true,
77
"builtin": true
88
},
9+
"parserOptions": {
10+
"ecmaVersion": 6
11+
},
912
"globals": {
10-
"require": false
13+
"require": false,
14+
"Promise": true
1115
},
1216
"rules": {
1317
"block-scoped-var": 2,

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ docs/doctrees
1212

1313
build
1414
node_modules
15+
plugins/combinations
1516
npm-debug.log
1617

1718
scratch/

Gruntfile.js

Lines changed: 106 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -2,165 +2,106 @@
22
module.exports = function(grunt) {
33
var path = require('path');
44
var os = require('os');
5-
var through = require('through2');
6-
var proxyquire = require('proxyquireify');
7-
var versionify = require('browserify-versionify');
8-
var derequire = require('derequire/plugin');
9-
var collapser = require('bundle-collapser/plugin');
105

116
var excludedPlugins = ['react-native'];
127

13-
var plugins = grunt.option('plugins');
14-
// Create plugin paths and verify they exist
15-
plugins = (plugins ? plugins.split(',') : []).map(function(plugin) {
16-
var p = 'plugins/' + plugin + '.js';
17-
18-
if (!grunt.file.exists(p))
19-
throw new Error("Plugin '" + plugin + "' not found in plugins directory.");
20-
21-
return p;
22-
});
23-
24-
// custom browserify transformer to re-write plugins to
25-
// self-register with Raven via addPlugin
26-
function AddPluginBrowserifyTransformer() {
27-
return function(file) {
28-
return through(function(buf, enc, next) {
29-
buf = buf.toString('utf8');
30-
if (/plugins/.test(file)) {
31-
buf += "\nrequire('../src/singleton').addPlugin(module.exports);";
32-
}
33-
this.push(buf);
34-
next();
35-
});
36-
};
37-
}
38-
39-
// Taken from http://dzone.com/snippets/calculate-all-combinations
40-
var combine = function(a) {
41-
var fn = function(n, src, got, all) {
42-
if (n === 0) {
43-
all.push(got);
44-
return;
45-
}
46-
47-
for (var j = 0; j < src.length; j++) {
48-
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
49-
}
50-
};
51-
52-
var excluded = excludedPlugins.map(function(plugin) {
53-
return 'plugins/' + plugin + '.js';
54-
});
55-
56-
// Remove the plugins that we don't want to build
57-
a = a.filter(function(n) {
58-
return excluded.indexOf(n) === -1;
59-
});
60-
61-
var all = [a];
62-
63-
for (var i = 0; i < a.length; i++) {
64-
fn(i, a, [], all);
65-
}
66-
67-
return all;
68-
};
69-
70-
var plugins = grunt.file.expand('plugins/*.js');
71-
72-
var cleanedPlugins = plugins.filter(function(plugin) {
8+
var plugins = grunt.file.expand('plugins/*.js').filter(function(plugin) {
739
var pluginName = path.basename(plugin, '.js');
7410

7511
return excludedPlugins.indexOf(pluginName) === -1;
7612
});
7713

78-
var pluginSingleFiles = cleanedPlugins.map(function(plugin) {
79-
var filename = path.basename(plugin);
80-
81-
var file = {};
82-
file.src = plugin;
83-
file.dest = path.join('build', 'plugins', filename);
84-
85-
return file;
86-
});
87-
88-
var pluginCombinations = combine(plugins);
89-
var pluginConcatFiles = pluginCombinations.reduce(function(dict, comb) {
90-
var key = comb.map(function(plugin) {
91-
return path.basename(plugin, '.js');
92-
});
93-
key.sort();
94-
95-
var dest = path.join('build/', key.join(','), '/raven.js');
96-
dict[dest] = ['src/singleton.js'].concat(comb);
14+
// These files are generated with the 'generate:plugins-combined' npm script
15+
var pluginCombinations = grunt.file.expand('plugins/combinations/*.js');
9716

98-
return dict;
99-
}, {});
17+
var tests = grunt.file.expand('test/**/*.test.js');
10018

101-
var browserifyConfig = {
102-
options: {
103-
banner: grunt.file.read('template/_copyright.js'),
104-
browserifyOptions: {
105-
standalone: 'Raven' // umd
106-
},
107-
transform: [versionify],
108-
plugin: [derequire, collapser]
109-
},
19+
var rollupConfig = {
11020
core: {
111-
src: 'src/singleton.js',
112-
dest: 'build/raven.js'
21+
options: [
22+
{
23+
input: {
24+
input: 'src/singleton.js'
25+
},
26+
output: {
27+
file: 'build/raven.js',
28+
name: 'Raven',
29+
banner: grunt.file.read('template/_copyright.js')
30+
}
31+
}
32+
]
11333
},
114-
'plugins-combined': {
115-
files: pluginConcatFiles,
116-
options: {
117-
transform: [[versionify], [new AddPluginBrowserifyTransformer()]]
118-
}
34+
plugins: {
35+
options: []
11936
},
120-
test: {
121-
src: 'test/**/*.test.js',
122-
dest: 'build/raven.test.js',
123-
options: {
124-
browserifyOptions: {
125-
debug: false // source maps
126-
},
127-
ignore: ['react-native'],
128-
plugin: [proxyquire.plugin]
129-
}
37+
pluginCombinations: {
38+
options: []
39+
},
40+
tests: {
41+
options: []
13042
}
13143
};
13244

133-
// Create a dedicated entry in browserify config for
134-
// each individual plugin (each needs a unique `standalone`
135-
// config)
136-
var browserifyPluginTaskNames = [];
137-
pluginSingleFiles.forEach(function(item) {
138-
var name = item.src
45+
// Create a dedicated entry in rollup config for each individual
46+
// plugin (each needs a unique `standalone` config)
47+
plugins.forEach(function(plugin) {
48+
var name = plugin
13949
.replace(/.*\//, '') // everything before slash
14050
.replace('.js', ''); // extension
14151
var capsName = name.charAt(0).toUpperCase() + name.slice(1);
14252
var config = {
143-
src: item.src,
144-
dest: item.dest,
145-
options: {
146-
browserifyOptions: {
147-
// e.g. Raven.Plugins.Angular
148-
standalone: 'Raven.Plugins.' + capsName
149-
}
53+
input: {
54+
input: plugin
55+
},
56+
output: {
57+
file: path.join('build', 'plugins', path.basename(plugin)),
58+
name: 'Raven.Plugins.' + capsName,
59+
banner: grunt.file.read('template/_copyright.js')
15060
}
15161
};
152-
browserifyConfig[name] = config;
153-
browserifyPluginTaskNames.push('browserify:' + name);
62+
63+
rollupConfig.plugins.options.push(config);
64+
});
65+
66+
// Create a dedicated entry in rollup config for each individual plugin combination
67+
pluginCombinations.forEach(function(pluginCombination) {
68+
var config = {
69+
input: {
70+
input: pluginCombination
71+
},
72+
output: {
73+
file: path.join('build', path.basename(pluginCombination, '.js'), 'raven.js'),
74+
name: 'Raven',
75+
banner: grunt.file.read('template/_copyright.js')
76+
}
77+
};
78+
79+
rollupConfig.pluginCombinations.options.push(config);
80+
});
81+
82+
// Transpile all test scripts
83+
tests.forEach(function (test) {
84+
var config = {
85+
input: {
86+
input: test
87+
},
88+
output: {
89+
file: path.join('build', path.basename(test)),
90+
name: path.basename(test, '.js'),
91+
}
92+
};
93+
94+
rollupConfig.tests.options.push(config);
15495
});
15596

15697
var awsConfigPath = path.join(os.homedir(), '.aws', 'raven-js.json');
15798
var gruntConfig = {
15899
pkg: grunt.file.readJSON('package.json'),
159100
aws: grunt.file.exists(awsConfigPath) ? grunt.file.readJSON(awsConfigPath) : {},
160101

161-
clean: ['build'],
102+
clean: ['build', 'plugins/combinations'],
162103

163-
browserify: browserifyConfig,
104+
rollup: rollupConfig,
164105

165106
uglify: {
166107
options: {
@@ -277,6 +218,30 @@ module.exports = function(grunt) {
277218
grunt.initConfig(gruntConfig);
278219

279220
// Custom Grunt tasks
221+
grunt.registerMultiTask('rollup', 'Create the bundles', function() {
222+
var build = require('./scripts/build');
223+
var options = this.options();
224+
var done = this.async();
225+
226+
var promises = Object.keys(options).map(function(key) {
227+
return build(options[key].input, options[key].output);
228+
});
229+
230+
Promise.all(promises)
231+
.then(function() {
232+
done();
233+
})
234+
['catch'](function(error) {
235+
grunt.fail.warn(error);
236+
});
237+
});
238+
239+
grunt.registerTask('generate-plugin-combinations', function() {
240+
var dest = './plugins/combinations';
241+
grunt.file.mkdir(dest);
242+
require('./scripts/generate-plugin-combinations')(plugins, dest);
243+
});
244+
280245
grunt.registerTask('version', function() {
281246
var pkg = grunt.config.get('pkg');
282247

@@ -315,34 +280,29 @@ module.exports = function(grunt) {
315280
grunt.loadNpmTasks('grunt-contrib-copy');
316281

317282
// 3rd party Grunt tasks
318-
grunt.loadNpmTasks('grunt-browserify');
319283
grunt.loadNpmTasks('grunt-release');
320284
grunt.loadNpmTasks('grunt-s3');
321285
grunt.loadNpmTasks('grunt-gitinfo');
322286
grunt.loadNpmTasks('grunt-sri');
323287

324288
// Build tasks
325-
grunt.registerTask('_prep', ['clean', 'gitinfo', 'version']);
326-
grunt.registerTask(
327-
'browserify.core',
328-
['_prep', 'browserify:core'].concat(browserifyPluginTaskNames)
329-
);
330-
grunt.registerTask('browserify.plugins-combined', [
289+
grunt.registerTask('_prep', ['gitinfo', 'version']);
290+
grunt.registerTask('build.test', ['_prep', 'rollup:core', 'rollup:tests']);
291+
grunt.registerTask('build.core', ['_prep', 'rollup:core', 'sri:dist']);
292+
grunt.registerTask('build.plugins', [
331293
'_prep',
332-
'browserify:plugins-combined'
333-
]);
334-
grunt.registerTask('build.test', ['_prep', 'browserify.core', 'browserify:test']);
335-
grunt.registerTask('build.core', ['browserify.core', 'uglify', 'sri:dist']);
336-
grunt.registerTask('build.plugins-combined', [
337-
'browserify.plugins-combined',
338-
'uglify',
294+
'generate-plugin-combinations',
295+
'rollup:plugins',
296+
'rollup:pluginCombinations',
339297
'sri:dist',
340298
'sri:build'
341299
]);
342-
grunt.registerTask('build', ['build.plugins-combined']);
343-
grunt.registerTask('dist', ['build.core', 'copy:dist']);
300+
grunt.registerTask('build', ['build.core', 'build.plugins', 'uglify']);
301+
302+
grunt.registerTask('dist', ['clean', 'build', 'copy:dist']);
344303

345-
grunt.registerTask('test:ci', ['config:ci', 'build.test']);
304+
// Test tasks
305+
grunt.registerTask('test:ci', ['config:ci', 'build:test']);
346306

347307
// Webserver tasks
348308
grunt.registerTask('run:test', ['build.test', 'connect:test']);

karma.unit.config.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
var commonConfig = require('./karma.config');
22

3-
var testFiles = ['test/globals.js', 'build/raven.test.js'];
3+
var testFiles = [
4+
'test/globals.js',
5+
'build/angular.test.js',
6+
'build/console.test.js',
7+
'build/json-stringify-safe.test.js',
8+
'build/raven.test.js',
9+
'build/react-native.test.js',
10+
'build/tracekit.test.js',
11+
'build/tracekit-parser.test.js',
12+
'build/utils.test.js',
13+
'build/vue.test.js',
14+
];
415

516
module.exports = function(config) {
617
var testConfig = Object.assign(

0 commit comments

Comments
 (0)