Comments (6)
Thanks for the context, @nschonni. I am going to close this.
For the curious, I ended up creating a new connect/express middleware from scratch that uses the dart-sass implementation. It looks like this:
const path = require('path')
const fs = require('fs')
const sass = require('sass')
const cache = {}
module.exports = async function renderSass (req, res, next) {
// ignore non-CSS requests
if (!req.path.endsWith('.css')) return next()
// derive SCSS filepath from CSS request path
const file = path.join(process.cwd(), req.path).replace('.css', '.scss')
if (!fs.existsSync(file)) return res.status(404).end()
// cache rendered CSS in memory
if (!cache[req.path]) {
cache[req.path] = sass.renderSync({
file,
includePaths: [path.join(process.cwd(), 'node_modules')],
outputStyle: (process.env.NODE_ENV === 'production') ? 'compressed' : 'expanded'
})
}
res.header('content-type', 'text/css')
res.send(cache[req.path].css.toString())
}
from node-sass-middleware.
Thanks for the context, @nschonni. I am going to close this.
For the curious, I ended up creating a new connect/express middleware from scratch that uses the dart-sass implementation. It looks like this:
const path = require('path') const fs = require('fs') const sass = require('sass') const cache = {} module.exports = async function renderSass (req, res, next) { // ignore non-CSS requests if (!req.path.endsWith('.css')) return next() // derive SCSS filepath from CSS request path const file = path.join(process.cwd(), req.path).replace('.css', '.scss') if (!fs.existsSync(file)) return res.status(404).end() // cache rendered CSS in memory if (!cache[req.path]) { cache[req.path] = sass.renderSync({ file, includePaths: [path.join(process.cwd(), 'node_modules')], outputStyle: (process.env.NODE_ENV === 'production') ? 'compressed' : 'expanded' }) } res.header('content-type', 'text/css') res.send(cache[req.path].css.toString()) }
Many thanks!
Here is a module
version with two changes:
- It searches in
scss
folder for.scss
files to compile - Watches the compiled
.scss
file for changes and clear the cache to recompile it upon next request so you can modify scss files without restarting the app
import path from 'path';
import fs from 'fs';
import sass from 'sass';
const cache = {}
export async function renderSass (req, res, next) {
// ignore non-CSS requests
if (!req.path.endsWith('.css')) return next()
// derive SCSS filepath from CSS request path
const file = path.join(process.cwd(), req.path).replace(/css/g, 'scss'); // search in scss folder
if (!fs.existsSync(file)) return res.status(404).end();
// cache rendered CSS in memory
let rp = req.path;
if (!cache[rp]) {
cache[rp] = sass.renderSync({
file,
includePaths: [path.join(process.cwd(), 'node_modules')],
outputStyle: (process.env.NODE_ENV === 'production') ? 'compressed' : 'expanded'
});
// watch for changes in .scss
fs.watchFile(file, _ => {
delete cache[rp];
fs.unwatchFile(file);
});
}
res.header('content-type', 'text/css');
res.send(cache[req.path].css.toString());
}
from node-sass-middleware.
I forked and put together a quick PR to try replacing with node-sass
with sass
. Most tests are passing. zeke#1
from node-sass-middleware.
I'd suggest looking at the approach that gulp-sass used for changing the compiler option https://github.com/dlmanning/gulp-sass/
Also, dart-sass still used fibers, so you don't really get away from native dependencies
from node-sass-middleware.
Thanks. It looks like fibers
is a devDependency of sass
, not a runtime depenency: https://github.com/sass/dart-sass/blob/4d78316cb7da3f71ffc9901e684349a5e3e5cf28/package.json#L11 -- so for an end-user of sass
there shouldn't be a native dependency there, right?
from node-sass-middleware.
Not mandatory, but recommended for performance reasons https://github.com/sass/dart-sass#javascript-api
from node-sass-middleware.
Related Issues (20)
- Security vulnerability HOT 1
- Sass is compiling in public directory and Sass directory HOT 1
- Publish release 0.12.0 to npm HOT 4
- Confusing documentation HOT 3
- Incorrect path concatenation HOT 3
- Extend the src field so that it can take multiple directory locations HOT 7
- Pre-existing files in the destination assumes there is a source file for it HOT 1
- invalid code loads infinitely
- The code is half-complete, the project is dead HOT 4
- Vulnerability node-sass-middleware > node-sass > meow > trim-newlines
- High Vulnerability in node-sass-middleware > node-sass > node-gyp > tar
- Node v16 compatibility HOT 2
- ChainAlert: npm package release (1.0.0) has no matching tag in this repo HOT 1
- npm install issues (fix)
- Can't install node-sass-middleware with latest node LTS (16.16.0) HOT 2
- Regular expression denial of service in scss-tokenizer HOT 1
- [sass] error: Incompatible units: 'vh' and 'rem'. HOT 1
- node-sass-middleware installation error HOT 1
- Dependency on a vulnerable version of node-sass /request
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-sass-middleware.