/
transpile.ts
155 lines (139 loc) · 5.03 KB
/
transpile.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
import type * as d from '@stencil/core/declarations';
import { convertDecoratorsToStatic } from '../decorators-to-static/convert-decorators';
import { convertStaticToMeta } from '../static-to-meta/visitor';
import { mockBuildCtx, mockCompilerCtx, mockConfig, mockStencilSystem } from '@stencil/core/testing';
import ts from 'typescript';
import { updateModule } from '../static-to-meta/parse-static';
import { getScriptTarget } from '../transform-utils';
export function transpileModule(
input: string,
config?: d.Config,
compilerCtx?: d.CompilerCtx,
sys?: d.CompilerSystem,
beforeTransformers: ts.TransformerFactory<ts.SourceFile>[] = [],
afterTransformers: ts.TransformerFactory<ts.SourceFile>[] = [],
) {
const options = ts.getDefaultCompilerOptions();
options.isolatedModules = true;
options.suppressOutputPathCheck = true;
options.allowNonTsExtensions = true;
options.removeComments = false;
options.noLib = true;
options.lib = undefined;
options.types = undefined;
options.noEmit = undefined;
options.noEmitOnError = undefined;
options.noEmitHelpers = true;
options.paths = undefined;
options.rootDirs = undefined;
options.declaration = undefined;
options.composite = undefined;
options.declarationDir = undefined;
options.out = undefined;
options.outFile = undefined;
options.noResolve = true;
options.module = ts.ModuleKind.ESNext;
options.target = getScriptTarget();
options.experimentalDecorators = true;
options.jsx = ts.JsxEmit.React;
options.jsxFactory = 'h';
options.jsxFragmentFactory = 'Fragment';
const inputFileName = 'module.tsx';
const sourceFile = ts.createSourceFile(inputFileName, input, options.target);
let outputText: string;
const emitCallback: ts.WriteFileCallback = (emitFilePath, data, _w, _e, tsSourceFiles) => {
if (emitFilePath.endsWith('.js')) {
outputText = data;
updateModule(config, compilerCtx, buildCtx, tsSourceFiles[0], data, emitFilePath, tsTypeChecker, null);
}
};
const compilerHost: ts.CompilerHost = {
getSourceFile: fileName => (fileName === inputFileName ? sourceFile : undefined),
writeFile: emitCallback,
getDefaultLibFileName: () => 'lib.d.ts',
useCaseSensitiveFileNames: () => false,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => '',
getNewLine: () => '',
fileExists: fileName => fileName === inputFileName,
readFile: () => '',
directoryExists: () => true,
getDirectories: () => [],
};
const tsProgram = ts.createProgram([inputFileName], options, compilerHost);
const tsTypeChecker = tsProgram.getTypeChecker();
config = config || mockConfig();
compilerCtx = compilerCtx || mockCompilerCtx(config);
sys = sys || config.sys || (mockStencilSystem() as any);
const buildCtx = mockBuildCtx(config, compilerCtx);
const transformOpts: d.TransformOptions = {
coreImportPath: '@stencil/core',
componentExport: 'lazy',
componentMetadata: null,
currentDirectory: '/',
proxy: null,
style: 'static',
styleImportData: 'queryparams',
};
tsProgram.emit(undefined, undefined, undefined, undefined, {
before: [convertDecoratorsToStatic(config, buildCtx.diagnostics, tsTypeChecker), ...beforeTransformers],
after: [convertStaticToMeta(config, compilerCtx, buildCtx, tsTypeChecker, null, transformOpts), ...afterTransformers],
});
while (outputText.includes(' ')) {
outputText = outputText.replace(/ /g, ' ');
}
const moduleFile: d.Module = compilerCtx.moduleMap.values().next().value;
const cmps = moduleFile ? moduleFile.cmps : null;
const cmp = Array.isArray(cmps) && cmps.length > 0 ? cmps[0] : null;
const tagName = cmp ? cmp.tagName : null;
const componentClassName = cmp ? cmp.componentClassName : null;
const properties = cmp ? cmp.properties : null;
const virtualProperties = cmp ? cmp.virtualProperties : null;
const property = properties ? properties[0] : null;
const states = cmp ? cmp.states : null;
const state = states ? states[0] : null;
const listeners = cmp ? cmp.listeners : null;
const listener = listeners ? listeners[0] : null;
const events = cmp ? cmp.events : null;
const event = events ? events[0] : null;
const methods = cmp ? cmp.methods : null;
const method = methods ? methods[0] : null;
const elementRef = cmp ? cmp.elementRef : null;
const legacyConnect = cmp ? cmp.legacyConnect : null;
const legacyContext = cmp ? cmp.legacyContext : null;
return {
outputText,
compilerCtx,
buildCtx,
diagnostics: buildCtx.diagnostics,
moduleFile,
cmps,
cmp,
componentClassName,
tagName,
properties,
virtualProperties,
property,
states,
state,
listeners,
listener,
events,
event,
methods,
method,
elementRef,
legacyContext,
legacyConnect,
};
}
export function getStaticGetter(output: string, prop: string) {
const toEvaludate = `return ${output.replace('export', '')}`;
try {
const Obj = new Function(toEvaludate);
return Obj()[prop];
} catch (e) {
console.error(e);
console.error(toEvaludate);
}
}