@anatine/esbuild-decorators
Version:
Esbuild plugin for Nx to handle emitDecoratorMetadata
81 lines • 3.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.esbuildDecorators = void 0;
const tslib_1 = require("tslib");
const fs_1 = require("fs");
const path_1 = require("path");
const typescript_1 = require("typescript");
const util_1 = require("util");
const strip_it_1 = require("./strip-it");
const { readFile } = fs_1.promises;
const theFinder = new RegExp(/((?<![(\s]\s*['"])@\w[.[\]\w\d]*\s*(?![;])[((?=\s)])/);
const findDecorators = (fileContent) => theFinder.test((0, strip_it_1.strip)(fileContent));
const esbuildDecorators = (options = {}) => ({
name: 'tsc',
setup(build) {
var _a, _b, _c;
const cwd = options.cwd || process.cwd();
const tsconfigPath = options.tsconfig ||
((_a = build.initialOptions) === null || _a === void 0 ? void 0 : _a.tsconfig) ||
(0, path_1.join)(cwd, './tsconfig.json');
const forceTsc = (_b = options.force) !== null && _b !== void 0 ? _b : false;
const tsx = (_c = options.tsx) !== null && _c !== void 0 ? _c : true;
let parsedTsConfig = null;
build.onLoad({ filter: tsx ? /\.tsx?$/ : /\.ts$/ }, (args) => tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!parsedTsConfig) {
parsedTsConfig = parseTsConfig(tsconfigPath, cwd);
if (parsedTsConfig.options.sourcemap) {
parsedTsConfig.options.sourcemap = false;
parsedTsConfig.options.inlineSources = true;
parsedTsConfig.options.inlineSourceMap = true;
}
}
// Just return if we don't need to search the file.
if (!forceTsc &&
(!parsedTsConfig ||
!parsedTsConfig.options ||
!parsedTsConfig.options.emitDecoratorMetadata)) {
return;
}
const ts = yield readFile(args.path, 'utf8').catch((err) => printDiagnostics({ file: args.path, err }));
// Find the decorator and if there isn't one, return out
const hasDecorator = findDecorators(ts);
if (!ts || !hasDecorator) {
return;
}
const program = (0, typescript_1.transpileModule)(ts, {
compilerOptions: parsedTsConfig.options,
});
return { contents: program.outputText };
}));
},
});
exports.esbuildDecorators = esbuildDecorators;
function parseTsConfig(tsconfig, cwd = process.cwd()) {
const fileName = (0, typescript_1.findConfigFile)(cwd, typescript_1.sys.fileExists, tsconfig);
// if the value was provided, but no file, fail hard
if (tsconfig !== undefined && !fileName)
throw new Error(`failed to open '${fileName}'`);
let loadedConfig = {};
let baseDir = cwd;
if (fileName) {
const text = typescript_1.sys.readFile(fileName);
if (text === undefined)
throw new Error(`failed to read '${fileName}'`);
const result = (0, typescript_1.parseConfigFileTextToJson)(fileName, text);
if (result.error !== undefined) {
printDiagnostics(result.error);
throw new Error(`failed to parse '${fileName}'`);
}
loadedConfig = result.config;
baseDir = (0, path_1.dirname)(fileName);
}
const parsedTsConfig = (0, typescript_1.parseJsonConfigFileContent)(loadedConfig, typescript_1.sys, baseDir);
if (parsedTsConfig.errors[0])
printDiagnostics(parsedTsConfig.errors);
return parsedTsConfig;
}
function printDiagnostics(...args) {
console.log((0, util_1.inspect)(args, false, 10, true));
}
//# sourceMappingURL=esbuild-decorators.js.map