1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const path_1 = __importDefault(require("path"));
|
7 | const typescript_1 = __importDefault(require("typescript"));
|
8 | const types_1 = require("./types");
|
9 | const visitor_1 = require("./visitor");
|
10 | const harmony_1 = require("./harmony");
|
11 | const minimatch_1 = require("minimatch");
|
12 | const ts_helpers_1 = require("./utils/ts-helpers");
|
13 |
|
14 |
|
15 |
|
16 | function getTsProperties(args) {
|
17 | var _a, _b;
|
18 | let tsInstance;
|
19 | let fileNames;
|
20 | let compilerOptions;
|
21 | let runMode;
|
22 | let tsNodeState;
|
23 | const { 0: program, 2: extras, 3: manualTransformOptions } = args;
|
24 | tsInstance = (_a = extras === null || extras === void 0 ? void 0 : extras.ts) !== null && _a !== void 0 ? _a : typescript_1.default;
|
25 | if (program)
|
26 | compilerOptions = program.getCompilerOptions();
|
27 | const tsNodeProps = (0, ts_helpers_1.getTsNodeRegistrationProperties)(tsInstance);
|
28 |
|
29 |
|
30 | const isTsNode = tsNodeProps && (!program || compilerOptions.configFilePath === tsNodeProps.compilerOptions.configFilePath);
|
31 |
|
32 | if (program && !isTsNode) {
|
33 | runMode = types_1.RunMode.Program;
|
34 | compilerOptions = compilerOptions;
|
35 | }
|
36 |
|
37 | else if (manualTransformOptions) {
|
38 | runMode = types_1.RunMode.Manual;
|
39 | fileNames = manualTransformOptions.fileNames;
|
40 | compilerOptions = manualTransformOptions.compilerOptions;
|
41 | }
|
42 |
|
43 | else if (isTsNode) {
|
44 | fileNames = tsNodeProps.fileNames;
|
45 | runMode = types_1.RunMode.TsNode;
|
46 | tsNodeState =
|
47 | !program ||
|
48 | (fileNames.length > 1 && (program === null || program === void 0 ? void 0 : program.getRootFileNames().length) === 1) ||
|
49 | (!compilerOptions.paths && tsNodeProps.compilerOptions.paths)
|
50 | ? types_1.TsNodeState.Stripped
|
51 | : types_1.TsNodeState.Full;
|
52 | compilerOptions =
|
53 | tsNodeState === types_1.TsNodeState.Full
|
54 | ? compilerOptions
|
55 | : Object.assign(Object.assign({}, ((_b = program === null || program === void 0 ? void 0 : program.getCompilerOptions()) !== null && _b !== void 0 ? _b : {})), tsNodeProps.compilerOptions);
|
56 | }
|
57 | else {
|
58 | throw new Error(`Cannot transform without a Program, ts-node instance, or manual parameters supplied. ` +
|
59 | `Make sure you're using ts-patch or ts-node with transpileOnly.`);
|
60 | }
|
61 | return { tsInstance, compilerOptions, fileNames, runMode, tsNodeState };
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function transformer(program, pluginConfig, transformerExtras,
|
68 | /**
|
69 | * Supply if manually transforming with compiler API via 'transformNodes' / 'transformModule'
|
70 | */
|
71 | manualTransformOptions) {
|
72 | return (transformationContext) => {
|
73 | var _a, _b, _c;
|
74 |
|
75 | const { tsInstance, compilerOptions, fileNames, runMode, tsNodeState } = getTsProperties([program, pluginConfig, transformerExtras, manualTransformOptions]);
|
76 | const rootDirs = (_a = compilerOptions.rootDirs) === null || _a === void 0 ? void 0 : _a.filter(path_1.default.isAbsolute);
|
77 | const config = pluginConfig !== null && pluginConfig !== void 0 ? pluginConfig : {};
|
78 | const getCanonicalFileName = tsInstance.createGetCanonicalFileName(tsInstance.sys.useCaseSensitiveFileNames);
|
79 |
|
80 | let emitHost = transformationContext.getEmitHost();
|
81 | if (!emitHost || tsNodeState === types_1.TsNodeState.Stripped) {
|
82 | if (!fileNames)
|
83 | throw new Error(`No EmitHost found and could not determine files to be processed. Please file an issue with a reproduction!`);
|
84 | emitHost = (0, ts_helpers_1.createSyntheticEmitHost)(compilerOptions, tsInstance, getCanonicalFileName, fileNames);
|
85 | }
|
86 |
|
87 | const { configFile, paths } = compilerOptions;
|
88 | const { tryParsePatterns } = tsInstance;
|
89 | const [tsVersionMajor, tsVersionMinor] = tsInstance.versionMajorMinor.split(".").map((v) => +v);
|
90 | const tsTransformPathsContext = {
|
91 | compilerOptions,
|
92 | config,
|
93 | elisionMap: new Map(),
|
94 | tsFactory: transformationContext.factory,
|
95 | program,
|
96 | rootDirs,
|
97 | transformationContext,
|
98 | tsInstance,
|
99 | tsVersionMajor,
|
100 | tsVersionMinor,
|
101 | emitHost,
|
102 | runMode,
|
103 | tsNodeState,
|
104 | excludeMatchers: (_b = config.exclude) === null || _b === void 0 ? void 0 : _b.map((globPattern) => new minimatch_1.Minimatch(globPattern, { matchBase: true })),
|
105 | outputFileNamesCache: new Map(),
|
106 |
|
107 | pathsPatterns: paths &&
|
108 | (tryParsePatterns
|
109 | ? ((_c = configFile === null || configFile === void 0 ? void 0 : configFile.configFileSpecs) === null || _c === void 0 ? void 0 : _c.pathPatterns) || tryParsePatterns(paths)
|
110 | : tsInstance.getOwnKeys(paths)),
|
111 | };
|
112 | return (sourceFile) => {
|
113 | const visitorContext = Object.assign(Object.assign({}, tsTransformPathsContext), { sourceFile, isDeclarationFile: sourceFile.isDeclarationFile, originalSourceFile: tsInstance.getOriginalSourceFile(sourceFile), getVisitor() {
|
114 | return visitor_1.nodeVisitor.bind(this);
|
115 | }, factory: (0, harmony_1.createHarmonyFactory)(tsTransformPathsContext) });
|
116 | return tsInstance.visitEachChild(sourceFile, visitorContext.getVisitor(), transformationContext);
|
117 | };
|
118 | };
|
119 | }
|
120 | exports.default = transformer;
|
121 |
|
122 |
|
\ | No newline at end of file |