node.js 如何使用 Express 安装 SASS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14242536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:54:41  来源:igfitidea点击:

How do you install SASS with Express?

node.jsexpresssass

提问by ilyo

I am creating a node.js app with Express and socket.io. I want to use SASS and I see there is a npm package for it, what I don't understand is how do I link between the SASS npm and the app and make it parse the SASS?

我正在使用 Express 和 socket.io 创建一个 node.js 应用程序。我想使用 SASS 并且我看到它有一个 npm 包,我不明白的是如何在 SASS npm 和应用程序之间进行链接并使其解析 SASS?

UPDATE: I used SASS middleware https://github.com/andrew/node-sassinstalled it and included it the following way:

更新:我使用了 SASS 中间件https://github.com/andrew/node-sass安装了它并以以下方式包含它:

  sass = require('node-sass');


app.configure(function(){
  app.set('port', process.env.PORT || 3000);

  /* other stuff */

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

But it still doesn't work

但它仍然不起作用

回答by soulcheck

You need to use the sass middleware, for example this one.

您需要使用 sass 中间件,例如this one

Quoting from docs:

引用文档:

var server = connect.createServer(
   sass.middleware({
       src: __dirname
       , dest: __dirname + '/public'
       , debug: true
    }),
   connect.static(__dirname + '/public')
);

in case of using express, just add:

如果使用快递,只需添加:

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

to your app.configure()call.

到你的app.configure()电话。

Of course on production systems it's a better idea to precompile sass to css.

当然,在生产系统上,将 sass 预编译为 css 是一个更好的主意。

update

更新

In the example above the middleware will look for sassfiles in __dirname + '/sass/css'. Also by default it looks for files with .scssextension. There doesn't seem to be an option to change the extension.

在上面的示例中,中间件将sass__dirname + '/sass/css'. 此外,默认情况下,它会查找带有.scss扩展名的文件。似乎没有更改扩展名的选项。

回答by Devendra Madheshiya

If you are using express-generator Then try

如果您使用的是 express-generator 然后尝试

express --view=ejs --css=sass

回答by swervo

Here is a solution based on various sources including the threads/comments above:

这是基于各种来源的解决方案,包括上面的线程/评论:

node:

节点:

var connect = require('connect');
var sass = require('node-sass');

var srcPath = __dirname + '/sass';
var destPath = __dirname + '/public/styles';

var server = connect.createServer(
    sass.middleware({
        src: srcPath,
        dest: destPath,
        debug: true,
        outputStyle: 'expanded',
        prefix: '/styles'
    }),
    connect.static(__dirname + '/public')
);

html:

html:

<!DOCTYPE html>
    <html>
    <head>
         <link rel="stylesheet" type="text/css" href="styles/main.css">
etc

file system:

文件系统:

rootDirectory / server.js (this is the node app)

rootDirectory / server.js(这是节点应用程序)

rootDirectory / public / styles / (this is where the compiled scss files will appear)

rootDirectory/public/styles/(这是编译后的scss文件出现的地方)

rootDirectory / sass / main.scss

根目录/sass/main.scss

This works for me and I've forked the example at:

这对我有用,我在以下位置分叉了示例:

node-sass-example

节点 sass 示例

here:

这里:

node-sass-example using prefix

node-sass-example 使用前缀

回答by Adam Grant

Looks like implementation has changed a bit for Express. I had to do this instead:

看起来 Express 的实现有所改变。我不得不这样做:

npm install node-sass-middleware --save

then

然后

var sass = require('node-sass-middleware');

 app.use(
     sass({
         src: __dirname + '/sass',    // Input SASS files
         dest: __dirname + '/public', // Output CSS
         debug: true                
     })
 );