Skip to content

Commit

Permalink
refactor: try using swc to compile generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Jul 20, 2024
1 parent 96f6516 commit a0b4c0d
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 46 deletions.
1 change: 1 addition & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@prisma/generator-helper": "5.17.x",
"@prisma/internals": "5.17.x",
"@swc/core": "^1.7.0",
"@zenstackhq/language": "workspace:*",
"@zenstackhq/runtime": "workspace:*",
"langium": "1.3.1",
Expand Down
44 changes: 26 additions & 18 deletions packages/sdk/src/code-gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CompilerOptions, DiagnosticCategory, ModuleKind, Project, ScriptTarget } from 'ts-morph';
import { PluginError } from './types';
import { transformFile } from '@swc/core';
import fs from 'fs';
import path from 'path';
import { CompilerOptions, ModuleKind, Project, ScriptTarget } from 'ts-morph';

/**
* Creates a TS code generation project
Expand Down Expand Up @@ -31,21 +33,27 @@ export async function saveProject(project: Project) {
* Emit a TS project to JS files.
*/
export async function emitProject(project: Project) {
const errors = project.getPreEmitDiagnostics().filter((d) => d.getCategory() === DiagnosticCategory.Error);
if (errors.length > 0) {
console.error('Error compiling generated code:');
console.error(project.formatDiagnosticsWithColorAndContext(errors.slice(0, 10)));
await project.save();
throw new PluginError('', `Error compiling generated code`);
}

const result = await project.emit();
await project.save();

const emitErrors = result.getDiagnostics().filter((d) => d.getCategory() === DiagnosticCategory.Error);
if (emitErrors.length > 0) {
console.error('Some generated code is not emitted:');
console.error(project.formatDiagnosticsWithColorAndContext(emitErrors.slice(0, 10)));
await project.save();
throw new PluginError('', `Error emitting generated code`);
}
await Promise.all(
project
.getSourceFiles()
.map((sf) => sf.getFilePath())
.filter((file) => !file.endsWith('.d.ts'))
.map(async (file) => {
const output = await transformFile(file, {
jsc: {
parser: {
syntax: 'typescript',
tsx: false,
},
target: 'es2020',
},
module: {
type: 'commonjs',
},
});
fs.writeFileSync(path.join(path.dirname(file), path.basename(file, '.ts') + '.js'), output.code);
})
);
}
2 changes: 1 addition & 1 deletion packages/testtools/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export async function loadSchema(schema: string, options?: SchemaLoadOptions) {
tsconfig.include = ['**/*.ts'];
tsconfig.exclude = ['node_modules'];
fs.writeFileSync(path.join(projectDir, './tsconfig.json'), JSON.stringify(tsconfig, null, 2));
run('npx tsc --project tsconfig.json');
run('npx tsc --project tsconfig.json --noEmit');
}

if (options?.getPrismaOnly) {
Expand Down
Loading

0 comments on commit a0b4c0d

Please sign in to comment.