Skip to content

misc(build): minify bundles with terser#9605

Merged
connorjclark merged 35 commits into
masterfrom
bundle-sourcemaps-2
Sep 17, 2020
Merged

misc(build): minify bundles with terser#9605
connorjclark merged 35 commits into
masterfrom
bundle-sourcemaps-2

Conversation

@connorjclark

@connorjclark connorjclark commented Aug 23, 2019

Copy link
Copy Markdown
Collaborator
  1. removes usage of babel for minifying, replace with terser
  2. emits source maps for each bundle
  3. adds a more detailed comment at the top of each bundle

image

I encourage reviewers to yarn build-lr and view the map on https://sokra.github.io/source-map-visualization/

dist/lightrider/lighthouse-lr-bundle.js 9.60 MB -> 8.94 MB (7%)
dist/lighthouse-dt-bundle.js 2.10 MB -> 1.62 MB (23%)

Comment thread build/build-bundle.js Outdated
@connorjclark

connorjclark commented Aug 23, 2019

Copy link
Copy Markdown
Collaborator Author

Note: the viewer needs additional changes to its build script to emit a map. This only emits a map for LR, the extension, and DT.

@connorjclark

connorjclark commented Aug 23, 2019

Copy link
Copy Markdown
Collaborator Author

some changes....

there's an extra space in the fn declaration for modules:
image

@connorjclark

connorjclark commented Aug 24, 2019

Copy link
Copy Markdown
Collaborator Author

not obvious why the devtools bundle got so much bigger (+ 504KB on disk).

Here's the visual bundle:
image

Can't do the same for "base"/master, because this tool requires a source map :)

@connorjclark

connorjclark commented Aug 24, 2019

Copy link
Copy Markdown
Collaborator Author

got a visual bundle from the previous process of browserify -> babel (ccf966d).

image

as you can see, "unmapped" went up a lot.

@connorjclark

connorjclark commented Aug 24, 2019

Copy link
Copy Markdown
Collaborator Author

the answer is in here somewhere... (diff b/wn dt bundles) https://gist.github.com/connorjclark/c80ca014042950ff0dd15ee23fcb6f42

Must be related to the babel version bump. I think it's related to "minifying" with a transform during browserify, instead of processing the output of browserify.

Comment thread yarn.lock
through2 "^2.0.0"
xtend "^4.0.0"

mold-source-map@~0.4.0:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this is an interesting package

@connorjclark

Copy link
Copy Markdown
Collaborator Author

The culprit is the browserify builtins. They don't get processed by the babelify transform - so comments, whitespace, all remain.

@connorjclark

connorjclark commented Aug 27, 2019

Copy link
Copy Markdown
Collaborator Author

Here where we're at:

We do browserify -> babel. Bundle is 1.6M. Babel's job is just to reduce the size of the script (via retain_lines and compact). Great when their were two passes. However, emitting source maps with this two-pass compilation created off-by-one issues.

Moving babel into browserify via babelify is cool, but it missed out on minimizing the builtins that browserify injects. This adds ~0.3M to the devtools bundle.

We could completely remove the line retaining minimization, and just use Terser. This save ~0.4MB. Source maps are perfect in this two-pass compilation. Terser handles input maps better than babel, apparently.

I asked @patrickhulce about the utility of "retain lines" in the bundle output. He said it originated from wanting "readable" diffs when rolling to DevTools. I wonder if that is a behavior we should bother keeping?

@patrickhulce

Copy link
Copy Markdown
Collaborator

He said it originated from wanting "readable" diffs when rolling to DevTools

yeah that and the moderately improved stack trace is a nice feature of retainLines, but I'm not sure how much we need either at this point. I can't remember the last time we got a legit bug report where the bundled stack trace was useful. If we had a mechanism (script?) that could easily translate a stack trace using our sourcemap, saving 0.5MB sounds like a steal to me.

@connorjclark

Copy link
Copy Markdown
Collaborator Author

If we had a mechanism (script?) that could easily translate a stack trace using our sourcemap, saving 0.5MB sounds like a steal to me.

covered! https://www.npmjs.com/package/stack-beautifier

npx stack-beautifier -t <file_with_stack.txt> <app.js.map>

@brendankenny

Copy link
Copy Markdown
Contributor

I like getting rid of yet another minifier in our deps :)

Did we figure out the serving thing, though? My main thing is probably the occasional stepping through LH in devtools and the current formatting does make that actually viable without a source map.

@brendankenny

Copy link
Copy Markdown
Contributor

Agreed with Patrick that it's pretty rare I find myself actually doing that, but then again that means what use are source maps? :)

@connorjclark

connorjclark commented Aug 27, 2019

Copy link
Copy Markdown
Collaborator Author

Did we figure out the serving thing, though? My main thing is probably the occasional stepping through LH in devtools and the current formatting does make that actually viable without a source map.

For devtools / Lightrider, you can attach source maps from a URL, which you would get by checking out the correct sha, building, and starting a local server in dist ... I think that's the best solution we have.

But don't forget the viewer - it's trivial to use maps there.

@connorjclark

Copy link
Copy Markdown
Collaborator Author

Worth noting: we have no tests that utilize the output of our bundled code (ala how React tests their lib). We might do good to run the smoke tests on the bundled code (#9501 references how).

@connorjclark connorjclark changed the title misc: emit source map for bundles misc: minify bundles with terser, emit source map Oct 4, 2019

@paulirish paulirish 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.

unfortunately, building the LR bundle now takes ~60 seconds. but there's not much we can do about that.

a few small things, but is good to get a green

*/
afterPass(passContext) {
const expression = `(function() {
const tapTargetsSelector = "${tapTargetsSelector}";

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 looked through the other uses of evalAsync for a similar situation and i think we're good.

only one that caught my eye was L116 of the start-url gatherer

.evaluateAsync(`window.location = '${startUrl}'`)

but looking at the minified source, i think we're ok

image

ya?


in minified land this was that taptarget section:

image

@connorjclark connorjclark Sep 16, 2020

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yea code inside template strings' ${...} are safe

Comment thread build/build-bundle.js
Comment thread package.json
"name": "lighthouse",
"version": "6.3.0",
"description": "Lighthouse",
"description": "Automated auditing, performance metrics, and best practices for the web.",

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.

👍

Comment thread package.json Outdated
Comment thread package.json
"@wardpeet/brfs": "2.1.0-0",
"angular": "^1.7.4",
"archiver": "^3.0.0",
"babel-core": "^6.26.0",

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.

👋 bye!

Comment thread package.json Outdated
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
"browserify": "^16.2.3",
"browserify-banner": "^1.0.15",
"bundlesize": "^0.18.0",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what now

:)

Comment thread build/build-bundle.js Outdated
assert.ok(!minified.includes('\nrequire='), 'contained unexpected browserify require stub');
// Add the banner and modify globals for DevTools if necessary.
// This will mess up the source map for the first line, but we don't ship
// the map to DevTools, so that's fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what does that leave that gets a source map?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I just removed this comment since we aren't shipping any maps in CDT or LR. I put the default map generation back to false. can set to true whenever we want to get a bundle viz.

Comment thread build/build-bundle.js Outdated
Comment thread build/build-bundle.js
Co-authored-by: Brendan Kenny <bckenny@gmail.com>
@connorjclark connorjclark changed the title misc(build): minify bundles with terser, emit source map misc(build): minify bundles with terser Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants