Skip to content

Commit 1fc18f6

Browse files
feat(plugin): use local ts binary for meta printer
1 parent e5f5fdf commit 1fc18f6

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

lib/compiler/plugins/plugin-metadata-generator.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export class PluginMetadataGenerator {
6060
private readonly pluginMetadataPrinter = new PluginMetadataPrinter();
6161
private readonly typeCheckerHost = new TypeCheckerHost();
6262
private readonly typescriptLoader = new TypeScriptBinaryLoader();
63+
private readonly tsBinary: typeof ts;
64+
65+
constructor() {
66+
this.tsBinary = this.typescriptLoader.load();
67+
}
6368

6469
generate(options: PluginMetadataGenerateOptions) {
6570
const {
@@ -145,9 +150,14 @@ export class PluginMetadataGenerator {
145150
...visitor.typeImports,
146151
};
147152
});
148-
this.pluginMetadataPrinter.print(collectedMetadata, typeImports, {
149-
outputDir,
150-
filename,
151-
});
153+
this.pluginMetadataPrinter.print(
154+
collectedMetadata,
155+
typeImports,
156+
{
157+
outputDir,
158+
filename,
159+
},
160+
this.tsBinary,
161+
);
152162
}
153163
}

lib/compiler/plugins/plugin-metadata-printer.ts

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,50 @@ export class PluginMetadataPrinter {
2424
metadata: ComposedPluginMeta,
2525
typeImports: Record<string, string>,
2626
options: PluginMetadataPrintOptions,
27+
tsBinary: typeof ts,
2728
) {
28-
const objectLiteralExpr = ts.factory.createObjectLiteralExpression(
29+
const objectLiteralExpr = tsBinary.factory.createObjectLiteralExpression(
2930
Object.keys(metadata).map((key) =>
3031
this.recursivelyCreatePropertyAssignment(
3132
key,
3233
metadata[key] as unknown as Array<
3334
[ts.CallExpression, DeepPluginMeta]
3435
>,
36+
tsBinary,
3537
),
3638
),
3739
);
3840

39-
const exportAssignment = ts.factory.createExportAssignment(
41+
const exportAssignment = tsBinary.factory.createExportAssignment(
4042
undefined,
4143
undefined,
42-
ts.factory.createArrowFunction(
43-
[ts.factory.createToken(ts.SyntaxKind.AsyncKeyword)],
44+
tsBinary.factory.createArrowFunction(
45+
[tsBinary.factory.createToken(tsBinary.SyntaxKind.AsyncKeyword)],
4446
undefined,
4547
[],
4648
undefined,
47-
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
48-
ts.factory.createBlock(
49+
tsBinary.factory.createToken(
50+
tsBinary.SyntaxKind.EqualsGreaterThanToken,
51+
),
52+
tsBinary.factory.createBlock(
4953
[
50-
this.createTypeImportVariableStatement(typeImports),
51-
ts.factory.createReturnStatement(objectLiteralExpr),
54+
this.createTypeImportVariableStatement(typeImports, tsBinary),
55+
tsBinary.factory.createReturnStatement(objectLiteralExpr),
5256
],
5357
true,
5458
),
5559
),
5660
);
5761

58-
const printer = ts.createPrinter({
59-
newLine: ts.NewLineKind.LineFeed,
62+
const printer = tsBinary.createPrinter({
63+
newLine: tsBinary.NewLineKind.LineFeed,
6064
});
61-
const resultFile = ts.createSourceFile(
65+
const resultFile = tsBinary.createSourceFile(
6266
'file.ts',
6367
'',
64-
ts.ScriptTarget.Latest,
68+
tsBinary.ScriptTarget.Latest,
6569
/*setParentNodes*/ false,
66-
ts.ScriptKind.TS,
70+
tsBinary.ScriptKind.TS,
6771
);
6872

6973
const filename = join(
@@ -75,7 +79,7 @@ export class PluginMetadataPrinter {
7579
filename,
7680
eslintPrefix +
7781
printer.printNode(
78-
ts.EmitHint.Unspecified,
82+
tsBinary.EmitHint.Unspecified,
7983
exportAssignment,
8084
resultFile,
8185
),
@@ -85,15 +89,16 @@ export class PluginMetadataPrinter {
8589
private recursivelyCreatePropertyAssignment(
8690
identifier: string,
8791
meta: DeepPluginMeta | Array<[ts.CallExpression, DeepPluginMeta]>,
92+
tsBinary: typeof ts,
8893
): ts.PropertyAssignment {
8994
if (Array.isArray(meta)) {
90-
return ts.factory.createPropertyAssignment(
91-
ts.factory.createStringLiteral(identifier),
92-
ts.factory.createArrayLiteralExpression(
95+
return tsBinary.factory.createPropertyAssignment(
96+
tsBinary.factory.createStringLiteral(identifier),
97+
tsBinary.factory.createArrayLiteralExpression(
9398
meta.map(([importExpr, meta]) =>
94-
ts.factory.createArrayLiteralExpression([
99+
tsBinary.factory.createArrayLiteralExpression([
95100
importExpr,
96-
ts.factory.createObjectLiteralExpression(
101+
tsBinary.factory.createObjectLiteralExpression(
97102
Object.keys(meta).map((key) =>
98103
this.recursivelyCreatePropertyAssignment(
99104
key,
@@ -102,6 +107,7 @@ export class PluginMetadataPrinter {
102107
[key: string]: DeepPluginMeta;
103108
}
104109
)[key],
110+
tsBinary,
105111
),
106112
),
107113
),
@@ -110,11 +116,11 @@ export class PluginMetadataPrinter {
110116
),
111117
);
112118
}
113-
return ts.factory.createPropertyAssignment(
114-
ts.factory.createStringLiteral(identifier),
115-
ts.isObjectLiteralExpression(meta as unknown as ts.Node)
119+
return tsBinary.factory.createPropertyAssignment(
120+
tsBinary.factory.createStringLiteral(identifier),
121+
tsBinary.isObjectLiteralExpression(meta as unknown as ts.Node)
116122
? (meta as ts.ObjectLiteralExpression)
117-
: ts.factory.createObjectLiteralExpression(
123+
: tsBinary.factory.createObjectLiteralExpression(
118124
Object.keys(meta).map((key) =>
119125
this.recursivelyCreatePropertyAssignment(
120126
key,
@@ -123,6 +129,7 @@ export class PluginMetadataPrinter {
123129
[key: string]: DeepPluginMeta;
124130
}
125131
)[key],
132+
tsBinary,
126133
),
127134
),
128135
),
@@ -131,37 +138,42 @@ export class PluginMetadataPrinter {
131138

132139
private createTypeImportVariableStatement(
133140
typeImports: Record<string, string>,
141+
tsBinary: typeof ts,
134142
): ts.Statement {
135-
return ts.factory.createVariableStatement(
143+
return tsBinary.factory.createVariableStatement(
136144
undefined,
137-
ts.factory.createVariableDeclarationList(
145+
tsBinary.factory.createVariableDeclarationList(
138146
[
139-
ts.factory.createVariableDeclaration(
140-
ts.factory.createIdentifier(TYPE_IMPORT_VARIABLE_NAME),
147+
tsBinary.factory.createVariableDeclaration(
148+
tsBinary.factory.createIdentifier(TYPE_IMPORT_VARIABLE_NAME),
141149
undefined,
142150
undefined,
143-
ts.factory.createObjectLiteralExpression(
151+
tsBinary.factory.createObjectLiteralExpression(
144152
Object.keys(typeImports).map((ti) =>
145-
this.createPropertyAssignment(ti, typeImports[ti]),
153+
this.createPropertyAssignment(ti, typeImports[ti], tsBinary),
146154
),
147155
true,
148156
),
149157
),
150158
],
151-
ts.NodeFlags.Const |
152-
ts.NodeFlags.AwaitContext |
153-
ts.NodeFlags.ContextFlags |
154-
ts.NodeFlags.TypeExcludesFlags,
159+
tsBinary.NodeFlags.Const |
160+
tsBinary.NodeFlags.AwaitContext |
161+
tsBinary.NodeFlags.ContextFlags |
162+
tsBinary.NodeFlags.TypeExcludesFlags,
155163
),
156164
);
157165
}
158166

159-
private createPropertyAssignment(identifier: string, target: string) {
160-
return ts.factory.createPropertyAssignment(
161-
ts.factory.createComputedPropertyName(
162-
ts.factory.createStringLiteral(identifier),
167+
private createPropertyAssignment(
168+
identifier: string,
169+
target: string,
170+
tsBinary: typeof ts,
171+
) {
172+
return tsBinary.factory.createPropertyAssignment(
173+
tsBinary.factory.createComputedPropertyName(
174+
tsBinary.factory.createStringLiteral(identifier),
163175
),
164-
ts.factory.createIdentifier(target),
176+
tsBinary.factory.createIdentifier(target),
165177
);
166178
}
167179
}

0 commit comments

Comments
 (0)