-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker-inlinify.js
204 lines (204 loc) · 7.56 KB
/
worker-inlinify.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
"use strict";
var acorn = require("acorn");
var acornLoose = require("acorn/dist/acorn_loose");
var walk = require("acorn/dist/walk");
var path = require("path");
var fs = require("fs");
var text2jsvar = require("text2jsvar");
var RefPosition = /** @class */ (function () {
function RefPosition(start, end) {
this.start = start;
this.end = end;
}
return RefPosition;
}());
var WorkerRefs = /** @class */ (function () {
function WorkerRefs(worker, start, end) {
this.worker = worker;
this.refs = [];
this.addRef(start, end);
}
WorkerRefs.prototype.addRef = function (start, end) {
this.refs.push(new RefPosition(start, end));
};
return WorkerRefs;
}());
var ReplaceNode = /** @class */ (function () {
function ReplaceNode(start, end, workerRef) {
this.start = start;
this.end = end;
this.workerRef = workerRef;
}
return ReplaceNode;
}());
var EvalReplaceNode = /** @class */ (function () {
function EvalReplaceNode(start, end, replacement) {
this.start = start;
this.end = end;
this.replacement = text2jsvar.convert(replacement);
}
return EvalReplaceNode;
}());
var workerInlinify = {
_webpackAssets: null,
contextPath: path.resolve(process.cwd()),
useLoose: false,
findWorkerRefs: function (source) {
var ast = this.useLoose ? acornLoose.parse_dammit(source) : acorn.parse(source);
var arr = [];
var jsFileRegExp = /^([^\?]+\.js)(\?.*)?$/i;
var workerMap = {};
walk.simple(ast, {
Expression: function (node) {
if (node.type === 'NewExpression' && node.callee.name === 'Worker') {
if (node.arguments.length > 0) {
var argNode = node.arguments[0];
var leftLiteralNode = findLeftLiteralNode(argNode);
if (leftLiteralNode && typeof leftLiteralNode.value === 'string') {
var matchResult = jsFileRegExp.exec(leftLiteralNode.value);
if (matchResult) {
var worker = matchResult[1];
if (workerMap[worker] !== undefined) {
arr[workerMap[worker]].addRef(argNode.start, argNode.end);
}
else {
arr.push(new WorkerRefs(worker, argNode.start, argNode.end));
workerMap[worker] = arr.length - 1;
}
}
}
}
}
}
});
return arr;
},
inlinify: function (source) {
var _this = this;
// start inlinify contents in eval
var ast = this.useLoose ? acornLoose.parse_dammit(source) : acorn.parse(source);
var evalReplaceNodes = [];
var newSource = '';
walk.simple(ast, {
Expression: function (node) {
if (node.type === 'CallExpression' && node.callee.name === 'eval' && node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
var argument = node.arguments[0];
var result = workerInlinify.inlinify(argument.value);
if (result !== argument.value) {
evalReplaceNodes.push(new EvalReplaceNode(argument.start + 1, argument.end - 1, result));
}
}
}
});
// replace eval contents if needed
evalReplaceNodes.forEach(function (node, index) {
if (index === 0) {
newSource += source.substr(0, node.start);
}
else {
newSource += source.substring(evalReplaceNodes[index - 1].end, node.start);
}
newSource += node.replacement;
if (index === evalReplaceNodes.length - 1) {
newSource += source.substring(node.end);
}
});
if (evalReplaceNodes.length > 0) {
source = newSource;
}
// inlinify contents outside eval
var workerRefs = this.findWorkerRefs(source);
if (workerRefs.length === 0) {
return source;
}
newSource = '';
var replaceNodes = [];
workerRefs.forEach(function (workerRef) {
var worker = path.join(_this.contextPath, workerRef.worker);
if (workerInlinify._webpackAssets !== null) {
// find the worker script in webpack assets
if (workerRef.worker in workerInlinify._webpackAssets) {
workerRef.script = workerInlinify._webpackAssets[workerRef.worker].source();
}
}
else if (fs.existsSync(worker)) {
// find the resource in file system
workerRef.script = fs.readFileSync(worker).toString();
}
if (workerRef.script !== undefined) {
workerRef.varname = getWorkerVarName(source);
workerRef.refs.forEach(function (ref) {
replaceNodes.push(new ReplaceNode(ref.start, ref.end, workerRef));
});
}
});
if (replaceNodes.length === 0) {
return source;
}
replaceNodes.sort(function (a, b) {
if (a.start > b.start) {
return 1;
}
});
replaceNodes.forEach(function (node, index) {
if (index === 0) {
newSource += source.substr(0, node.start);
}
else {
newSource += source.substring(replaceNodes[index - 1].end, node.start);
}
newSource += 'window.URL.createObjectURL(';
if (node.workerRef.refs.length > 1) {
newSource += node.workerRef.varname;
}
else {
newSource += 'new Blob(["' + text2jsvar.convert(node.workerRef.script) + '"])';
}
newSource += ')';
if (index === replaceNodes.length - 1) {
newSource += source.substring(node.end);
}
});
workerRefs.forEach(function (workerRef) {
if (workerRef.script && workerRef.refs.length > 1) {
newSource = "var " + workerRef.varname + "=new Blob([\"" + text2jsvar.convert(workerRef.script) + "\"]);\r\n" + newSource;
}
});
return newSource;
},
inlinifyFile: function (file) {
var filePath = path.join(this.contextPath, file);
var extname = path.extname(filePath).toLowerCase();
if (extname === '.js' && fs.existsSync(filePath)) {
var source = fs.readFileSync(filePath).toString();
var inlinifyResult = this.inlinify(source);
if (source !== inlinifyResult) {
fs.writeFileSync(filePath, inlinifyResult);
}
}
}
};
function getWorkerVarName(source) {
var prefix = 'inlineWorker_';
var varname = prefix + getUniqueString();
while (source.indexOf(varname) > -1) {
varname = prefix + getUniqueString();
}
return varname;
function getUniqueString() {
return Math.random().toString(36).substring(7);
}
}
function findLeftLiteralNode(node) {
var lastNode = node;
while (lastNode.type === 'BinaryExpression') {
lastNode = lastNode.left;
}
if (lastNode.type === 'Literal') {
return lastNode;
}
else {
return null;
}
}
module.exports = workerInlinify;