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

Add TypeScript to the build system #13489

Merged
merged 5 commits into from
Mar 28, 2022
Merged

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Feb 2, 2022

This commit modifies the build system so that TypeScript files can be
transpiled into ES5 just like JavaScript files.

Note that this commit does NOT change the build system to run TypeScript
files through the TypeScript compiler. In other words, no files will be
type-checked at the build stage, as we expect type-checking to be
handled elsewhere (live, via your editor integration with tsserver,
and before a PR is merged, via yarn lint). Rather, we merely instruct
Babel to strip TypeScript-specific syntax from any files that have it,
as if those files had been written using JavaScript syntax alone.

Why take this approach? Because it prevents the build process from being
negatively impacted with respect to performance (as TypeScript takes a
significant amount of time to run).

It's worth noting the downside of this approach: because we aren't
running files through TypeScript, but relying on Babel's TypeScript
transform
to identify TypeScript syntax, this transform has to keep
up with any syntax changes that TypeScript adds in the future. In fact
there are a few syntactical forms that Babel already does not recognize.
These forms are rare or are deprecated by TypeScript, so I don't
consider them to be a blocker, but it's worth noting just in case it
comes up later. Also, any settings we place in tsconfig.json will be
completely ignored by Babel. Again, this isn't a blocker because there
are some analogs for the most important settings reflected in the
options we can pass to the transform. These and other caveats are
detailed in the documentation for the transform.


Manual testing steps:

  • Check out this branch locally.
  • Look up the commit called "Add some sample files so linting can be tested" and cherry-pick it (if the commit disappears, try git cherry-pick 9af9b6bbf1b26df408a40101e7eb8e727d334e05).
    • This commit converts two files to TypeScript, ui/helpers/utils/tx-helper.js and ui/helpers/utils/storage-helpers.ts. Both of these files have dependents which are JavaScript files, and ui/helpers/utils/tx-helper.js has dependencies which are JavaScript files.
  • Run yarn setup.
  • Open ui/helpers/utils/storage-helpers.ts in your editor. Remove the semicolon from the first line; your editor should tell you (via ESLint) that this is invalid.
  • Now open ui/index.js. This file imports tx-helper.ts. Mouse over txHelper in the import txHelper line; you should see proper types for this function which come from the TypeScript file.
  • Run yarn lint. All files should pass.
  • Run yarn start. You shouldn't see any errors.
  • Open MetaMask in your browser (this assumes that you have the dev version installed). Click around. You shouldn't see any UI changes.
  • Run yarn dist. You shouldn't see any errors.
  • Open MetaMask in your browser (this assumes that you have the dev version installed). Click around. You shouldn't see any UI changes.
  • If you like, open dist/chrome/common-2.js and search for "getStorageItem" (one of the functions in ui/helpers/utils/storage-helpers.ts). Warning: this is a big file. You should find a reference to the function here.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2022

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@mcmire mcmire force-pushed the add-typescript-to-build-system branch from a1c8ea1 to 5bf6bee Compare February 15, 2022 22:33
@mcmire mcmire changed the base branch from develop to add-typescript-to-eslint-config February 15, 2022 22:34
"@babel/plugin-transform-runtime": true,
"@babel/preset-env": true,
"@babel/preset-react": true,
"@babel/preset-typescript": true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I re-alphabetized this list, but @babel/preset-typescript is the new entry here.

