Node-sass不是在最新的node / express中自动编译 [英] Node-sass is not auto-compiling in latest node/express

查看:126
本文介绍了Node-sass不是在最新的node / express中自动编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用节点(0.10.15)和express(3.3.4)以及最新的节点sass,我无法获取我的scss文件进行编译。我的app.js文件如下所示:

Using node (0.10.15) and express (3.3.4), along with the latest node-sass, and I can't get my scss files to compile. My app.js file looks like this:

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path')
  , sass = require('node-sass');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

app.use(
  sass.middleware({
    src: __dirname + '/public/sass',
    dest: __dirname + '/public',
    debug: true,
    outputStyle: 'compressed'
  })
);

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

我缺少什么让sass自动编译?

What am I missing to get sass to auto-compile?

如果重要,我正在使用主管监控更改。

If it matters, I'm using supervisor to monitor changes.

推荐答案

中间件按顺序执行它们被附加到应用程序。 node-sass 中间件仅将 scss 文件编译为 css ,它不服务他们。 static 中间件能够提供编译的 css 文件,但是,只要 css 文件尚未编译。如果您切换 static sass 中间件,一切都应按预期工作:

Middlewares are executed in the order they are attached to the app. The node-sass middleware only compiles scss files to css, it does not serve them. The static middleware is able to serve the compiled css files, however, it can’t do so as long as the css files are not compiled yet. If you switch static and sass middlewares, everything should work as expected:

app.use(
  sass.middleware({
    src: __dirname + '/public/sass',
    dest: __dirname + '/public',
    debug: true,
    outputStyle: 'compressed'
  })
);

app.use(express.static(path.join(__dirname, 'public')));

当您要求 /style.css


  1. sass 通知请求一个 css 文件。如果dest文件( __ dirname +'/ public'+'/style.css')是最新的,它什么都不做。否则,它会查找src文件( __ dirname +'/ public / sass'+'/style.scss')并编译它(如果存在)。

  2. static 注意到静态资产的请求。如果请求的文件( path.join(__ dirname,'public')+'/style.css')存在,它将被提供。

  1. sass notices a request for a css file. If the dest file (__dirname + '/public' + '/style.css') is up-to-date, it does nothing. Otherwise, it looks for the src file (__dirname + '/public/sass' + '/style.scss') and compiles it, if it exists.
  2. static notices a request for a static asset. If the requested file (path.join(__dirname, 'public') + '/style.css') does exist, it is served.

这篇关于Node-sass不是在最新的node / express中自动编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