tsc-path-fix
Version:
Zero-runtime TypeScript path resolver - converts aliases to relative paths at compile time. Fast, lightweight, with native watch mode.
129 lines • 6.51 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceTscAliasPaths = replaceTscAliasPaths;
exports.prepareSingleFileReplaceTscAliasPaths = prepareSingleFileReplaceTscAliasPaths;
const chokidar_1 = require("chokidar");
const globby_1 = require("globby");
const plimit_lit_1 = require("plimit-lit");
const os_1 = require("os");
const helpers_1 = require("./helpers");
const progress_bar_1 = require("./utils/progress-bar");
const import_path_resolver_1 = require("./utils/import-path-resolver");
const trie_1 = require("./utils/trie");
const defaultConfig = {
watch: false,
verbose: false,
debug: false,
declarationDir: undefined,
output: undefined,
aliasTrie: undefined,
showProgress: true
};
const cpuCount = (0, os_1.cpus)().length;
const OptimalConcurrency = Math.max(cpuCount * 3, 10);
const OpenFilesLimit = (0, plimit_lit_1.pLimit)(OptimalConcurrency);
const BATCH_SIZE = 500;
function processFilesInBatches(files, config, progressBar, options) {
return __awaiter(this, void 0, void 0, function* () {
let processedCount = 0;
let modifiedCount = 0;
for (let i = 0; i < files.length; i += BATCH_SIZE) {
const batch = files.slice(i, i + BATCH_SIZE);
config.output.debug(`Processing batch ${Math.floor(i / BATCH_SIZE) + 1}/${Math.ceil(files.length / BATCH_SIZE)}`);
const batchResults = yield Promise.all(batch.map((file) => OpenFilesLimit(() => __awaiter(this, void 0, void 0, function* () {
const result = yield (0, helpers_1.replaceAlias)(config, file, options === null || options === void 0 ? void 0 : options.resolveFullPaths, options === null || options === void 0 ? void 0 : options.resolveFullExtension);
if (progressBar) {
processedCount++;
progressBar.update(processedCount);
}
return result;
}))));
modifiedCount += batchResults.filter(Boolean).length;
if (global.gc && typeof global.gc === 'function') {
global.gc();
}
}
return modifiedCount;
});
}
function replaceTscAliasPaths() {
return __awaiter(this, arguments, void 0, function* (options = Object.assign({}, defaultConfig)) {
const config = yield (0, helpers_1.prepareConfig)(options);
const output = config.output;
const showProgress = options.showProgress !== undefined ? options.showProgress : config.showProgress;
const posixOutput = config.outPath.replace(/\\/g, '/').replace(/\/+$/g, '');
const globPattern = [
`${posixOutput}/**/*.${config.inputGlob}`,
`!${posixOutput}/**/node_modules/**`,
`!${posixOutput}/**/.git/**`,
`!${posixOutput}/**/dist/**`,
`!${posixOutput}/**/build/**`
];
output.debug('Search pattern:', globPattern);
const files = (0, globby_1.globbySync)(globPattern, {
dot: true,
onlyFiles: true,
followSymbolicLinks: false,
concurrency: OptimalConcurrency
});
output.debug('Found files:', files);
let progressBar = null;
if (showProgress && files.length > 0 && !options.watch) {
progressBar = new progress_bar_1.ProgressBar(files.length, {
showElapsed: true,
showCount: true,
width: 40,
});
progressBar.start();
}
const replaceCount = yield processFilesInBatches(files, config, progressBar, options);
if (progressBar) {
progressBar.complete(`Processed ${files.length} files, ${replaceCount} were updated`);
}
output.info(`${replaceCount} files were affected!`);
if (options.watch) {
output.verbose = true;
output.info('[Watching for file changes...]');
const filesWatcher = (0, chokidar_1.watch)(globPattern);
const tsconfigWatcher = (0, chokidar_1.watch)(config.configFile);
const onFileChange = (file) => __awaiter(this, void 0, void 0, function* () {
(0, import_path_resolver_1.clearPathResolutionCache)();
(0, helpers_1.clearFileContentCache)();
(0, trie_1.clearTrieCache)();
yield (0, helpers_1.replaceAlias)(config, file, options === null || options === void 0 ? void 0 : options.resolveFullPaths);
});
filesWatcher.on('add', onFileChange);
filesWatcher.on('change', onFileChange);
tsconfigWatcher.on('change', () => {
output.clear();
filesWatcher.close();
tsconfigWatcher.close();
(0, import_path_resolver_1.clearPathResolutionCache)();
(0, helpers_1.clearFileContentCache)();
(0, trie_1.clearTrieCache)();
replaceTscAliasPaths(options);
});
}
if (options.declarationDir) {
replaceTscAliasPaths(Object.assign(Object.assign({}, options), { outDir: options.declarationDir, declarationDir: undefined, output: config.output, aliasTrie: undefined }));
}
});
}
function prepareSingleFileReplaceTscAliasPaths() {
return __awaiter(this, arguments, void 0, function* (options = Object.assign({}, defaultConfig)) {
const config = yield (0, helpers_1.prepareConfig)(options);
return ({ fileContents, filePath }) => {
return (0, helpers_1.replaceAliasString)(config, filePath, fileContents, options === null || options === void 0 ? void 0 : options.resolveFullPaths, options === null || options === void 0 ? void 0 : options.resolveFullExtension);
};
});
}
//# sourceMappingURL=index.js.map
;