|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var path = require('path'); |
| 4 | +var browserify = require('browserify'); |
| 5 | +var derequire = require('derequire'); |
| 6 | + |
| 7 | +var bannerTemplate = '/*! p5.js v<%= pkg.version %> <%= grunt.template.today("mmmm dd, yyyy") %> */'; |
| 8 | + |
| 9 | +module.exports = function(grunt) { |
| 10 | + |
| 11 | + var srcFilePath = require.resolve('../../src/app.js'); |
| 12 | + |
| 13 | + // This file will not exist until it has been built |
| 14 | + var libFilePath = path.resolve('lib/p5.js'); |
| 15 | + |
| 16 | + grunt.registerTask('browserify', 'Compile the p5.js source with Browserify', function() { |
| 17 | + // Reading and writing files is asynchronous |
| 18 | + var done = this.async(); |
| 19 | + |
| 20 | + // Render the banner for the top of the file |
| 21 | + var banner = grunt.template.process(bannerTemplate); |
| 22 | + |
| 23 | + // Invoke Browserify programatically to bundle the code |
| 24 | + var bundle = browserify(srcFilePath, { |
| 25 | + standalone: 'p5' |
| 26 | + }) |
| 27 | + .transform('brfs') |
| 28 | + .bundle(); |
| 29 | + |
| 30 | + // Start the generated output with the banner comment, |
| 31 | + var code = banner + '\n'; |
| 32 | + |
| 33 | + // Then read the bundle into memory so we can run it through derequire |
| 34 | + bundle.on('data', function(data) { |
| 35 | + code += data; |
| 36 | + }).on('end', function() { |
| 37 | + |
| 38 | + // "code" is complete: create the distributable UMD build by running |
| 39 | + // the bundle through derequire, then write the bundle to disk. |
| 40 | + // (Derequire changes the bundle's internal "require" function to |
| 41 | + // something that will not interfere with this module being used |
| 42 | + // within a separate browserify bundle.) |
| 43 | + grunt.file.write(libFilePath, derequire(code)); |
| 44 | + |
| 45 | + // Print a success message |
| 46 | + grunt.log.writeln('>>'.green + ' Bundle ' + 'lib/p5.js'.cyan + ' created.'); |
| 47 | + |
| 48 | + // Complete the task |
| 49 | + done(); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}; |
0 commit comments