Skip to content

refactor(typescript): add foundation for incremential TS migration - #13569

Merged
sdepold merged 17 commits into
mainfrom
prep-ts-migration
Nov 18, 2021
Merged

sdepold merged 17 commits into
mainfrom
prep-ts-migration

Conversation

@wbourne0

@wbourne0 wbourne0 commented Oct 17, 2021 •

Copy link
Copy Markdown
Member

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions? N/A, though tests are now run on the compiled code.
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Adds a foundation for a migration to TypeScript (not just .d.ts files). This is intended to be for a gradual migration, and getting this in sooner makes it easier to start converting files to TypeScript once we're ready.

The primary changes are:

  • Added a build script which generates .js, .js.map, and .d.ts files via esbuild and typescript.
  • Added eslint rules for TypeScript.
  • Updated existing tests to import from the generated code.
  • Converts the logger interface to TS. This change is intended to ensure that the build script is at least working; the file itself was chosen primarily because it is unlikely to be changed often, and therefore unlikely to run into merge conflicts.

The following (dev) dependencies were added:

  • @types/debug - Types for the builtin debug package, used by the logger.
  • @typescript-eslint/eslint-plugin - TypeScript plugin for eslint.
  • @typescript-eslint/parser - TypeScript parser for eslint.
  • copyfiles - Used in build.js to copy type declaration files from types/ to dist/.
  • esbuild - Used in build.js to transpile the sequelize source code into minified code.
  • fast-glob - Used in build.js to locate files that need to be copied / transpiled.
  • module-alias - Used for testing to alias imports to sequelize/ to the dist directory.

Additionally, the strict rule which required use strict at the start of every file was removed. This is because esbuild will add this in the transpiled files, so it doesn't make sense to require it anymore.

As for why I went with esbuild - 2 primary reasons:

  • When I tested it against swc, it was consistently faster (from memory, ~150ms vs ~60ms for esbuild)
  • I'm already familiar with esbuild and so it was easier for me to setup.

Note that this does not add support for .test.ts files yet - I wanted to limit the complexity of this PR, so I opted to leave that out of this one. I'll put up a PR with that if this gets approved.

@wbourne0 wbourne0 added type: refactor DEPRECATED: replace with the "meta" issue type typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. labels Oct 17, 2021
@wbourne0 wbourne0 self-assigned this Oct 17, 2021
Converts the logger interface to TypeScript.
Adds eslint rules and TypeScript configuration to allow for a gradual migration to TS.
Adds a build script to transpile both TS and JS code while keeping the same API for package usage.
Changes imports from test files to use "absolute" paths that point to the directory of the compiled code.
@wbourne0 wbourne0 changed the title feat(typescript): add foundation for incremential TypeScript migration refactor(typescript): add foundation for incremential TS migration Oct 18, 2021

@sdepold sdepold left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this changing the way I can import / user Sequelize in a TS based application?

Comment thread build.js Resolved
Comment thread build.js Resolved
Comment thread build.js Resolved
Comment thread build.js Resolved
Comment thread lib/sequelize.js Resolved
Comment thread lib/utils/logger.ts Resolved
Comment thread lib/utils/logger.ts Resolved
Comment thread package.json Resolved
Comment thread package.json Resolved
@sdepold

sdepold commented Oct 29, 2021

Copy link
Copy Markdown
Member

Soooo. Can we just merge this or do we have to double check or prepare anything else?

@wbourne0

Copy link
Copy Markdown
Member Author

Soooo. Can we just merge this or do we have to double check or prepare anything else?

Should be good to go! We'll have to resolve any merge conflicts (which will probably be any test file due to the change in relative paths into sequelize/*) but other than that it should be seamless.

@sdepold

sdepold commented Nov 1, 2021

Copy link
Copy Markdown
Member

Soooo. Can we just merge this or do we have to double check or prepare anything else?

Should be good to go! We'll have to resolve any merge conflicts (which will probably be any test file due to the change in relative paths into sequelize/*) but other than that it should be seamless.

