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

build: Add script to build & minify loader #46780

Merged
merged 3 commits into from
Apr 6, 2023
Merged

build: Add script to build & minify loader #46780

merged 3 commits into from
Apr 6, 2023

Conversation

mydea
Copy link
Member

@mydea mydea commented Apr 3, 2023

This adds a simple build script for the JS Loader. This should help with two things:

  • The base template is plain JS, making syntax highlighting and error finding etc. much easier
  • Have a reproducible minification step

This just has to be run manually whenever the loader script is changed, the output is fully checked in etc. so nothing changes there.

I've tried the minified output in a local test app, would be great if someone else could also give it a try!

@mydea mydea requested review from HazAT and AbhiPrasad April 3, 2023 09:52
@mydea mydea self-assigned this Apr 3, 2023
@mydea mydea requested review from a team as code owners April 3, 2023 09:52
@github-actions github-actions bot added Scope: Frontend Automatically applied to PRs that change frontend components Scope: Backend Automatically applied to PRs that change backend components labels Apr 3, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Apr 3, 2023

🚨 Warning: This pull request contains Frontend and Backend changes!

It's discouraged to make changes to Sentry's Frontend and Backend in a single pull request. The Frontend and Backend are not atomically deployed. If the changes are interdependent of each other, they must be separated into two pull requests and be made forward or backwards compatible, such that the Backend or Frontend can be safely deployed independently.

Have questions? Please ask in the #discuss-dev-infra channel.

@@ -0,0 +1,233 @@
(
function sentryLoader(
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: Without making this non-anonymous, terser keeps inlining __LOADER_IS_LAZY__ as a constant (which wouldn't be terrible, but it's nicer to have this all at the end). Adding a function name fixes this (and it is actually removed in the minified bundle anyhow).

Copy link
Member

Choose a reason for hiding this comment

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

Are you able to write this script in typescript?

Copy link
Member

Choose a reason for hiding this comment

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

agree with @evanpurkhiser ... and wouldn't it be nice to have prettier formatting this as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

So I set it up to have prettier & eslint. We could use typescript but that would complicate the build step - we'd need to use either rollup or babel or something like this then. Right now we just use the script as-is mostly (which also means the script has to be ES5 compatible!)

Copy link
Member

Choose a reason for hiding this comment

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

We have ts-node installed, so it should just work

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, so i refactored it to use typescript and use typescript directly to compile it!

Copy link
Member Author

Choose a reason for hiding this comment

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

I also rewrote the build-script itself to typescript.

@priscilawebdev
Copy link
Member

will this generate the script we are displaying in the onboarding? it is not clear to me what is the goal of this PR

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

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

Minified loader impl looks good!

@mydea mydea force-pushed the fn/loader-tests branch from 7907bd5 to fff542a Compare April 4, 2023 12:04
@mydea
Copy link
Member Author

mydea commented Apr 4, 2023

will this generate the script we are displaying in the onboarding? it is not clear to me what is the goal of this PR

This script generates the actual Django templates from a base .js file. The goal of this is mainly to streamline the minification of this script, as previously we did that manually e.g. online by closure compiler. This is a) manual, b) error prone, c) not reproducible very well (and actually lead to an incident last Friday).

I added a comment to the top of the script explaining the purpose of the script in detail as well!

Note: I've also checked the source script for ES5 compatibility to make sure it is valid ES5 JS.

Copy link
Member

@priscilawebdev priscilawebdev left a comment

Choose a reason for hiding this comment

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

the types in the file src/sentry/templates/sentry/js-sdk-loader.ts still can be improved but it looks good to me!

@mydea mydea force-pushed the fn/loader-tests branch from db03d3c to 29b88bf Compare April 5, 2023 08:45
@mydea mydea requested a review from a team as a code owner April 5, 2023 08:45
@mydea
Copy link
Member Author

mydea commented Apr 6, 2023

OK, so I tested this multiple times in a local test app, both minified and unminifed seem to be working as expected!

@mydea mydea merged commit 8014401 into master Apr 6, 2023
@mydea mydea deleted the fn/loader-tests branch April 6, 2023 07:15
@github-actions github-actions bot locked and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components Scope: Frontend Automatically applied to PRs that change frontend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants