1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createTsTranspileModule = void 0;
|
4 |
|
5 | function createTsTranspileModule(ts, transpileOptions) {
|
6 | const { createProgram, createSourceFile, getDefaultCompilerOptions, getImpliedNodeFormatForFile, fixupCompilerOptions, transpileOptionValueCompilerOptions, getNewLineCharacter, fileExtensionIs, normalizePath, Debug, toPath, getSetExternalModuleIndicator, getEntries, addRange, hasProperty, getEmitScriptTarget, getDirectoryPath, } = ts;
|
7 | const compilerOptionsDiagnostics = [];
|
8 | const options = transpileOptions.compilerOptions
|
9 | ? fixupCompilerOptions(transpileOptions.compilerOptions, compilerOptionsDiagnostics)
|
10 | : {};
|
11 |
|
12 | const defaultOptions = getDefaultCompilerOptions();
|
13 | for (const key in defaultOptions) {
|
14 | if (hasProperty(defaultOptions, key) && options[key] === undefined) {
|
15 | options[key] = defaultOptions[key];
|
16 | }
|
17 | }
|
18 | for (const option of transpileOptionValueCompilerOptions) {
|
19 | options[option.name] = option.transpileOptionValue;
|
20 | }
|
21 |
|
22 | options.suppressOutputPathCheck = true;
|
23 |
|
24 | options.allowNonTsExtensions = true;
|
25 | const newLine = getNewLineCharacter(options);
|
26 |
|
27 | const compilerHost = {
|
28 | getSourceFile: (fileName) => fileName === normalizePath(inputFileName) ? sourceFile : undefined,
|
29 | writeFile: (name, text) => {
|
30 | if (fileExtensionIs(name, '.map')) {
|
31 | Debug.assertEqual(sourceMapText, undefined, 'Unexpected multiple source map outputs, file:', name);
|
32 | sourceMapText = text;
|
33 | }
|
34 | else {
|
35 | Debug.assertEqual(outputText, undefined, 'Unexpected multiple outputs, file:', name);
|
36 | outputText = text;
|
37 | }
|
38 | },
|
39 | getDefaultLibFileName: () => 'lib.d.ts',
|
40 | useCaseSensitiveFileNames: () => true,
|
41 | getCanonicalFileName: (fileName) => fileName,
|
42 | getCurrentDirectory: () => '',
|
43 | getNewLine: () => newLine,
|
44 | fileExists: (fileName) => fileName === inputFileName || fileName === packageJsonFileName,
|
45 | readFile: (fileName) => fileName === packageJsonFileName ? `{"type": "${_packageJsonType}"}` : '',
|
46 | directoryExists: () => true,
|
47 | getDirectories: () => [],
|
48 | };
|
49 | let inputFileName;
|
50 | let packageJsonFileName;
|
51 | let _packageJsonType;
|
52 | let sourceFile;
|
53 | let outputText;
|
54 | let sourceMapText;
|
55 | return transpileModule;
|
56 | function transpileModule(input, transpileOptions2, packageJsonType = 'commonjs') {
|
57 |
|
58 | inputFileName =
|
59 | transpileOptions2.fileName ||
|
60 | (transpileOptions.compilerOptions && transpileOptions.compilerOptions.jsx
|
61 | ? 'module.tsx'
|
62 | : 'module.ts');
|
63 | packageJsonFileName = getDirectoryPath(inputFileName) + '/package.json';
|
64 | _packageJsonType = packageJsonType;
|
65 | sourceFile = createSourceFile(inputFileName, input, {
|
66 | languageVersion: getEmitScriptTarget(options),
|
67 | impliedNodeFormat: getImpliedNodeFormatForFile(toPath(inputFileName, '', compilerHost.getCanonicalFileName),
|
68 | undefined, compilerHost, options),
|
69 | setExternalModuleIndicator: getSetExternalModuleIndicator(options),
|
70 | });
|
71 | if (transpileOptions2.moduleName) {
|
72 | sourceFile.moduleName = transpileOptions2.moduleName;
|
73 | }
|
74 | if (transpileOptions2.renamedDependencies) {
|
75 | sourceFile.renamedDependencies = new Map(getEntries(transpileOptions2.renamedDependencies));
|
76 | }
|
77 |
|
78 | outputText = undefined;
|
79 | sourceMapText = undefined;
|
80 | const program = createProgram([inputFileName], options, compilerHost);
|
81 | const diagnostics = compilerOptionsDiagnostics.slice();
|
82 | if (transpileOptions.reportDiagnostics) {
|
83 | addRange(
|
84 | diagnostics,
|
85 | program.getSyntacticDiagnostics(sourceFile));
|
86 | addRange( diagnostics, program.getOptionsDiagnostics());
|
87 | }
|
88 |
|
89 | program.emit(
|
90 | undefined,
|
91 | undefined,
|
92 | undefined,
|
93 | undefined, transpileOptions.transformers);
|
94 | if (outputText === undefined)
|
95 | return Debug.fail('Output generation failed');
|
96 | return { outputText, diagnostics, sourceMapText };
|
97 | }
|
98 | }
|
99 | exports.createTsTranspileModule = createTsTranspileModule;
|
100 |
|
\ | No newline at end of file |