-
Notifications
You must be signed in to change notification settings - Fork 36
/
gulpfile.js
270 lines (226 loc) · 7.29 KB
/
gulpfile.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const { src, task, dest, series, watch } = require( 'gulp' ),
babel = require( 'gulp-babel' ),
babelify = require( 'babelify' ),
browserify = require( 'browserify' ),
source = require( 'vinyl-source-stream' ),
rename = require( 'gulp-rename' ),
fs = require( 'fs' ),
guglify = require( 'gulp-uglify-es' ).default,
buffer = require( 'vinyl-buffer' ),
gzip = require( 'gulp-gzip' ),
clean = require( 'gulp-clean' ),
workletStr = {
prefix:`const global = typeof window === 'undefined' ? {} : window;
let Gibberish = null,
Clock = null,
time = 0,
sin = null,
sinr = null,
sinn = null,
cos = null,
cosr = null,
cosn = null,
abs = null,
random= null,
floor = null,
ceil = null,
round = null,
min = null,
max = null,
g = null
let initialized = false;\n`,
postfix:`
global.Gibberish.workletProcessor = GibberishProcessor
registerProcessor( 'gibberish', global.Gibberish.workletProcessor );
`
}
let workletBlob = workletStr.prefix
const workletFnc = cb => {
const gibberishText = fs.readFileSync( './dist/gibberish.js', 'utf-8' )
const processorText = fs.readFileSync( './js/workletProcessor.js', 'utf-8' )
workletBlob += gibberishText
workletBlob += processorText
workletBlob += workletStr.postfix
fs.writeFileSync( './dist/gibberish_worklet.js', workletBlob )
cb()
}
const jsFunc = cb => {
const out = browserify({ standalone:'Gibberish' })
.require( './js/index.js', { entry: true } )
.transform( babelify, {
presets: ["@babel/env"],
plugins: [ jsdsp ],
//global: true,
//only: [/\/node_modules\/(\@strudel.cycles\/)/]
})
.on( 'error', e=>console.log('transform error',e ) )
.bundle()
.on( 'error', e=>console.log('bundle error', e.toString() ) )
.pipe( source( 'gibberish.js' ) )
.pipe( dest( './dist' ) )
cb()
}
/*const minifyLib = ()=> {
return gulp.src( './dist/gibberish.js' )
.pipe( guglify() )
.pipe( rename('gibberish.min.js') )
.pipe( gulp.dest('./dist') )
.pipe( gzip() )
.pipe( rename('gibberish.min.js.gz') )
.pipe( gulp.dest('./dist') )
}
const minifyWorklet = ()=> {
return gulp.src( './dist/gibberish_worklet.js' )
.pipe( guglify() )
.pipe( rename('gibberish_worklet.min.js') )
.pipe( gulp.dest('./dist') )
.pipe( gzip() )
.pipe( rename('gibberish_worklet.min.js.gz') )
.pipe( gulp.dest('./dist') )
}*/
//gulp.task( 'js', gulp.series( jsFunc ) )
//gulp.task( 'workletblob', gulp.series('js'), workletFnc )
exports.default = series( jsFunc, workletFnc )
exports.worklet = workletFnc
exports.js = jsFunc
//gulp.task( 'clean', ()=> {
// return gulp.src( './dist/*.js*', { read:false })
// .pipe( clean() )
//})
//gulp.task( 'minifyLib', [], minifyLib )
//gulp.task( 'minifyWorklet', [], minifyWorklet )
//gulp.task( 'minify', ['minifyLib', 'minifyWorklet'] )
//gulp.task( 'watch', function() {
// return gulp.watch( './js/**/*.js', ['workletblob'] )
// [>.pipe(
// notify({
// message:'Gibberish build completed.',
// onLast:true
// })
// )*/
//})
const gibberFunc = ()=> {
const gibberishText = fs.readFileSync( './dist/gibberish.js', 'utf-8' )
const gibberishWorklet= fs.readFileSync( './dist/gibberish_worklet.js', 'utf-8' )
fs.writeFileSync( '/Users/charlie/Documents/code/gibber.audio.lib/dist/gibberish.js', gibberishText )
fs.writeFileSync( '/Users/charlie/Documents/code/gibber.audio.lib/dist/gibberish_worklet.js', gibberishWorklet )
}
//gulp.task( 'gibber', gulp.series('workletblob'), gibberFunc )
//gulp.task( 'default', gulp.series('workletblob') )
const jsdsp = function({ types: t }) {
const operators = {
'+': 'add',
'-': 'sub',
'*': 'mul',
'/': 'div',
'^': 'pow',
'%': 'mod',
/* '**': 'pow',
'+=': 'add',
'*=': 'mul',
'-=': 'sub',
'/=': 'div',
'%=': 'mod',
'^=': 'pow',
/* '<': 'lt',
'<=': 'lte',
'>': 'gt',
'>=': 'gte',
'==': 'eq',
'===':'eq',
'!=': 'neq',
'!==':'neq',
'&&': 'and'*/
}
const innerVisitor = {
BinaryExpression( path, state ) {
//console.log( 'jsdsp:', state.usejsdsp )
if( state.usejsdsp === false ) return
// don't transform if arguments are both number literals
if( t.isNumericLiteral( path.node.left ) && t.isNumericLiteral( path.node.right ) ) return
// don't transform if no overload is found
if( !(path.node.operator in operators) ) return
const operatorString = operators[ path.node.operator ]
//console.log( 'replacing:', path.node.operator, operatorString )
path.replaceWith(
t.callExpression(
t.memberExpression(
t.identifier( 'genish' ),
t.identifier( operatorString )
),
[ path.node.left, path.node.right ]
)
)
},
AssignmentExpression( path, state ) {
if( state.usejsdsp === false ) return
// don't transform if arguments are both number literals
if( t.isNumericLiteral( path.node.left ) && t.isNumericLiteral( path.node.right ) ) return
// don't transform if no overload is found
if( !(path.node.operator in operators) ) return
if( path.node.operator.length < 2 ) return
const operatorString = operators[ path.node.operator ]
path.replaceWith(
t.assignmentExpression(
'=',
path.node.left,
t.callExpression(
t.memberExpression(
t.identifier( 'genish' ),
t.identifier( operatorString )
),
[ path.node.left, path.node.right ]
)
)
)
},
ExpressionStatement( path, state ) {
if( path.node.expression.value === 'use jsdsp' ) {
state.usejsdsp = true
//path.traverse( innerVisitor, state )
}
//state.usejsdsp = false
//path.skip()
},
BlockStatement( path, state ) {
if( path.node.directives !== undefined ) {
path.node.directives.forEach( directive => {
if( directive.value.value === 'use jsdsp' ) {
state.usejsdsp = true
}else if( directive.value.value === 'no jsdsp' ) {
state.usejsdsp = false
}
})
}
path.traverse( innerVisitor, state )
state.usejsdsp = state.usejsdsp === true ? true : false
//path.skip()
}
}
return {
visitor: {
BlockStatement( path, state ) {
// off by default
state.usejsdsp = false
if( path.node.directives !== undefined ) {
path.node.directives.forEach( directive => {
if( directive.value.value === 'use jsdsp' ) {
state.usejsdsp = true
}else if( directive.value.value === 'no jsdsp' ) {
state.usejsdsp = false
}
})
}
path.traverse( innerVisitor, state )
path.skip()
state.usejsdp = false
},
Function( path, state ) {
state.usejsdsp = false
path.traverse( innerVisitor, state )
state.usejsdsp = false
path.skip()
},
}
}
}