Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SWC as compiler #9216

Closed

Conversation

stevefan1999-personal
Copy link

@stevefan1999-personal stevefan1999-personal commented Jul 19, 2022

Description of change

This PR will replace tsc with swc, that the responsibility of tsc will be to emit declarations only. SWC is the fastest Typescript transpiler that supports decorators, and because it is written in Rust, making it virtually zero transpilation turnaround time compared to ts-node, and can be seamlessly integrated into Mocha as well using @swc-node/register without needing to prebuild as well. You can now just click the test run button in either VSCode or IDEA to get direct test result. This also means we can use Typescript paths to infer the location of our source code. I have also did a preliminary replacement of ../../../src and such to typeorm instead which resulted in a huge diff in the PR.

Also, by switching the build responsibility to a set of new npm run commands that invokes SWC, this also means you can now save at least 3x time in Gulp that now we lowered it down to around 20 seconds, which is before at least 1 minute. Keep in mind that SWC does have some slight semantic difference compared to the emission of TSC, which is now discovered in some of the tests: ironically speaking, there are unintentional bugs inside tests due to implementation difference between CommonJS and ES modules though.

Bug #1: Cyclic import

ReferenceError: Cannot access 'One' before initialization
    at Object.get (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\test\benchmark\multiple-joins-querybuilder\entity\One.ts:16:14)
    at Object.<anonymous> (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\test\benchmark\multiple-joins-querybuilder\entity\Two.ts:13:13)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Module._compile (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Object.newLoader [as .ts] (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\test\benchmark\multiple-joins-querybuilder\entity\One.ts:13:14)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Module._compile (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Object.newLoader [as .ts] (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\test\benchmark\multiple-joins-querybuilder\entity\Eight.ts:12:14)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Module._compile (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\pirates\lib\index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Object.newLoader [as .ts] (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.exports.requireOrImport (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\mocha\lib\esm-utils.js:42:12)
    at Object.exports.loadFilesAsync (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\mocha\lib\esm-utils.js:55:34)
    at singleRun (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\mocha\lib\cli\run-helpers.js:125:3)
    at Object.exports.handler (C:\Users\steve\project\git\github.com\stevefan1999-personal\typeorm\node_modules\mocha\lib\cli\run.js:362:5)

Solution: Lazy initialization by amalgamated export. POC:

// File one.ts
import * as entities from './'
@Entity()
export class Two {
  @Column(() => entities.Two) two: entities.Two
}
// File two.ts
import * as entities from './'
@Entity()
export class Two {
  @Column(() => entities.One) one: entities.One
}
// File index.ts
import { One } from './one'
import { Two } from './two'

export default { One, Two }

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run format to apply prettier formatting
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
gulp: prepend shebang for cli transpiled file;
gulp: fix parallel build;

Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
since we can directly use typescript in mocha now, and swc-node/register supports tsconfig path

Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
@stevefan1999-personal
Copy link
Author

Test seems to be blocked by swc-project/swc#5047, but I've pinned the last version that can work with circular imports.

Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
blocked by swc-project/swc#5252

Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
Signed-off-by: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com>
@pleerock
Copy link
Member

I don't think I want to switch out of tsc at the moment. For now it satisfies me in most of things. Also it's a mainstream right now. Maybe someday in the future. Thanks for suggestion, I'll keep it in mind.

@pleerock pleerock closed this Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants