-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathbrowserify.js
119 lines (97 loc) · 3.56 KB
/
browserify.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
// This file holds the "browersify" task which compiles the individual src/ code into p5.js and p5.min.js.
'use strict';
import { resolve } from 'path';
import browserify from 'browserify';
import derequire from 'derequire';
const bannerTemplate =
'/*! p5.js v<%= pkg.version %> <%= grunt.template.today("mmmm dd, yyyy") %> */';
module.exports = function(grunt) {
const srcFilePath = require.resolve('../../src/app.js');
grunt.registerTask(
'browserify',
'Compile the p5.js source with Browserify',
function(param) {
const isMin = param === 'min';
const isTest = param === 'test';
const isDev = param === 'dev';
const filename = isMin
? 'p5.pre-min.js'
: isTest ? 'p5-test.js' : 'p5.js';
// This file will not exist until it has been built
const libFilePath = resolve('lib/' + filename);
// Reading and writing files is asynchronous
const done = this.async();
// Render the banner for the top of the file
const banner = grunt.template.process(bannerTemplate);
let globalVars = {};
if (isDev) {
globalVars['P5_DEV_BUILD'] = () => true;
}
// Invoke Browserify programatically to bundle the code
let browserified = browserify(srcFilePath, {
standalone: 'p5',
insertGlobalVars: globalVars
});
if (isMin) {
// These paths should be the exact same as what are used in the import
// statements in the source. They are not relative to this file. It's
// just how browserify works apparently.
browserified = browserified
.exclude('../../docs/reference/data.json')
.exclude('../../../docs/parameterData.json')
.exclude('../../translations')
.exclude('./browser_errors')
.ignore('i18next')
.ignore('i18next-browser-languagedetector');
}
if (!isDev) {
browserified = browserified.exclude('../../translations/dev');
}
const babelifyOpts = {
global: true
};
if (isTest) {
babelifyOpts.envName = 'test';
}
const bundle = browserified
.transform('brfs-babel')
.transform('babelify', babelifyOpts)
.bundle();
// Start the generated output with the banner comment,
let code = banner + '\n';
// Then read the bundle into memory so we can run it through derequire
bundle
.on('data', function(data) {
code += data;
})
.on('end', function() {
code = code.replace(
"'VERSION_CONST_WILL_BE_REPLACED_BY_BROWSERIFY_BUILD_PROCESS'",
grunt.template.process("'<%= pkg.version %>'")
);
// "code" is complete: create the distributable UMD build by running
// the bundle through derequire
// (Derequire changes the bundle's internal "require" function to
// something that will not interfere with this module being used
// within a separate browserify bundle.)
code = derequire(code);
// and prettify the code
if (!isMin) {
const prettyFast = require('pretty-fast');
code = prettyFast(code, {
url: '(anonymous)',
indent: ' '
}).code;
}
// finally, write it to disk
grunt.file.write(libFilePath, code);
// Print a success message
grunt.log.writeln(
'>>'.green + ' Bundle ' + ('lib/' + filename).cyan + ' created.'
);
// Complete the task
done();
});
}
);
};