-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (94 loc) · 3.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import path from 'path';
import typescript from 'typescript';
import stripComments from 'strip-comments';
import { inspect } from 'util';
const tsRegex = /\.[mc]?ts$/;
const tsxRegex = /\.([mc]?tsx?)$/;
const decoratorsRegex = new RegExp(/((?<![\(\s]\s*['"])@\w*[\w\d]\s*(?![;])[\((?=\s)])/);
const findDecorators = (fileContent) => decoratorsRegex.test(stripComments(fileContent));
// Plugins for esbuild:
// 1. esbuild-plugin-ts-decorators
// https://github.com/andreisergiu98/esbuild-plugin-ts-decorators
// https://github.com/andreisergiu98/esbuild-plugin-ts-decorators/blob/main/src/plugin.ts
// 2. esbuild-plugin-typescript-decorators
// https://github.com/jcindex/esbuild-plugin-typescript-decorators
// https://github.com/jcindex/esbuild-plugin-typescript-decorators/blob/main/src/esbuild-plugin-typescript-decorators.ts
export default function emitDecoratorMetadata({
tsconfigPath = path.join(process.cwd(), './tsconfig.json'),
forceTsc = false,
tsx = true,
} = {}) {
let parsedTsConfig = null;
const fileRegex = tsx ? tsxRegex : tsRegex;
return {
name: 'transform-file',
enforce: 'pre',
async transform(src, id) {
if (fileRegex.test(id)) {
if (!parsedTsConfig) {
parsedTsConfig = parseTsConfig(tsconfigPath, process.cwd());
if (parsedTsConfig.sourcemap) {
parsedTsConfig.sourcemap = false;
parsedTsConfig.inlineSources = true;
parsedTsConfig.inlineSourceMap = true;
}
}
// Just return if we don't need to search the file.
if (
!forceTsc &&
(!parsedTsConfig ||
!parsedTsConfig.options ||
!parsedTsConfig.options.emitDecoratorMetadata)
) {
return;
}
// Find the decorator and if there isn't one, return out
const hasDecorator = findDecorators(src);
console.log("hasDecorator: ", hasDecorator);
if (!hasDecorator) {
return;
}
const program = typescript.transpileModule(src, { compilerOptions: parsedTsConfig.options });
return {
code: program.outputText,
map: null
}
}
},
}
}
function parseTsConfig(tsconfig, cwd = process.cwd()) {
const fileName = typescript.findConfigFile(
cwd,
typescript.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;
let configFileName;
if (fileName) {
const text = typescript.sys.readFile(fileName);
if (text === undefined) throw new Error(`failed to read '${fileName}'`);
const result = typescript.parseConfigFileTextToJson(fileName, text);
if (result.error !== undefined) {
printDiagnostics(result.error);
throw new Error(`failed to parse '${fileName}'`);
}
loadedConfig = result.config;
baseDir = path.dirname(fileName);
configFileName = fileName;
}
const parsedTsConfig = typescript.parseJsonConfigFileContent(
loadedConfig,
typescript.sys,
baseDir
);
if (parsedTsConfig.errors[0]) printDiagnostics(parsedTsConfig.errors);
return parsedTsConfig;
}
function printDiagnostics(...args) {
console.log(inspect(args, false, 10, true));
}