Run the `compass compile` command by Nodejs

Run the compass compile command by Nodejs

Installation

$ gem install compass
$ npm install node-cc

Usage

var app = require('express')()

app.configure('development', function() {
  // [required] The source directory where you keep your sass stylesheets.
  app.set('--sass-dir', __dirname + '/app/assets/stylesheets')

  // [optional] The target directory where you keep your css stylesheets.
  app.set('--css-dir', __dirname + '/public/stylesheets')

  // [optional] The directory where you keep your images.
  app.set('--images-dir', __dirname + '/public/images')

  // [optional] Require the given ruby LIBRARY before running commands.
  app.set('--require', __dirname + '/lib/ooxx.rb')

  app.use(require('node-cc'))
})

License

Licensed under the MIT License.


2 回复

Run the compass compile Command Using Node.js

In this guide, we will walk through how to run the compass compile command using Node.js. This can be particularly useful for developers who prefer a more integrated development workflow within their Node.js applications.

Installation

First, ensure that you have both Ruby and Node.js installed on your system. Then, install the necessary gems and Node.js packages:

# Install Compass via RubyGems
$ gem install compass

# Install the `node-cc` package via npm
$ npm install node-cc

Usage

To use the compass compile command in a Node.js application, you can set up an Express server and integrate the node-cc middleware. Here’s a step-by-step example:

  1. Set Up Your Express Application

    var express = require('express');
    var app = express();
    
  2. Configure the Compass Settings

    You need to configure the paths for your SASS files, CSS output directory, and other options. These settings are passed through app.set().

    app.configure('development', function() {
      // Set the source directory for your SASS stylesheets
      app.set('--sass-dir', __dirname + '/app/assets/stylesheets');
    
      // Optionally set the target directory for your compiled CSS files
      app.set('--css-dir', __dirname + '/public/stylesheets');
    
      // Optionally set the directory for your images
      app.set('--images-dir', __dirname + '/public/images');
    
      // Optionally load any required Ruby libraries before running commands
      app.set('--require', __dirname + '/lib/ooxx.rb');
    });
    
  3. Integrate node-cc Middleware

    Add the node-cc middleware to your Express application to handle the compilation of SASS files.

    app.use(require('node-cc'));
    
  4. Start Your Server

    Finally, start your Express server to serve your application.

    app.listen(3000, function() {
      console.log('Server is running on port 3000');
    });
    

Putting it all together, your main file (e.g., server.js) would look like this:

var express = require('express');
var app = express();

app.configure('development', function() {
  app.set('--sass-dir', __dirname + '/app/assets/stylesheets');
  app.set('--css-dir', __dirname + '/public/stylesheets');
  app.set('--images-dir', __dirname + '/public/images');
  app.set('--require', __dirname + '/lib/ooxx.rb');
});

app.use(require('node-cc'));

app.listen(3000, function() {
  console.log('Server is running on port 3000');
});

License

This setup is licensed under the MIT License. For more details, refer to the MIT License.

By following these steps, you can seamlessly integrate the compass compile functionality into your Node.js-based web application, allowing you to manage your SASS stylesheets efficiently.


要通过Node.js运行compass compile命令,可以使用node-cc模块来实现。以下是如何配置和使用node-cc来编译Sass文件的步骤。

安装

首先确保已经安装了Compass和node-cc

gem install compass
npm install node-cc

使用

接下来,你可以编写一个简单的Node.js应用来调用compass compile命令。这里展示如何设置和使用node-cc模块。

const express = require('express');
const cc = require('node-cc');

const app = express();

// 设置Sass源目录
app.set('--sass-dir', __dirname + '/app/assets/stylesheets');

// 可选设置CSS输出目录
app.set('--css-dir', __dirname + '/public/stylesheets');

// 可选设置图片目录
app.set('--images-dir', __dirname + '/public/images');

// 可选加载Ruby库
app.set('--require', __dirname + '/lib/ooxx.rb');

// 使用node-cc中间件
app.use((req, res, next) => {
    cc.compile(app.settings, err => {
        if (err) return next(err);
        console.log('Compass compilation completed successfully.');
        next();
    });
});

// 启动Express服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

解释

  1. 引入依赖:首先引入express框架和node-cc模块。
  2. 设置路径:通过app.set()方法设置Sass、CSS和图像目录的路径。
  3. 可选设置:如果有需要,可以设置Ruby库的路径。
  4. 中间件:使用node-cc中间件来执行compass compile命令。当编译完成后,它会打印一条成功消息。
  5. 启动服务:最后,启动Express服务器监听指定端口。

这样,每次请求时都会触发Compass编译过程,从而将Sass文件转换为CSS文件。

回到顶部