gpt4 book ai didi

node.js - 让 SASS 使用 NodeJS Express 和 node-sass 自动编译

转载 作者:IT老高 更新时间:2023-10-28 22:01:30 25 4
gpt4 key购买 nike

我正在使用 node.js 进行开发,而不是编写 css,而是想编写每当我刷新页面时自动编译的 SCSS 文件。

在使用 NodeJS、Express 和 node-sass 时如何让 SASS 文件自动编译。

最佳答案

更新(2014 年 12 月 7 日)

来自 node-sass 的连接中间件已提取到node-sass-middleware ,见 this answer


安装 node-sass

在你的项目文件夹中运行:

$ npm install node-sass

修改 app.js

假设您使用

生成了您的应用程序
$ express my_app

您的 app.js 应该看起来像这样:

var express = require('express'),
routes = require('./routes');

var app = module.exports = express.createServer();

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

....

以下是修改:

var express = require('express'),
routes = require('./routes'),
sass = require('node-sass'), // We're adding the node-sass module
path = require('path'); // Also loading the path module

var app = module.exports = express.createServer();

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);

// notice that the following line has been removed
// app.use(express.static(__dirname + '/public'));

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

// The static middleware must come after the sass middleware
app.use(express.static( path.join( __dirname, 'public' ) ) );
});

需要注意的是

app.use( sass.middleware ... );

必须先于

app.use( express.static ... )

原因是我们首先希望 sass 编译任何已更改的 sass 文件,然后才提供它们(由 express.static 完成)。

重启你的应用

您必须重新启动应用才能使这些更改生效。

在某处包含它以便编译

我们现在可以在 /sass 文件夹中包含 app.scss。但它还不会编译。 sass 中间件只会编译应用程序请求的文件,因此我们必须在某处包含(要渲染的)css 文件,例如在 `/views/layout.jade' 中:

doctype html
html(lang="en")
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
link(rel="stylesheet", href="app.css") < we've added this
body!= body `/views/layout.jade`

请注意,与 stylesheets 子文件夹下的 style.css 不同,app.css 是从根目录读取的(在本例 /public)。

修复路径

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

第一次编译后,文件和文件夹层次结构如下:

Project folder
app.js
public
app.css < This is where the compiled file goes
sytlesheets
style.css
sass
app.scss < This is where the sass file is

您可能希望将编译后的输出放在 stylesheets 文件夹中,而不是 public 文件夹中;像这样:

Project folder
app.js
public
sytlesheets
app.css
style.css
sass
app.scss

这样, View 将链接到 css 文件,如下所示:

link(rel='stylesheet', href='/stylesheets/style.css')
link(rel="stylesheet", href="/stylesheets/app.css")

但是,如果将 sass 中间件配置更改为

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

事情会变成梨形。

当像这样链接到 css 文件时:

link(rel="stylesheet", href="stylesheets/app.css")

生成的请求将针对 stylesheets/app.css。但是由于我们为 sass 中间件提供了以下配置:

src: __dirname + '/sass',

它会去寻找/sass/stylesheets/app.scss,但不存在这样的文件。

一种解决方案是保持配置不变,但将所有 sass 文件放在子文件夹 `/sass/stylesheets/.但有更好的解决方案。

如果你像这样定义前缀配置:

app.use(
sass.middleware({
src: __dirname + '/sass',
dest: __dirname + '/public/stylesheets',
prefix: '/stylesheets', // We've added prefix
})
);

它将告诉 sass 编译器请求文件将始终以 /stylesheets 为前缀,并且应忽略此前缀,因此对于 /stylesheets/app.css 的请求>,sass 中间件将查找文件 /sass/app.scss 而不是 /sass/stylesheets/app.scss

最终代码

app.js

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

var app = module.exports = express.createServer();

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);

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

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

});

layout.jade

doctype html
html(lang="en")
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
link(rel="stylesheet", href="/stylesheets/app.css")
body!= body

文件夹和文件

Project folder
app.js
public
sytlesheets
app.css
style.css
sass
app.scss

关于node.js - 让 SASS 使用 NodeJS Express 和 node-sass 自动编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23711897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com