![cooltext394785080075403](https://user-images.githubusercontent.com/160981/136289874-26ce7269-ea08-47dd-be31-9bf0ef7a0b8d.png) ![image](https://user-images.githubusercontent.com/160981/150880682-4915c4dd-726b-4fea-8f3b-597d191f05bc.png) [![Build Status][travis-image]][travis-url] A plugin for [esbuild](https://esbuild.github.io/) to handle Sass & SCSS files. ### Features * **PostCSS** & **CSS modules** * support for **constructable stylesheet** to be used in custom elements or `dynamic style` to be added to the html page * uses the **new [Dart Sass](https://www.npmjs.com/package/sass) Js API**. * caching * **url rewriting** * pre-compiling (to add **global resources** to the sass files) ### Breaking Changes * `type` has been simplified and now accepts only a string. If you need different types in a project [you can use more than one instance](https://github.com/glromeo/esbuild-sass-plugin/issues/60) of the plugin. You can have a look at the [**multiple** fixture](https://github.com/glromeo/esbuild-sass-plugin/blob/main/test/fixture/multiple) for an example where **lit CSS** and **CSS modules** are both used in the same app * The support for [node-sass](https://github.com/sass/node-sass) has been removed and for good. Sadly, node-sass is at a dead end and so it's 1.x. * `transform` now is expected to send back the CSS text in contents and anything that has to be default exported in `pluginData`. ### Install ```console $ npm i esbuild-sass-plugin ``` ### Usage Just add it to your esbuild plugins: ```javascript import {sassPlugin} from 'esbuild-sass-plugin' await esbuild.build({ ... plugins: [sassPlugin()] }) ``` ### Options You can pass a series of **options** to the plugin that are a superset of Sass [compile string options](https://sass-lang.com/documentation/js-api/interfaces/StringOptionsWithImporter). \ The following are the options specific to the plugin with their defaults whether provided: | Option | Type | Default | |---------------|---------------------------------------------------------|-----------------------------------------| | filter | regular expression (in Go syntax) | /\.(s[ac]ss|css)$/ | | type | `"css"`
`"style"`
`"lit-css"`
`"css-text"` | `"css"` | | cache | boolean or Map | `true` (there is one Map per namespace) | | transform | function | | | [loadPaths](https://sass-lang.com/documentation/js-api/interfaces/Options#loadPaths) | string[] | [] | | precompile | function | | | importMapper | function | | | cssImports | boolean | false | | nonce | string | | | prefer | string | preferred package.json field | | quietDeps | boolean | false | Two main options control the plugin: `filter` which has the same meaning of filter in [esbuild](https://esbuild.github.io/plugins/#on-load) allowing to select the URLs handled by a plugin instance and then `type` that's what specifies how the css should be rendered and imported. ### `filter` The default filter is quite simple but also quite permissive. When specifying a custom regex bear in mind that this is in [Go syntax](https://pkg.go.dev/regexp/syntax) > If you have URLs in your imports and you want the plugin to ignore them you can't just a filter expression like: `/^(?!https?:).*\.(s[ac]ss|css)$/` because *Go regex engine doesn't support lookarounds* but you can use > **esbuild**'s `external` option to ignore these imports or try a [solution like this one](https://esbuild.github.io/plugins/#on-resolve). You can try to list multiple plugin instances in order so that the most specific RegEx come first: ```javascript await esbuild.build({ ... plugins: [ sassPlugin({ filter: /\.module\.scss$/, transform: postcssModules() }), sassPlugin({ filter: /\.scss$/ }), ], ... }) ``` ### `type` The example in [Usage](#usage) uses the default type `css` and will use esbuild CSS loader so your transpiled Sass will be in `index.css` alongside your bundle. In all other cases `esbuild` won't process the CSS content which instead will be handled by the plugin. > if you want `url()` resolution or other processing you have to use `postcss` like in [this example](https://github.com/glromeo/esbuild-sass-plugin/issues/92#issuecomment-1219209442) **NOTE:** Since version `2.7.0` the `css` type works also with postcss, CSS modules and more in general with any transformation function by keeping an internal cache of CSS chunks (virtual CSS files) importing them in the module wrapping the contents #### `type: "style"` In this mode the stylesheet will be in the javascript bundle and will be dynamically added to the page when the bundle is loaded. #### `type: "css-text"` You can use this mode if you want to use the resulting css text as a string import ```javascript await esbuild.build({ ... plugins: [sassPlugin({ type: "css-text", ... // for the options availanle look at 'SassPluginOptions' in index.ts })] }) ``` ...and in your module do something like ```javascript import cssText from './styles.scss' customElements.define('hello-world', class HelloWorld extends HTMLElement { constructor() { super(); this.attachShadow({mode: 'open'}); this.sheet = new CSSStyleSheet(); this.sheet.replaceSync(cssText); this.shadowRoot.adoptedStyleSheets = [this.sheet]; } } ``` #### `type: "lit-css"` Or you can import a **lit-element** css result using `type: "lit-css"` ```javascript import styles from './styles.scss' @customElement("hello-world") export default class HelloWorld extends LitElement { static styles = styles render() { ... } } ``` Look in `test/fixtures` folder for more usage examples. ### `cache` The cache is enabled by default and can be turned off with `cache: false`. Each plugin instance creates and maintain its own cache (as a Map) and this cache lives for the duration of the build. If you want to pass a Map to preserve the cache amongst subsequent builds bear in mind that sharing the very same cache between different instances might work just fine or it might lead to issues if the contents are incompatible. > If you are not sure of what to do just keep a separate Map for each plugin instance. ### `cssImports` when this is set to `true` the plugin rewrites the node-modules relative URLs starting with the `~` prefix so that esbuild can resolve them similarly to what `css-loader` does. > Although this practice is [kind of deprecated nowadays](https://webpack.js.org/loaders/sass-loader/#resolving-import-at-rules) > some packages out there still use this notation (e.g. `formio`) > \ > so I added this feature to help in cases [like this one](https://github.com/glromeo/esbuild-sass-plugin/issues/74). ### `nonce` in presence of Content-Security-Policy [(CSP)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src) the `nonce` option allows to specify the nonce attribute for the dynamically generated `