question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

support for dart sass version without native dependencies

See original GitHub issue

Hello! First off, thanks for writing and maintaining this library. It’s useful! ✨

I work on an app that uses this middleware. It’s working fine us, but occasionally there are members of my team who run into issues with the native module compilation process that is inherent in the node-sass dependency. Team members are also often alarmed and confused by scary node-gyp output.

I have come to learn that there’s a Dart implementation of sass (https://www.npmjs.com/package/sass) that has no native code or external dependencies. Is it possible to use that implementation with this middleware?

cc @sarahs

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
zekecommented, Mar 15, 2020

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())
}
3reactions
luca-viggianicommented, May 5, 2021

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:

  1. It searches in scss folder for .scss files to compile
  2. 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());
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dart Sass
When installed via npm, Dart Sass supports a JavaScript API that aims to be compatible with Node Sass. Full compatibility is a work...
Read more >
dart-sass - npm
This package is a distribution of Dart Sass, compiled to pure JavaScript with no native code or external dependencies.
Read more >
sass | Yarn - Package Manager
This package is a distribution of Dart Sass, compiled to pure JavaScript with no native code or external dependencies. It provides a command-line...
Read more >
Is there a way to use Dart Sass command line version with ...
I can install dart libraries in the build machine. But is there any webpack plugin which can leverage that and use the machine...
Read more >
dart-sass - npm Package Health Analysis - Snyk
An important project maintenance signal to consider for dart-sass is that it hasn't seen any new versions released to npm in the past...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found