Ok. And there is no change whatsoever to the release and require / import flow, right?

@wbourne0

wbourne0 commented Nov 1, 2021 •

Copy link
Copy Markdown
Member Author

Ok. And there is no change whatsoever to the release and require / import flow, right?

For the release flow, npm publish should run npm prepare. Under Life Cycle Scripts in the docs for npm:
image

There shouldn't be any from a user's perspective, though where require('sequelize/*') resolves to has changed.
I've verified parts of this via npm pack -> tar -xf sequelize-0.0.0-development.tgz -> try to require package/.
Never too much testing though, feel free to poke at it through npm pack.

@sdepold

sdepold commented Nov 2, 2021

Copy link
Copy Markdown
Member

Ok. And there is no change whatsoever to the release and require / import flow, right?

For the release flow, npm publish should run npm prepare. Under Life Cycle Scripts in the docs for npm: image

There shouldn't be any from a user's perspective, though where require('sequelize/*') resolves to has changed. I've verified parts of this via npm pack -> tar -xf sequelize-0.0.0-development.tgz -> try to require package/. Never too much testing though, feel free to poke at it through npm pack.

once merged, we could try to release an alpha version to npm :)

@sdepold sdepold self-assigned this Nov 2, 2021
@sdepold

sdepold commented Nov 6, 2021

Copy link
Copy Markdown
Member

@allawesome497 Are you going to enable the test compilation too?

Comment thread lib/utils/logger.ts
*
* @default 'sequelize'
*/
context?: string;

@Keimeno Keimeno Nov 6, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I suppose you removed the debug property because it isn't used anywhere in the codebase. @sdepold do you know why we had a debug property in the logger config in the first place?

this.config = {
  context: 'sequelize',
  debug: true,
  ...config
};

reference

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nope no idea

@wbourne0

wbourne0 commented Nov 7, 2021

Copy link
Copy Markdown
Member Author

@allawesome497 Are you going to enable the test compilation too?

Yep! just did.

Tests now run with a require hook (transpiles imports to .ts files via esbuild when they're required), so .test.ts files will be transpiled via esbuild which will let them be executed, then I also added another tsconfig.json for tests which will handle type checking.

I also added a logger.test.ts file to ensure that ts tests work properly (and removed the existing tests from the general util tests since they weren't needed anymore).

@sdepold

sdepold commented Nov 8, 2021

Copy link
Copy Markdown
Member

That last commit is a tribute to @tj:

test.js -> test.[tj]s

😂👋

@sdepold

sdepold commented Nov 8, 2021

Copy link
Copy Markdown
Member

Nice stuff. I'd say let's release a new version of main, merge this guy and attempt another release afterwards.

@sdepold

sdepold commented Nov 8, 2021

Copy link
Copy Markdown
Member

I guess you need to fix the merge conflicts

@sdepold
sdepold merged commit 51bc05a into main Nov 18, 2021
@sdepold
sdepold deleted the prep-ts-migration branch November 18, 2021 18:57
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 6.12.0-alpha.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions

github-actions Bot commented Dec 4, 2021

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 6.12.0-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 6.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

aliatsis pushed a commit to creditiq/sequelize that referenced this pull request Jun 2, 2022
…equelize#13569)

* refactor(typescript): add foundation for incremential TS migration

Converts the logger interface to TypeScript.
Adds eslint rules and TypeScript configuration to allow for a gradual migration to TS.
Adds a build script to transpile both TS and JS code while keeping the same API for package usage.
Changes imports from test files to use "absolute" paths that point to the directory of the compiled code.

* use || instead of ??

* fix source maps breaking tests

* fix build script for older versions of node

* make some small fixes + add better docs to logger.ts

* add ts support to tests

* update yarn.lock

* disable no-loss-of-percesion rule

* fix & cleanup a lil

* test.js -> test.[tj]s

Co-authored-by: Sascha Depold <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released on @v6-alpha released on @v6-beta released type: refactor DEPRECATED: replace with the "meta" issue type typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants