A webpack loader to generate JavaScript styles from imported CSS/SCSS files for webcomponents based on Polymers LitElement.
npm install lit-element-scss-loader --save-dev
- LitElement
- Webpack 4 Loaders
- extract-loader
- css-loader
- sass-loader (optional)
- Include
lit-element-scss-loader
in your Webpack config. You will also need to use theextract-loader
if you're usingsass-loader
and/orcss-loader
. Make sure that you putlit-element-scss-loader
at the first position so it will be called last by the webpack loader mechanism.
rules: [
{
test: /\.css|\.s(c|a)ss$/,
use: [
'lit-element-scss-loader',
'extract-loader',
'css-loader',
'sass-loader'
]
}
]
};
- Include your .css or .scss files in your JavaScript:
import {
html,
LitElement
} from 'lit-element';
import stylesFromSCSS from './styles.scss';
import stylesFromCSS from './styles.css';
class MyComponent extends LitElement {
constructor(){
super();
this.text = 'component';
}
static get properties() {
return {
text: {
type: String
},
};
}
static get styles() {
return [stylesFromSCSS, stylesFromCSS];
}
render() {
return html`
<p class="paragraph">This is my <span class="bold">${this.text}</span></p>
`;
}
}
window.customElements.define('my-component', MyComponent);
Writing CSS strings inside javascript files is ridiculous. In doing so you lose:
- autocompletion
- linting
- precompilation features
Also, designers may not want to work inside .js files for adding certain CSS styles. You don't want it that way. With this loader, you simply import your css
or scss
into your lit-component, add the variables to your styles
function and here we go!