UNPKG

4.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createTsTranspileModule = void 0;
4/** @internal */
5function 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 // mix in default options
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 // transpileModule does not write anything to disk so there is no need to verify that there are no conflicts between input and output paths.
22 options.suppressOutputPathCheck = true;
23 // Filename can be non-ts file.
24 options.allowNonTsExtensions = true;
25 const newLine = getNewLineCharacter(options);
26 // Create a compilerHost object to allow the compiler to read and write files
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 // if jsx is specified then treat file as .tsx
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 /*cache*/ 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 // Output
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 /*to*/ diagnostics,
85 /*from*/ program.getSyntacticDiagnostics(sourceFile));
86 addRange(/*to*/ diagnostics, /*from*/ program.getOptionsDiagnostics());
87 }
88 // Emit
89 program.emit(
90 /*targetSourceFile*/ undefined,
91 /*writeFile*/ undefined,
92 /*cancellationToken*/ undefined,
93 /*emitOnlyDtsFiles*/ undefined, transpileOptions.transformers);
94 if (outputText === undefined)
95 return Debug.fail('Output generation failed');
96 return { outputText, diagnostics, sourceMapText };
97 }
98}
99exports.createTsTranspileModule = createTsTranspileModule;
100//# sourceMappingURL=ts-transpile-module.js.map
\No newline at end of file