|
2 | 2 | module.exports = function(grunt) {
|
3 | 3 | var path = require('path');
|
4 | 4 | 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'); |
10 | 5 |
|
11 | 6 | var excludedPlugins = ['react-native'];
|
12 | 7 |
|
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) { |
73 | 9 | var pluginName = path.basename(plugin, '.js');
|
74 | 10 |
|
75 | 11 | return excludedPlugins.indexOf(pluginName) === -1;
|
76 | 12 | });
|
77 | 13 |
|
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'); |
97 | 16 |
|
98 |
| - return dict; |
99 |
| - }, {}); |
| 17 | + var tests = grunt.file.expand('test/**/*.test.js'); |
100 | 18 |
|
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 = { |
110 | 20 | 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 | + ] |
113 | 33 | },
|
114 |
| - 'plugins-combined': { |
115 |
| - files: pluginConcatFiles, |
116 |
| - options: { |
117 |
| - transform: [[versionify], [new AddPluginBrowserifyTransformer()]] |
118 |
| - } |
| 34 | + plugins: { |
| 35 | + options: [] |
119 | 36 | },
|
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: [] |
130 | 42 | }
|
131 | 43 | };
|
132 | 44 |
|
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 |
139 | 49 | .replace(/.*\//, '') // everything before slash
|
140 | 50 | .replace('.js', ''); // extension
|
141 | 51 | var capsName = name.charAt(0).toUpperCase() + name.slice(1);
|
142 | 52 | 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') |
150 | 60 | }
|
151 | 61 | };
|
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); |
154 | 95 | });
|
155 | 96 |
|
156 | 97 | var awsConfigPath = path.join(os.homedir(), '.aws', 'raven-js.json');
|
157 | 98 | var gruntConfig = {
|
158 | 99 | pkg: grunt.file.readJSON('package.json'),
|
159 | 100 | aws: grunt.file.exists(awsConfigPath) ? grunt.file.readJSON(awsConfigPath) : {},
|
160 | 101 |
|
161 |
| - clean: ['build'], |
| 102 | + clean: ['build', 'plugins/combinations'], |
162 | 103 |
|
163 |
| - browserify: browserifyConfig, |
| 104 | + rollup: rollupConfig, |
164 | 105 |
|
165 | 106 | uglify: {
|
166 | 107 | options: {
|
@@ -277,6 +218,30 @@ module.exports = function(grunt) {
|
277 | 218 | grunt.initConfig(gruntConfig);
|
278 | 219 |
|
279 | 220 | // 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 | + |
280 | 245 | grunt.registerTask('version', function() {
|
281 | 246 | var pkg = grunt.config.get('pkg');
|
282 | 247 |
|
@@ -315,34 +280,29 @@ module.exports = function(grunt) {
|
315 | 280 | grunt.loadNpmTasks('grunt-contrib-copy');
|
316 | 281 |
|
317 | 282 | // 3rd party Grunt tasks
|
318 |
| - grunt.loadNpmTasks('grunt-browserify'); |
319 | 283 | grunt.loadNpmTasks('grunt-release');
|
320 | 284 | grunt.loadNpmTasks('grunt-s3');
|
321 | 285 | grunt.loadNpmTasks('grunt-gitinfo');
|
322 | 286 | grunt.loadNpmTasks('grunt-sri');
|
323 | 287 |
|
324 | 288 | // 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', [ |
331 | 293 | '_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', |
339 | 297 | 'sri:dist',
|
340 | 298 | 'sri:build'
|
341 | 299 | ]);
|
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']); |
344 | 303 |
|
345 |
| - grunt.registerTask('test:ci', ['config:ci', 'build.test']); |
| 304 | + // Test tasks |
| 305 | + grunt.registerTask('test:ci', ['config:ci', 'build:test']); |
346 | 306 |
|
347 | 307 | // Webserver tasks
|
348 | 308 | grunt.registerTask('run:test', ['build.test', 'connect:test']);
|
|
0 commit comments