UNPKG

gulp-simple-typescript

Version:
37 lines (36 loc) 1.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tsParser = void 0; const path_1 = require("path"); const typescript_1 = require("typescript"); const configManager_1 = require("./configManager"); const make_replacements_1 = __importDefault(require("replace-in-file/lib/helpers/make-replacements")); const stream_1 = require("stream"); var compilerOptions; function replaceString(file) { return (match) => { let replacePath = compilerOptions.paths[match][0]; replacePath = path_1.relative(path_1.dirname(file.path), path_1.resolve(file.cwd, replacePath)); const prefix = /\.|\//.test(replacePath.charAt(0)) ? '' : './'; return `${prefix}${replacePath}`; }; } function tsParser(file, tsConfig) { compilerOptions = configManager_1.getConfig(); const replaceOption = { from: Object.keys(compilerOptions.paths).map(alias => new RegExp(`(?<=(require *\\(|import *\\(|from *)['"])${alias}(?=['"/])`, 'g')), to: replaceString(file) }; const [, result] = make_replacements_1.default(file.contents.toString(), replaceOption.from, replaceOption.to, file.path, false); const { outputText: javascript } = typescript_1.transpileModule(result, { compilerOptions: tsConfig, reportDiagnostics: true }); file.contents = stream_1.Readable.from(javascript); file.extname = '.js'; return file; } exports.tsParser = tsParser;