SASS, LESS, CSS without JavaScript in Polymer 3 with Webpack

Jose Javi Asilis
HackerNoon.com
3 min readJul 6, 2018

--

Ever since the beginning, you’d had to write or inject your CSS into HTML, in order to style Polymer Components. At a time, Polymer did include the ability to import external stylesheets, although it was an experimental feature, and was deprecated in favor of style modules. With the recent version of Polymer 3, we ditch HTML (finally, no more silent errors 🎉!) in favor of good ol… er, I mean, good and new JavaScript (ES2015 classes).

Nonetheless, there’s a problem. Within HTML, we could get CSS support with our IDE or Text Editors. Writing them in a JavaScript makes it more difficult to static analyze and autocomplete our code.

Fortunately, since it’s JavaScript, this means that we can tap into its powerful ecosystem, and use the tooling available to us.

polymer-css-loader is a Webpack Loader that creates the JavaScript styling for you, and “makes CSS great again” by just importing the CSS to the Polymer Component JavaScript file.

How to use

Install the loader:

Or

Add it to your Webpack.config file:

Note: You can use either sass-loader or less-loader. Leave polymer-css-loader for last.

And import your css in your JavaScript Component file

That’s it! The name of the file will be used for the <style include=””>

Other Features

Add a custom name for <style include=””>

Add a custom name by adding ?name= at the end of the import (Don’t use quotes “” for the value).

Skip a css file

You can skip a css file by adding ?skip at the end of the import. This will include the CSS into your Webpack Bundle, but it will not be parsed as a Polymer JavaScript styling file.

Skip all the files and explicitly include the ones you need

Useful when you’re combining multiple libraries. This will include the CSS into your Webpack Bundle, but it will not be parsed as a Polymer JavaScript styling file.

Then, you’d use ?include for the ones that you’d like to be parsed by the loader.

--

--

Jose Javi Asilis
HackerNoon.com

Building a legacy by building startups and connecting people through Social Commerce. Always excellent and getting better with 600% of energy. 🔥🔥🔥