@mcmire mcmire changed the title [WIP] Add TypeScript to build system Add TypeScript to build system Feb 15, 2022
@mcmire mcmire marked this pull request as ready for review February 15, 2022 22:38
@mcmire mcmire requested review from a team and kumavis as code owners February 15, 2022 22:38
@mcmire mcmire requested review from danjm and removed request for a team February 15, 2022 22:38
@mcmire mcmire force-pushed the add-typescript-to-eslint-config branch from 8842d00 to c0852fe Compare February 15, 2022 22:43
@mcmire mcmire force-pushed the add-typescript-to-build-system branch from 5bf6bee to 58e8669 Compare February 15, 2022 22:43
@mcmire mcmire changed the title Add TypeScript to build system Add TypeScript to the build system Feb 15, 2022
@mcmire mcmire force-pushed the add-typescript-to-eslint-config branch from c0852fe to 8d473ee Compare February 15, 2022 22:46
@mcmire mcmire force-pushed the add-typescript-to-build-system branch 4 times, most recently from 4f884f6 to b2d7275 Compare February 16, 2022 19:08
@@ -1,7 +1,11 @@
module.exports = function (api) {
api.cache(false);
return {
parserOpts: {
strictMode: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sure TypeScript files are parsed using strict mode. See https://babeljs.io/docs/en/babel-plugin-transform-typescript#typescript-compiler-options.

@metamaskbot
Copy link
Collaborator

Builds ready [b2d7275]
Page Load Metrics (1182 ± 32 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint711214378406195
domContentLoaded1107136311796431
load1108137211826632
domInteractive1107136211786431

@metamaskbot
Copy link
Collaborator

Builds ready [4fe05c8]
Page Load Metrics (1113 ± 25 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6242020915574
domContentLoaded1037124911135225
load1037124911135225
domInteractive1037124911135225

@mcmire mcmire force-pushed the add-typescript-to-eslint-config branch 3 times, most recently from e91257c to 93d8f12 Compare March 1, 2022 22:28
@mcmire mcmire force-pushed the add-typescript-to-build-system branch from 4fe05c8 to 9b822d3 Compare March 1, 2022 22:32
@mcmire mcmire force-pushed the add-typescript-to-eslint-config branch from 93d8f12 to 0d855e4 Compare March 2, 2022 16:45
@mcmire mcmire force-pushed the add-typescript-to-build-system branch from 9b822d3 to 3bfd3a9 Compare March 2, 2022 16:45
@metamaskbot
Copy link
Collaborator

Builds ready [3bfd3a9]
Page Load Metrics (1236 ± 44 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint691398440491236
domContentLoaded1097140212329144
load1097140212369244
domInteractive1097140212329144

@mcmire mcmire force-pushed the add-typescript-to-eslint-config branch from 0d855e4 to de92b97 Compare March 4, 2022 16:47
Base automatically changed from add-typescript-to-eslint-config to develop March 21, 2022 18:54
@mcmire mcmire force-pushed the add-typescript-to-build-system branch 2 times, most recently from ade7789 to 3266b8a Compare March 21, 2022 19:55
@metamaskbot
Copy link
Collaborator

Builds ready [3266b8a]
Page Load Metrics (1467 ± 62 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint7412891147
domContentLoaded12481773146213062
load12481773146712862
domInteractive12481773146213062

mcmire added 3 commits March 22, 2022 10:31
This commit modifies the build system so that TypeScript files can be
transpiled into ES5 just like JavaScript files.

Note that this commit does NOT change the build system to run TypeScript
files through the TypeScript compiler. In other words, no files will be
type-checked at the build stage, as we expect type-checking to be
handled elsewhere (live, via your editor integration with `tsserver`,
and before a PR is merged, via `yarn lint`). Rather, we merely instruct
Babel to strip TypeScript-specific syntax from any files that have it,
as if those files had been written using JavaScript syntax alone.

Why take this approach? Because it prevents the build process from being
negatively impacted with respect to performance (as TypeScript takes a
significant amount of time to run).

It's worth noting the downside of this approach: because we aren't
running files through TypeScript, but relying on Babel's [TypeScript
transform][1] to identify TypeScript syntax, this transform has to keep
up with any syntax changes that TypeScript adds in the future. In fact
there are a few syntactical forms that Babel already does not recognize.
These forms are rare or are deprecated by TypeScript, so I don't
consider them to be a blocker, but it's worth noting just in case it
comes up later. Also, any settings we place in `tsconfig.json` will be
completely ignored by Babel. Again, this isn't a blocker because there
are some analogs for the most important settings reflected in the
options we can pass to the transform. These and other caveats are
detailed in the [documentation for the transform][2].

[1]: https://babeljs.io/docs/en/babel-plugin-transform-typescript
[2]: https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats
@mcmire mcmire force-pushed the add-typescript-to-build-system branch from 471fa31 to aa802f5 Compare March 22, 2022 16:31
@metamaskbot
Copy link
Collaborator

Builds ready [aa802f5]
Page Load Metrics (1406 ± 36 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint80151101209
domContentLoaded1281158514027636
load1288158514067436
domInteractive1281158514027636

@metamaskbot
Copy link
Collaborator

Builds ready [79740fd]
Page Load Metrics (1264 ± 19 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint731245144253122
domContentLoaded1199138212614019
load1199138212644019
domInteractive1199138212614019

@metamaskbot
Copy link
Collaborator

Builds ready [628d4e9]
Page Load Metrics (1455 ± 96 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint721932187401192
domContentLoaded12182013145120297
load12292013145519996
domInteractive12182013145120297

Copy link
Contributor

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@digiwand digiwand left a comment

Choose a reason for hiding this comment

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

LGTM 🙌 One of the questions asked at Ethereum Rio 2022 was if or when we are supporting TypeScript in our repo? I told them you've been making tremendous headway here in Q1!

@mcmire mcmire merged commit 53006d4 into develop Mar 28, 2022
@mcmire mcmire deleted the add-typescript-to-build-system branch March 28, 2022 22:33
@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants