Skip to content

Commit 4496014

Browse files
author
Lauren McCarthy
committed
Merge pull request #847 from kadamwhite/build/fix-browserify-woes
Pass generated browserify bundle through derequire
2 parents 9174558 + ce344be commit 4496014

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

Gruntfile.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ module.exports = function(grunt) {
5858
reporter: require('jscs-stylish').path
5959
},
6060
build: {
61-
src: ['Gruntfile.js']
61+
src: [
62+
'Gruntfile.js',
63+
'build/**/*.js'
64+
]
6265
},
6366
source: {
6467
src: [
@@ -77,7 +80,10 @@ module.exports = function(grunt) {
7780
options: {
7881
jshintrc: '.jshintrc'
7982
},
80-
src: ['Gruntfile.js']
83+
src: [
84+
'Gruntfile.js',
85+
'build/**/*.js'
86+
]
8187
},
8288
source: {
8389
options: {
@@ -162,21 +168,6 @@ module.exports = function(grunt) {
162168
}
163169
},
164170

165-
// Build p5 into a single, UMD-wrapped file
166-
browserify: {
167-
p5: {
168-
options: {
169-
transform: ['brfs'],
170-
browserifyOptions: {
171-
standalone: 'p5'
172-
},
173-
banner: '/*! p5.js v<%= pkg.version %> <%= grunt.template.today("mmmm dd, yyyy") %> */'
174-
},
175-
src: 'src/app.js',
176-
dest: 'lib/p5.js'
177-
}
178-
},
179-
180171
// The actual compile step: This should collect all the dependencies
181172
// and compile them into a single file.
182173
requirejs: {
@@ -259,8 +250,10 @@ module.exports = function(grunt) {
259250
}
260251
});
261252

253+
// Load task definitions
254+
grunt.loadTasks('build/tasks');
255+
262256
// Load the external libraries used.
263-
grunt.loadNpmTasks('grunt-browserify');
264257
grunt.loadNpmTasks('grunt-jscs');
265258
grunt.loadNpmTasks('grunt-contrib-jshint');
266259
grunt.loadNpmTasks('grunt-contrib-watch');

build/tasks/browserify.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
"version": "0.4.7",
1414
"devDependencies": {
1515
"amdclean": "~0.3.3",
16+
"browserify": "^11.0.1",
1617
"concat-files": "^0.1.0",
18+
"derequire": "^2.0.0",
1719
"grunt": "^0.4.5",
18-
"grunt-browserify": "^3.8.0",
1920
"grunt-cli": "^0.1.13",
2021
"grunt-contrib-concat": "^0.5.1",
2122
"grunt-contrib-connect": "^0.9.0",

0 commit comments

Comments
 (0)