-
Notifications
You must be signed in to change notification settings - Fork 798
/
Copy pathworker-plugin.ts
459 lines (419 loc) · 12.6 KB
/
worker-plugin.ts
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import type * as d from '../../declarations';
import type { Plugin, TransformResult, PluginContext } from 'rollup';
import { bundleOutput } from './bundle-output';
import { normalizeFsPath, hasError, generatePreamble } from '@utils';
import { optimizeModule } from '../optimize/optimize-module';
import { STENCIL_INTERNAL_ID } from './entry-alias-ids';
export const workerPlugin = (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
platform: string,
inlineWorkers: boolean
): Plugin => {
if (platform === 'worker' || platform === 'hydrate') {
return {
name: 'workerPlugin',
transform(_, id) {
if (id.endsWith('?worker') || id.endsWith('?worker-inline')) {
return getMockedWorkerMain();
}
return null;
},
};
}
const workersMap = new Map<string, WorkerMeta>();
return {
name: 'workerPlugin',
buildStart() {
workersMap.clear();
},
resolveId(id) {
if (id === WORKER_HELPER_ID) {
return {
id,
moduleSideEffects: false,
};
}
return null;
},
load(id) {
if (id === WORKER_HELPER_ID) {
return WORKER_HELPERS;
}
return null;
},
async transform(_, id): Promise<TransformResult> {
if (/\0/.test(id)) {
return null;
}
// Canonical worker path
if (id.endsWith('?worker')) {
const workerEntryPath = normalizeFsPath(id);
const workerName = getWorkerName(workerEntryPath);
const { code, dependencies, workerMsgId } = await getWorker(
config,
compilerCtx,
buildCtx,
this,
workersMap,
workerEntryPath
);
const referenceId = this.emitFile({
type: 'asset',
source: code,
name: workerName + '.js',
});
dependencies.forEach((id) => this.addWatchFile(id));
return {
code: getWorkerMain(referenceId, workerName, workerMsgId),
moduleSideEffects: false,
};
} else if (id.endsWith('?worker-inline')) {
const workerEntryPath = normalizeFsPath(id);
const workerName = getWorkerName(workerEntryPath);
const { code, dependencies, workerMsgId } = await getWorker(
config,
compilerCtx,
buildCtx,
this,
workersMap,
workerEntryPath
);
const referenceId = this.emitFile({
type: 'asset',
source: code,
name: workerName + '.js',
});
dependencies.forEach((id) => this.addWatchFile(id));
return {
code: getInlineWorker(referenceId, workerName, workerMsgId),
moduleSideEffects: false,
};
}
// Proxy worker path
const workerEntryPath = getWorkerEntryPath(id);
if (workerEntryPath != null) {
const worker = await getWorker(config, compilerCtx, buildCtx, this, workersMap, workerEntryPath);
if (worker) {
if (inlineWorkers) {
return {
code: getInlineWorkerProxy(workerEntryPath, worker.workerMsgId, worker.exports),
moduleSideEffects: false,
};
} else {
return {
code: getWorkerProxy(workerEntryPath, worker.exports),
moduleSideEffects: false,
};
}
}
}
return null;
},
};
};
const getWorkerEntryPath = (id: string) => {
if (WORKER_SUFFIX.some((p) => id.endsWith(p))) {
return normalizeFsPath(id);
}
return null;
};
interface WorkerMeta {
code: string;
workerMsgId: string;
exports: string[];
dependencies: string[];
}
const getWorker = async (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
ctx: PluginContext,
workersMap: Map<string, WorkerMeta>,
workerEntryPath: string
): Promise<WorkerMeta> => {
let worker = workersMap.get(workerEntryPath);
if (!worker) {
worker = await buildWorker(config, compilerCtx, buildCtx, ctx, workerEntryPath);
workersMap.set(workerEntryPath, worker);
}
return worker;
};
const getWorkerName = (id: string) => {
const parts = id.split('/').filter((i) => !i.includes('index'));
id = parts[parts.length - 1];
return id.replace('.tsx', '').replace('.ts', '');
};
const buildWorker = async (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
ctx: PluginContext,
workerEntryPath: string
) => {
const workerName = getWorkerName(workerEntryPath);
const workerMsgId = `stencil.${workerName}`;
const build = await bundleOutput(config, compilerCtx, buildCtx, {
platform: 'worker',
id: workerName,
inputs: {
[workerName]: workerEntryPath,
},
inlineDynamicImports: true,
});
if (build) {
// Generate commonjs output so we can intercept exports at runtime
const output = await build.generate({
format: 'commonjs',
banner: `${generatePreamble(config)}\n(()=>{\n`,
footer: '})();',
intro: getWorkerIntro(workerMsgId, config.devMode),
esModule: false,
preferConst: true,
externalLiveBindings: false,
});
const entryPoint = output.output[0];
if (entryPoint.imports.length > 0) {
ctx.error('Workers should not have any external imports: ' + JSON.stringify(entryPoint.imports));
}
// Optimize code
let code = entryPoint.code;
const results = await optimizeModule(config, compilerCtx, {
input: code,
sourceTarget: config.buildEs5 ? 'es5' : 'es2017',
isCore: false,
minify: config.minifyJs,
inlineHelpers: true,
});
buildCtx.diagnostics.push(...results.diagnostics);
if (!hasError(results.diagnostics)) {
code = results.output;
}
return {
code,
exports: entryPoint.exports,
workerMsgId,
dependencies: Object.keys(entryPoint.modules).filter((id) => !/\0/.test(id) && id !== workerEntryPath),
};
}
return null;
};
const WORKER_SUFFIX = ['.worker.ts', '.worker.tsx', '.worker/index.ts', '.worker/index.tsx'];
const WORKER_HELPER_ID = '@worker-helper';
const GET_TRANSFERABLES = `
const isInstanceOf = (value, className) => {
const C = globalThis[className];
return C != null && value instanceof C;
}
const getTransferables = (value) => {
if (value != null) {
if (
isInstanceOf(value, "ArrayBuffer") ||
isInstanceOf(value, "MessagePort") ||
isInstanceOf(value, "ImageBitmap") ||
isInstanceOf(value, "OffscreenCanvas")
) {
return [value];
}
if (typeof value === "object") {
if (value.constructor === Object) {
value = Object.values(value);
}
if (Array.isArray(value)) {
return value.flatMap(getTransferables);
}
return getTransferables(value.buffer);
}
}
return [];
};`;
const getWorkerIntro = (workerMsgId: string, isDev: boolean) => `
${GET_TRANSFERABLES}
const exports = {};
const workerMsgId = '${workerMsgId}';
const workerMsgCallbackId = workerMsgId + '.cb';
addEventListener('message', async ({data}) => {
if (data && data[0] === workerMsgId) {
let id = data[1];
let method = data[2];
let args = data[3];
let i = 0;
let argsLen = args.length;
let value;
let err;
try {
for (; i < argsLen; i++) {
if (Array.isArray(args[i]) && args[i][0] === workerMsgCallbackId) {
const callbackId = args[i][1];
args[i] = (...cbArgs) => {
postMessage(
[workerMsgCallbackId, callbackId, cbArgs]
);
};
}
}
${
isDev
? `
value = exports[method](...args);
if (!value || !value.then) {
throw new Error('The exported method "' + method + '" does not return a Promise, make sure it is an "async" function');
}
value = await value;
`
: `
value = await exports[method](...args);`
}
} catch (e) {
value = null;
if (e instanceof Error) {
err = {
isError: true,
value: {
message: e.message,
name: e.name,
stack: e.stack,
}
};
} else {
err = {
isError: false,
value: e
};
}
value = undefined;
}
const transferables = getTransferables(value);
${isDev ? `if (transferables.length > 0) console.debug('Transfering', transferables);` : ''}
postMessage(
[workerMsgId, id, value, err],
transferables
);
}
});
`;
export const WORKER_HELPERS = `
import { consoleError } from '${STENCIL_INTERNAL_ID}';
${GET_TRANSFERABLES}
let pendingIds = 0;
let callbackIds = 0;
const pending = new Map();
const callbacks = new Map();
export const createWorker = (workerPath, workerName, workerMsgId) => {
const worker = new Worker(workerPath, {name:workerName});
worker.addEventListener('message', ({data}) => {
if (data) {
const workerMsg = data[0];
const id = data[1];
const value = data[2];
if (workerMsg === workerMsgId) {
const err = data[3];
const [resolve, reject, callbackIds] = pending.get(id);
pending.delete(id);
if (err) {
const errObj = (err.isError)
? Object.assign(new Error(err.value.message), err.value)
: err.value;
consoleError(errObj);
reject(errObj);
} else {
if (callbackIds) {
callbackIds.forEach(id => callbacks.delete(id));
}
resolve(value);
}
} else if (workerMsg === workerMsgId + '.cb') {
try {
callbacks.get(id)(...value);
} catch (e) {
consoleError(e);
}
}
}
});
return worker;
};
export const createWorkerProxy = (worker, workerMsgId, exportedMethod) => (
(...args) => new Promise((resolve, reject) => {
let pendingId = pendingIds++;
let i = 0;
let argLen = args.length;
let mainData = [resolve, reject];
pending.set(pendingId, mainData);
for (; i < argLen; i++) {
if (typeof args[i] === 'function') {
const callbackId = callbackIds++;
callbacks.set(callbackId, args[i]);
args[i] = [workerMsgId + '.cb', callbackId];
(mainData[2] = mainData[2] || []).push(callbackId);
}
}
const postMessage = (w) => (
w.postMessage(
[workerMsgId, pendingId, exportedMethod, args],
getTransferables(args)
)
);
if (worker.then) {
worker.then(postMessage);
} else {
postMessage(worker);
}
})
);
`;
const getWorkerMain = (referenceId: string, workerName: string, workerMsgId: string) => {
return `
import { createWorker } from '${WORKER_HELPER_ID}';
export const workerName = '${workerName}';
export const workerMsgId = '${workerMsgId}';
export const workerPath = /*@__PURE__*/import.meta.ROLLUP_FILE_URL_${referenceId};
export const worker = /*@__PURE__*/createWorker(workerPath, workerName, workerMsgId);
`;
};
const getInlineWorker = (referenceId: string, workerName: string, workerMsgId: string) => {
return `
import { createWorker } from '${WORKER_HELPER_ID}';
export const workerName = '${workerName}';
export const workerMsgId = '${workerMsgId}';
export const workerPath = /*@__PURE__*/import.meta.ROLLUP_FILE_URL_${referenceId};
const blob = new Blob(['importScripts("' + workerPath + '")'], { type: 'text/javascript' });
const url = URL.createObjectURL(blob);
export const worker = /*@__PURE__*/createWorker(url, workerName, workerMsgId);
URL.revokeObjectURL(url);
`;
};
const getMockedWorkerMain = () => {
// for the hydrate build the workers won't actually work
// however, we still need to make the {worker} export
// kick-in otherwise bundling chokes
return `
export const workerName = 'mocked-worker';
export const workerMsgId = workerName;
export const workerPath = workerName;
export const worker = { name: workerName };
`;
};
const getWorkerProxy = (workerEntryPath: string, exportedMethods: string[]) => {
return `
import { createWorkerProxy } from '${WORKER_HELPER_ID}';
import { worker, workerName, workerMsgId } from '${workerEntryPath}?worker';
${exportedMethods
.map((exportedMethod) => {
return `export const ${exportedMethod} = /*@__PURE__*/createWorkerProxy(worker, workerMsgId, '${exportedMethod}');`;
})
.join('\n')}
`;
};
const getInlineWorkerProxy = (workerEntryPath: string, workerMsgId: string, exportedMethods: string[]) => {
return `
import { createWorkerProxy } from '${WORKER_HELPER_ID}';
const workerPromise = import('${workerEntryPath}?worker-inline').then(m => m.worker);
${exportedMethods
.map((exportedMethod) => {
return `export const ${exportedMethod} = /*@__PURE__*/createWorkerProxy(workerPromise, '${workerMsgId}', '${exportedMethod}');`;
})
.join('\n')}
`;
};