Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
esbuild-sass-plugin
Advanced tools
esbuild plugin for sass/scss files supporting both css loader and css result import (lit-element)
The esbuild-sass-plugin is an npm package that integrates Sass/SCSS compilation into the esbuild bundler. It allows developers to seamlessly compile Sass/SCSS files into CSS during the build process, leveraging the speed and efficiency of esbuild.
Basic Sass/SCSS Compilation
This feature allows you to compile Sass/SCSS files into CSS as part of the esbuild process. The code sample demonstrates how to set up esbuild with the esbuild-sass-plugin to bundle JavaScript and compile Sass/SCSS files.
const esbuild = require('esbuild');
const sassPlugin = require('esbuild-sass-plugin').sassPlugin;
esbuild.build({
entryPoints: ['src/index.js'],
bundle: true,
outfile: 'dist/bundle.js',
plugins: [sassPlugin()]
}).catch(() => process.exit(1));
Custom Sass Options
This feature allows you to pass custom options to the Sass compiler. The code sample shows how to configure the plugin to include additional paths and enable indented syntax for Sass files.
const esbuild = require('esbuild');
const sassPlugin = require('esbuild-sass-plugin').sassPlugin;
esbuild.build({
entryPoints: ['src/index.js'],
bundle: true,
outfile: 'dist/bundle.js',
plugins: [sassPlugin({
includePaths: ['./src/styles'],
indentedSyntax: true
})]
}).catch(() => process.exit(1));
Source Maps
This feature enables source map generation for easier debugging of Sass/SCSS files. The code sample demonstrates how to configure esbuild to generate source maps along with the compiled CSS.
const esbuild = require('esbuild');
const sassPlugin = require('esbuild-sass-plugin').sassPlugin;
esbuild.build({
entryPoints: ['src/index.js'],
bundle: true,
outfile: 'dist/bundle.js',
sourcemap: true,
plugins: [sassPlugin()]
}).catch(() => process.exit(1));
The 'sass' package is a standalone Sass compiler that can be used with various build tools. Unlike esbuild-sass-plugin, it does not integrate directly with esbuild but can be used in conjunction with other tools to achieve similar results.
The 'node-sass' package is another popular Sass compiler that uses the LibSass library. It is similar to the 'sass' package but is often used in older projects. It does not integrate directly with esbuild but can be used with other build tools.
The 'sass-loader' package is a loader for webpack that compiles Sass/SCSS files. It is similar in functionality to esbuild-sass-plugin but is designed specifically for webpack rather than esbuild.
A plugin for esbuild to handle sass & scss files.
css loader
constructable stylesheet
modules or dynamic style
added to main pagenpm i esbuild-sass-plugin
Just add it to your esbuild plugins:
import {sassPlugin} from "esbuild-sass-plugin";
await esbuild.build({
...
plugins: [sassPlugin()]
});
this will use loader: "css"
and your transpiled sass will be included in index.css.
If you specify type: "style"
then the stylesheet will be dynamically added to the page.
If you want to use the resulting css text as a string import you can use type: "css-text"
await esbuild.build({
...
plugins: [sassPlugin({
type: "css-text",
... // other options for sass.renderSync(...)
})]
});
...and in your module do something like
...
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];
}
}
Or you can import a lit-element css result using type: "lit-css"
...
import styles from "./styles.scss";
...
@customElement("hello-world")
export default class HelloWorld extends LitElement {
static styles = styles
render() {
...
}
}
Look in the test
folder for more usage examples.
The options passed to the plugin are a superset of the sass Options.
Option | Type | Default |
---|---|---|
cache | boolean or Map | true |
type | string or array | "css" |
implementation | string | "sass" |
transform | function | undefined |
If you want to have different loaders for different parts of your code you can pass type
an array.
Each item is going to be:
css
, css-text
, lit-css
or style
)e.g.
await esbuild.build({
...
plugins: [sassPlugin({
type: [ // this is somehow like a case 'switch'...
["css", "bootstrap/**"], // ...all bootstrap scss files (args.path)
["style", ["src/nomod/**"]], // ...all files imported from files in 'src/nomod' (args.importer)
["style", ["**/index.ts","**/*.scss"]], // all scss files imported from files name index.ts (both params)
["lit-css"] // this matches all, similar to a case 'default'
],
})]
})
NOTE: last type applies to all the files that don't match any matchers.
async (css:string, resolveDir:string?) => string
It's function which will be invoked before passing the css to esbuild or wrapping it in a module.
The simplest use case is to invoke PostCSS like this:
const postcss = require("postcss");
const autoprefixer = require("autoprefixer");
const postcssPresetEnv = require("postcss-preset-env");
esbuild.build({
...
plugins: [sassPlugin({
async transform(source, resolveDir) {
const {css} = await postcss([autoprefixer, postcssPresetEnv({stage:0})]).process(source);
return css;
}
})]
});
But it can be used to invoke esbuild to do some post processing of the css like in this example where I rely on esbuild to create data urls:
await esbuild.build({
entryPoints: ["./src/index.ts"],
outdir: "./out",
bundle: true,
format: "esm",
plugins: [sassPlugin({
type: "lit-css",
async transform(css, resolveDir) {
const {outputFiles:[out]} = await esbuild.build({
stdin: {
contents: css,
resolveDir,
loader: "css"
},
bundle: true,
write: false,
format: "esm",
loader: {
".eot": "dataurl",
".woff": "dataurl",
".ttf": "dataurl",
".svg": "dataurl",
".otf": "dataurl"
},
});
return out.text;
}
})]
});
I am not really happy with this but I am waiting on esbuild to revamp its plugin api before finding another way to achieve the same result.
Look at open-iconic test fixture for a working example.
Remember to add the dependency
npm i esbuild-sass-plugin node-sass
and to specify the implementation in the options:
await esbuild.build({
...
plugins: [sassPlugin({
implementation: "node-sass",
... // other options for sass.renderSync(...)
})]
});
It greatly improves the performance in incremental builds or watch mode.
It has to be enabled with cache: true
in the options.
You can pass your own map instead of true if you want to recycle it across different builds.
const pluginCache = new Map();
await esbuild.build({
...
plugins: [sassPlugin({cache: pluginCache})],
...
})
Given 24 x 24 = 576 lit-element files & 576 imported css styles
initial build: 2.033s
incremental build: 1.199s (one ts modified)
incremental build: 512.429ms (same ts modified again)
incremental build: 448.871ms (one scss modified)
incremental build: 448.92ms (same scss modified)
initial build: 1.961s
incremental build: 1.986s (touch 1 ts)
incremental build: 1.336s (touch 1 ts)
incremental build: 1.069s (touch 1 scss)
incremental build: 1.061s (touch 1 scss)
initial build: 1.030s
incremental build: 468.677ms (one ts modified)
incremental build: 347.55ms (same ts modified again)
incremental build: 401.264ms (one scss modified)
incremental build: 364.649ms (same scss modified)
MIT
FAQs
esbuild plugin for sass/scss files supporting both css loader and css result import (lit-element)
The npm package esbuild-sass-plugin receives a total of 111,611 weekly downloads. As such, esbuild-sass-plugin popularity was classified as popular.
We found that esbuild-sass-plugin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.