forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstencil-worker.ts
More file actions
42 lines (34 loc) · 1.02 KB
/
stencil-worker.ts
File metadata and controls
42 lines (34 loc) · 1.02 KB
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
const ctx = self as any;
let loadedStencilVersion: string;
const loadStencilVersion = (version: string) => {
if (version !== loadedStencilVersion) {
loadedStencilVersion = version;
ctx.importScripts(
`https://unpkg.com/@stencil/core@${version}/compiler/stencil.js`
);
}
};
ctx.importScripts('https://unpkg.com/typescript@3.5.3/lib/typescript.js');
ctx.postMessage('ready');
ctx.addEventListener('message', event => {
const { code, path, stencilVersion } = event.data;
loadStencilVersion(stencilVersion);
const opts = {
file: path,
module: 'cjs',
};
ctx.stencil.compile(code, opts).then(results => {
results.imports.forEach(dependency => {
ctx.postMessage({
type: 'add-dependency',
path: dependency.path,
isGlob: false,
});
});
ctx.postMessage({
type: 'result',
// Code won't execute with an import.meta in the code, so removing it now as a hack
transpiledCode: results.code.replace(/^.*import\.meta.*$/gm, ''),
});
});
});