Node-Sass |除非我手动删除css文件,否则不重新编译

时间:2013-12-26 07:18:53

标签: css node.js sass

我刚刚开始使用node-sass。我已经到了我要求style.css的地步。如果我的样式表目录中没有style.css,那么它会为我编译它,但是一旦编译它就不会更新/重新编译更改,直到我手动删除css文件。

非常感谢任何帮助。

app.js

/**
 * Module dependencies.
 */

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

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

app.use(
    sass.middleware({
     src: __dirname + '/public/sass', //where the sass files are 
     dest: __dirname + '/public', //where css should go
     debug: true, // obvious
     outputStyle: 'compressed'
    })
);

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

// 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'));
});

1 个答案:

答案 0 :(得分:7)

您需要切换静态和SASS中间件的声明(也就是说,静态中间件应该在sass中间件之后 - 请参阅this answer以获取更多信息):

// Notice we are removing this line:
// app.use(express.static(path.join(__dirname, 'public')));

app.use(
  sass.middleware({
   src: __dirname + '/public/sass', //where the sass files are 
   dest: __dirname + '/public', //where css should go
   debug: true, // obvious
   outputStyle: 'compressed'
  })
);
app.use(express.static(path.join(__dirname, 'public')));

否则,当您请求CSS文件并且它已经存在时,静态中间件将处理它并且请求永远不会到达SASS中间件。