forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
65 lines (50 loc) · 1.62 KB
/
index.ts
File metadata and controls
65 lines (50 loc) · 1.62 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// @ts-ignore
import StencilWorker from 'worker-loader?publicPath=/&name=stencil-transpiler.[hash:8].worker.js!./stencil-worker.ts';
import WorkerTranspiler from '../worker-transpiler';
import { LoaderContext } from '../../transpiled-module';
import { TranspilerResult } from '..';
const DEFAULT_STENCIL_VERSION = '1.2.0-1';
const getStencilVersion = (packageJSON: any) => {
if (!packageJSON || !packageJSON.parsed) {
return DEFAULT_STENCIL_VERSION;
}
const testVersion = (parsed, keyToCheck): string | undefined =>
parsed[keyToCheck] && parsed[keyToCheck]['@stencil/core'];
return (
testVersion(packageJSON.parsed, 'dependencies') ||
testVersion(packageJSON.parsed, 'devDependencies') ||
DEFAULT_STENCIL_VERSION
);
};
class StencilTranspiler extends WorkerTranspiler {
worker: Worker;
constructor() {
super('stencil-loader', StencilWorker, 2);
}
doTranspilation(code: string, loaderContext: LoaderContext) {
return new Promise<TranspilerResult>((resolve, reject) => {
const path = loaderContext.path;
const packageJSON = loaderContext.options.configurations.package;
const stencilVersion = getStencilVersion(packageJSON);
this.queueTask(
{
code,
path,
stencilVersion,
},
loaderContext._module.getId(),
loaderContext,
(err, data) => {
if (err) {
loaderContext.emitError(err);
return reject(err);
}
return resolve(data);
}
);
});
}
}
const transpiler = new StencilTranspiler();
export { StencilTranspiler };
export default transpiler;