8000 Improve minification · Issue #10 · GoogleChromeLabs/dark-mode-toggle · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

Improve minification #10

Closed
tomayac opened this issue Oct 3, 2019 · 11 comments
Closed

Improve minification #10

tomayac opened this issue Oct 3, 2019 · 11 comments

Comments

@tomayac
Copy link
Member
tomayac commented Oct 3, 2019

The minification of template strings with terser currently is suboptimal, the problem being strings with repeated spaces and line breaks ending up in the minified code (excerpt):

`\n<style>\n*,\n::before,\n::after {\n  box-sizing: border-box;\n}\n\n:host {\n  […]`

This is most probably me holding terser wrong. Essentially I'm looking for something like babel-plugin-minify-template-strings, but for terser. Help wanted.

CC: @RReverser @mathiasbynens

@mathiasbynens
Copy link
Member

You don't just want minify-template-strings (i.e. just removing whitespace), you want minify-css-in-template-strings. I don't know of an existing tool that does this.

Some other ideas to reduce minified bundle size: #11

@mathiasbynens
Copy link
Member

Is this necessary?

:host([hidden]) {
  display: none;
}

I thought this was the default but am likely missing something.

@mathiasbynens
Copy link
Member

Another thing that jumps out is this. occurs more than 150 times in the minified source. We could largely reduce that number by doing something like const _this = this; at the top of the constructor. _this can then be minified just like any other identifier.

Although this likely wouldn't reduce the gzip/brotli size, it would reduce the number of bytes that need to be parsed and compiled.

@tomayac
Copy link
Member Author
tomayac commented Oct 4, 2019

Is [:host([hidden]) {…}] necessary?

Yes, it's a best practice.

@tomayac
Copy link
Member Author
tomayac commented Oct 4, 2019

You don't just want minify-template-strings (i.e. just removing whitespace), you want minify-css-in-template-strings. I don't know of an existing tool that does this.

Eagerly awaiting CSS modules that will make this so much better.

@mathiasbynens
Copy link
Member

You don't just want minify-template-strings (i.e. just removing whitespace), you want minify-css-in-template-strings. I don't know of an existing tool that does this.

Eagerly awaiting CSS modules that will make this so much better.

We could hack something together ourselves. The template literal's contents would be stored in a separate source file in the repository, and processed by a custom script that strips whitespace and applies some simple CSS optimizations. The result would then be injected as a template literal into the built .js file. The downside is that the source *.js file would no longer be useful for testing; you'd have to create a build just to get a working file. It's definitely a trade-off... Maybe it's worth it though.

Happy to prototype this if this doesn't sound too horrible to you.

@tomayac
Copy link
Member Author
tomayac commented Oct 4, 2019

We could hack something together ourselves.

Sure, but at this point you'd build a build system. I really preferred if we instead chimed in on making CSS Modules a thing rather sooner than later. If you could review their proposal and comment on it, this would be a better use of your brain in my opinion.

@tomayac
Copy link
Member Author
tomayac commented Oct 4, 2019

We could largely reduce that number by doing something like const _this = this; at the top of the constructor. _this can then be minified just like any other identifier.

Great idea, thanks. Could you add this to your PR? You can test the changes by running npm run start and navigating to http://localhost:8080/demo (you have probably figured this out, but just thought I'd point it out). Will add this to the README as well.

@mathiasbynens
Copy link
Member

Great idea, thanks. Could you add this to your PR?

Tried it, but turns out not to save anything: #11 (comment) So didn't add it to the PR.

Thanks for the pointer, I have been testing the changes!

@tomayac
Copy link
Member Author
tomayac commented Oct 4, 2019

Also added the instructions to the README.

@tomayac
Copy link
Member Author
tomayac commented Oct 4, 2019

Closed via #11. 🎉

@tomayac tomayac closed this as completed Oct 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0