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

feat: use angular compiler api to transform codes #562

Merged
merged 6 commits into from
Dec 12, 2020
Merged

feat: use angular compiler api to transform codes #562

merged 6 commits into from
Dec 12, 2020

Conversation

ahnpnl
Copy link
Collaborator

@ahnpnl ahnpnl commented Oct 20, 2020

Summary

See individual commits

NgJestCompiler takes care of compiling files and return a string which is the compiled output concatenating with source map string. The compiler also does type checking.

This compiler is mainly inspired by AngularCompilerPlugin in combination with ts-jest LanguageService.

Closes #108
Closes #288
Closes #322
Closes #353
Closes #622

Check list

  • Type checking

  • Code compilation

  • Memory cache for Program

  • Allow custom compiler module, e.g ttypescript (via ts-jest option)

  • Downlevel ctor transformer

  • hoisting (from ts-jest)

  • ngcc util script

Test plan

Does this PR introduce a breaking change?

  • Yes
  • No

With the new jest transformer, jest-preset-angular now switches to default to use this new transformer and no longer use ts-jest to transform codes.

Users who are currently doing in jest config

// jest.config.js
module.exports = {
    // [...]
    transform: {
      '^.+\\.(ts|js|html)$': 'ts-jest',
    },
}

should change to

// jest.config.js
module.exports = {
    // [...]
    transform: {
      '^.+\\.(ts|js|html)$': 'jest-preset-angular',
    },
}

isolatedModule: true will still use ts-jest to compile ts to js but you won't get full compatibility with Ivy.

Other information

N.A.

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Oct 20, 2020

@wtho here we go !!! The compiler now works with our current e2e tests, as well as solves #353

Copy link
Collaborator

@wtho wtho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you added the downlevelConstructorParameters!

Wow this is so cool! I wish I would have been more involved into this, but I was quite busy recently.
Once more thanks again!

},
},
transform: {
'^.+\\.(ts|js|html)$': 'ts-jest',
'^.+\\.(ts|js|html)$': 'jest-preset-angular',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this is it! Haha, will have to get used to it!

@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Oct 20, 2020

Still have some fine tunings to do but we are pretty close. The current state is already in pretty good shape. Probably next week I can push this out in 9.0.0-next.1 once this one is done.

@wtho
Copy link
Collaborator

wtho commented Oct 27, 2020

What's still open? Is there still more to address?

@ahnpnl ahnpnl marked this pull request as ready for review October 27, 2020 09:46
@ahnpnl ahnpnl requested a review from thymikee as a code owner October 27, 2020 09:46
@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Oct 27, 2020

Should be ready now, I couldn't find example in #322 to try out, but seems like #322 will be solved with this PR too.

Updated: #322 will be solved too, I just added e2e test taking example from it.
Updated 2: #108 will also be solved (forwardRef related with downlevel ctor)

@ahnpnl ahnpnl requested a review from wtho October 27, 2020 10:06
@ahnpnl ahnpnl linked an issue Oct 27, 2020 that may be closed by this pull request
@ahnpnl
Copy link
Collaborator Author

ahnpnl commented Oct 29, 2020

I have published this PR under 9.0.0-next.1 release so it's easier to test with real projects.

ahnpnl added a commit that referenced this pull request Nov 5, 2020
@ahnpnl ahnpnl added the 💣 Breaking Changes Includes a breaking change and should probably wait until we're preparing for the release of a major label Nov 8, 2020
@ahnpnl ahnpnl added this to the 9.0.0 milestone Nov 10, 2020
@ahnpnl ahnpnl changed the title feat(compiler): introduce NgJestCompiler feat: use angular compiler api to transform codes Dec 12, 2020
@ahnpnl ahnpnl mentioned this pull request Dec 12, 2020
2 tasks
Closes #108
Closes #288
Closes #322
Closes #353

BREAKING CHANGE:
With the new jest transformer, `jest-preset-angular` now switches to default to use this new transformer and no longer use `ts-jest` to transform codes.

Users who are currently doing in jest config
```
// jest.config.js
module.exports = {
    // [...]
    transform: {
      '^.+\\.(ts|js|html)$': 'ts-jest',
    },
}
```

should change to
```
// jest.config.js
module.exports = {
    // [...]
    transform: {
      '^.+\\.(ts|js|html)$': 'jest-preset-angular',
    },
}
```

`isolatedModule: true` will still use `ts-jest` to compile `ts` to `js` but you won't get full compatibility with Ivy.

To make sure tests work, please always run `ngcc` in postinstall hook
Jest parallel mode doesn't allow `ngcc` to run. The only way to run `ngcc` with Jest is invoking `ngcc` directly in `jest.config.js`. Because Jest reads `jest.config.js` only once before running tests, running `ngcc` is possible with it
In a jest transformer, `process` is the place where code transformation happens. Therefore, we should only create `NgJestTransformer` instance in `process`, not when creating `NgJestConfig` instance
@ahnpnl ahnpnl merged commit 1468ceb into thymikee:master Dec 12, 2020
@ahnpnl ahnpnl deleted the ng-jest-compiler branch December 12, 2020 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💣 Breaking Changes Includes a breaking change and should probably wait until we're preparing for the release of a major
Projects
None yet
2 participants