-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebWorkerPlugin.js
76 lines (63 loc) · 1.94 KB
/
WebWorkerPlugin.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
var fsbx = require('fuse-box')
class WebWorkerPlugin {
constructor(options) {
this.test = /(.*)/
this.webworkerPaths = []
console.log('added test prop')
}
// https://github.com/bjyoungblood/webworker-loader
init(context) {
if (this.debug)
console.debug('initted')
this.context = context
}
loadIfNeeded(file) {
file.loadContents()
file.analysis.parseUsingAcorn()
file.analysis.analyze()
}
transform(file) {
var path = file.contents.match(/\s+require\('([^']+)'\);?/)
const has = this.webworkerPaths.filter((p) => {
const ps = (p.includes('/') ? p.split('/') : [null, p]).pop()
return file.info.absPath.includes(ps)
}).length
var webworkerPath = path ? path[1] : false
if (webworkerPath && !this.webworkerPaths.includes(webworkerPath))
return this.webworkerPaths.push(webworkerPath)
if (!has) return
// file.info.fuseBoxPath
console.log(file.info.absPath, this.webworkerPaths)
console.log('..........')
// load & do parsing if it hasn't been done yet
// if babel has been used, it will have been
this.loadIfNeeded(file)
var webworkerOut = resolve('./src/webworker/dist/code.webworker.js')
// flush / reset dependencies
// so it will not include this file in the bundle
file.analysis.dependencies = []
// rewrite this file to point to the actual output chunk
file.contents = `module.exports = new WebWorker(${webworkerOut})`
fsbx.FuseBox
.init({
debug: true,
cache: false,
homeDir: resolve('./'),
outFile: webworkerOut,
})
.bundle(`>${file.info.fuseBoxPath}`)
}
}
var worker = new WebWorkerPlugin()
worker.test = /(.*)/
var params = {
debug: true,
cache: false,
homeDir: resolve('./'),
outFile: resolve('./src/webworker/dist/ww.js'),
plugins: [
worker,
],
}
var fuse = new fsbx.FuseBox(params)
fuse.bundle('>/src/webworker/index.js src/**/webworker/*.js')