/
transpile-module.ts
129 lines (108 loc) · 4.7 KB
/
transpile-module.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
import type * as d from '../../declarations';
import { BuildContext } from '../build/build-ctx';
import { CompilerContext } from '../build/compiler-ctx';
import { convertDecoratorsToStatic } from '../transformers/decorators-to-static/convert-decorators';
import { convertStaticToMeta } from '../transformers/static-to-meta/visitor';
import { createLogger } from '../sys/logger/console-logger';
import { getCurrentDirectory } from '../sys/environment';
import { isNumber, isString, loadTypeScriptDiagnostics, normalizePath } from '@utils';
import { lazyComponentTransform } from '../transformers/component-lazy/transform-lazy-component';
import { nativeComponentTransform } from '../transformers/component-native/tranform-to-native-component';
import { updateStencilCoreImports } from '../transformers/update-stencil-core-import';
import ts from 'typescript';
/**
* Stand-alone compiling of a single string
*/
export const transpileModule = (config: d.Config, input: string, transformOpts: d.TransformOptions) => {
if (!config.logger) {
config = {
...config,
logger: createLogger(),
};
}
const compilerCtx = new CompilerContext();
const buildCtx = new BuildContext(config, compilerCtx);
const tsCompilerOptions: ts.CompilerOptions = {
...config.tsCompilerOptions,
};
let sourceFilePath = transformOpts.file;
if (isString(sourceFilePath)) {
sourceFilePath = normalizePath(sourceFilePath);
} else {
sourceFilePath = tsCompilerOptions.jsx ? `module.tsx` : `module.ts`;
}
const results: d.TranspileModuleResults = {
sourceFilePath: sourceFilePath,
code: null,
map: null,
diagnostics: [],
moduleFile: null,
};
if (transformOpts.module === 'cjs') {
tsCompilerOptions.module = ts.ModuleKind.CommonJS;
} else {
tsCompilerOptions.module = ts.ModuleKind.ESNext;
}
tsCompilerOptions.target = getScriptTargetKind(transformOpts);
if ((sourceFilePath.endsWith('.tsx') || sourceFilePath.endsWith('.jsx')) && tsCompilerOptions.jsx == null) {
// ensure we're setup for JSX in typescript
tsCompilerOptions.jsx = ts.JsxEmit.React;
}
if (tsCompilerOptions.jsx != null && !isString(tsCompilerOptions.jsxFactory)) {
tsCompilerOptions.jsxFactory = 'h';
}
if (tsCompilerOptions.paths && !isString(tsCompilerOptions.baseUrl)) {
tsCompilerOptions.baseUrl = '.';
}
const sourceFile = ts.createSourceFile(sourceFilePath, input, tsCompilerOptions.target);
// Create a compilerHost object to allow the compiler to read and write files
const compilerHost: ts.CompilerHost = {
getSourceFile: fileName => {
return normalizePath(fileName) === normalizePath(sourceFilePath) ? sourceFile : undefined;
},
writeFile: (name, text) => {
if (name.endsWith('.js.map')) {
results.map = text;
} else if (name.endsWith('.js')) {
results.code = text;
}
},
getDefaultLibFileName: () => `lib.d.ts`,
useCaseSensitiveFileNames: () => false,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => transformOpts.currentDirectory || getCurrentDirectory(),
getNewLine: () => ts.sys.newLine || '\n',
fileExists: fileName => normalizePath(fileName) === normalizePath(sourceFilePath),
readFile: () => '',
directoryExists: () => true,
getDirectories: () => [],
};
const program = ts.createProgram([sourceFilePath], tsCompilerOptions, compilerHost);
const typeChecker = program.getTypeChecker();
const after: ts.TransformerFactory<ts.SourceFile>[] = [convertStaticToMeta(config, compilerCtx, buildCtx, typeChecker, null, transformOpts)];
if (transformOpts.componentExport === 'customelement' || transformOpts.componentExport === 'module') {
after.push(nativeComponentTransform(compilerCtx, transformOpts));
} else {
after.push(lazyComponentTransform(compilerCtx, transformOpts));
}
program.emit(undefined, undefined, undefined, false, {
before: [convertDecoratorsToStatic(config, buildCtx.diagnostics, typeChecker), updateStencilCoreImports(transformOpts.coreImportPath)],
after,
});
const tsDiagnostics = [...program.getSyntacticDiagnostics()];
if (config.validateTypes) {
tsDiagnostics.push(...program.getOptionsDiagnostics());
}
buildCtx.diagnostics.push(...loadTypeScriptDiagnostics(tsDiagnostics));
results.diagnostics.push(...buildCtx.diagnostics);
results.moduleFile = compilerCtx.moduleMap.get(results.sourceFilePath);
return results;
};
const getScriptTargetKind = (transformOpts: d.TransformOptions) => {
const target = transformOpts.target && transformOpts.target.toUpperCase();
if (isNumber((ts.ScriptTarget as any)[target])) {
return (ts.ScriptTarget as any)[target];
}
// ESNext and Latest are the same
return ts.ScriptTarget.Latest;
};