Stywrap is a modular set of Webpack loaders and utilities to convert stylesheets from various preprocessors (SASS, LESS, Stylus, CSS) into JavaScript strings and optionally inject them into the Shadow DOM of Web Components
- β¨ Supports SCSS, SASS, LESS, Stylus, CSS
- π§± Structured as a Yarn Workspaces monorepo
- π Built-in support for Shadow DOM injection
- π§© One plugin to rule them all β with optional per-preprocessor loaders
- π Written in TypeScript
stywrap/
βββ package.json # Root workspace config
βββ yarn.lock
βββ .yarnrc.yml # Enables Yarn Berry features
βββ tsconfig.base.json # Shared base config
βββ packages/
β βββ core/ # Shared utilities
β β βββ src/
β β βββ package.json
β βββ sass/ # Sass loader
β β βββ src/
β β βββ package.json
β βββ less/ # Less loader
β β βββ src/
β β βββ package.json
β βββ stylus/ # Stylus loader
β β βββ src/
β β βββ package.json
β βββ plugin-webpack/ # Combined Webpack plugin to register all loaders
β β βββ src/
β β βββ package.json
βββ demo/
β βββ lit/ # Example usage with Lit Web Components
β βββ vanilla/ # Vanilla JS Web Component example
βββ README.md- Package usage: π¦ @stywrap/sass
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.scss$/,
use: ['@stywrap/sass'],
}
]
}
}Given that you have an SCSS file like this:
// button.scss
.btn {
padding: 1rem;
}You can now import the SCSS like this:
import styles from './button.scss';
console.log(styles); // => ".btn { padding: 1rem; }"- π Shadow DOM Injection (via π¦ @stywrap/core)
import { injectStylesToShadowDOM } from '@stywrap/core';
import styles from './button.scss';
injectStylesToShadowDOM(shadowRoot, styles);- βοΈ Webpack Plugin (for auto-config across preprocessors)
// webpack.config.js
const StywrapPlugin = require('@stywrap/plugin-webpack');
module.exports = {
plugins: [
new StywrapPlugin({
shadow: true, // Automatically injects styles into Shadow DOM
minify: false, // Optionally minify
})
]
}