1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
10 | if (k2 === undefined) k2 = k;
|
---|
11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
12 | }) : (function(o, m, k, k2) {
|
---|
13 | if (k2 === undefined) k2 = k;
|
---|
14 | o[k2] = m[k];
|
---|
15 | }));
|
---|
16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
18 | }) : function(o, v) {
|
---|
19 | o["default"] = v;
|
---|
20 | });
|
---|
21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
22 | if (mod && mod.__esModule) return mod;
|
---|
23 | var result = {};
|
---|
24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
25 | __setModuleDefault(result, mod);
|
---|
26 | return result;
|
---|
27 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.transformTypescript = exports.createTypescriptContext = void 0;
|
---|
30 | const path_1 = require("path");
|
---|
31 | const ts = __importStar(require("typescript"));
|
---|
32 | // Test transform helpers.
|
---|
33 | const basefileName = 'test-file.ts';
|
---|
34 | function createTypescriptContext(content, additionalFiles, useLibs = false, extraCompilerOptions = {}, jsxFile = false) {
|
---|
35 | const fileName = basefileName + (jsxFile ? 'x' : '');
|
---|
36 | // Set compiler options.
|
---|
37 | const compilerOptions = {
|
---|
38 | noEmitOnError: useLibs,
|
---|
39 | allowJs: true,
|
---|
40 | newLine: ts.NewLineKind.LineFeed,
|
---|
41 | moduleResolution: ts.ModuleResolutionKind.NodeJs,
|
---|
42 | module: ts.ModuleKind.ES2020,
|
---|
43 | target: ts.ScriptTarget.ES2020,
|
---|
44 | skipLibCheck: true,
|
---|
45 | sourceMap: false,
|
---|
46 | importHelpers: true,
|
---|
47 | experimentalDecorators: true,
|
---|
48 | types: [],
|
---|
49 | ...extraCompilerOptions,
|
---|
50 | };
|
---|
51 | // Create compiler host.
|
---|
52 | const compilerHost = ts.createCompilerHost(compilerOptions, true);
|
---|
53 | const baseFileExists = compilerHost.fileExists;
|
---|
54 | compilerHost.fileExists = function (compilerFileName) {
|
---|
55 | return (compilerFileName === fileName ||
|
---|
56 | !!(additionalFiles === null || additionalFiles === void 0 ? void 0 : additionalFiles[path_1.basename(compilerFileName)]) ||
|
---|
57 | baseFileExists(compilerFileName));
|
---|
58 | };
|
---|
59 | const baseReadFile = compilerHost.readFile;
|
---|
60 | compilerHost.readFile = function (compilerFileName) {
|
---|
61 | if (compilerFileName === fileName) {
|
---|
62 | return content;
|
---|
63 | }
|
---|
64 | else if (additionalFiles === null || additionalFiles === void 0 ? void 0 : additionalFiles[path_1.basename(compilerFileName)]) {
|
---|
65 | return additionalFiles[path_1.basename(compilerFileName)];
|
---|
66 | }
|
---|
67 | else {
|
---|
68 | return baseReadFile(compilerFileName);
|
---|
69 | }
|
---|
70 | };
|
---|
71 | // Create the TypeScript program.
|
---|
72 | const program = ts.createProgram([fileName], compilerOptions, compilerHost);
|
---|
73 | return { compilerHost, program };
|
---|
74 | }
|
---|
75 | exports.createTypescriptContext = createTypescriptContext;
|
---|
76 | function transformTypescript(content, transformers, program, compilerHost) {
|
---|
77 | // Use given context or create a new one.
|
---|
78 | if (content !== undefined) {
|
---|
79 | const typescriptContext = createTypescriptContext(content);
|
---|
80 | if (!program) {
|
---|
81 | program = typescriptContext.program;
|
---|
82 | }
|
---|
83 | if (!compilerHost) {
|
---|
84 | compilerHost = typescriptContext.compilerHost;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | else if (!program || !compilerHost) {
|
---|
88 | throw new Error('transformTypescript needs either `content` or a `program` and `compilerHost');
|
---|
89 | }
|
---|
90 | const outputFileName = basefileName.replace(/\.tsx?$/, '.js');
|
---|
91 | let outputContent;
|
---|
92 | // Emit.
|
---|
93 | const { emitSkipped, diagnostics } = program.emit(undefined, (filename, data) => {
|
---|
94 | if (filename === outputFileName) {
|
---|
95 | outputContent = data;
|
---|
96 | }
|
---|
97 | }, undefined, undefined, { before: transformers });
|
---|
98 | // Throw error with diagnostics if emit wasn't successfull.
|
---|
99 | if (emitSkipped) {
|
---|
100 | throw new Error(ts.formatDiagnostics(diagnostics, compilerHost));
|
---|
101 | }
|
---|
102 | // Return the transpiled js.
|
---|
103 | return outputContent;
|
---|
104 | }
|
---|
105 | exports.transformTypescript = transformTypescript;
|
---|