简体   繁体   English

如何在 Node.js 中使用 Autoprefixer

[英]How do I use Autoprefixer with Node.js

According to the official documentation, this is simple:根据官方文档,这很简单:

Usage:用法:

var autoprefixer = require('autoprefixer-core');
var postcss      = require('postcss');

postcss([ autoprefixer ]).process(css).then(function (result) {
    result.warnings().forEach(function (warn) {
        console.warn(warn.toString());
    });
    console.log(result.css);
});

However, I'm confused as to what exactly I do to establish the object css to use with process() .但是,我对如何建立对象cssprocess()一起使用感到困惑。 I tried using the result of fs.readfile() but it doesn't seem to be working.我尝试使用fs.readfile()的结果,但它似乎不起作用。 My server module is fairly large, and it might be better to leave out code here.我的服务器模块相当大,最好在此处省略代码。 I really just simply need to know how to create css for the process function.我真的只需要知道如何为 process 函数创建css

I think I've solved your issue.我想我已经解决了你的问题。

You want to read a file into a variable called css and pass css to process() .你想文件读入一个变量,名为css ,并通过cssprocess() The problem lies in which method you use to read the contents of the file.问题在于您使用哪种方法来读取文件内容。

Currently, you use fs.readFile which is asynchronous.目前,您使用的是异步的fs.readFile You are using it as if it were synchronous.您正在使用它,就好像它是同步的一样。 Therefore, you have two options:因此,您有两个选择:

Use fs.readFile the way it is intended to be used, aka: asynchronously:fs.readFile的方式使用fs.readFile ,又名:异步:

var autoprefixer = require('autoprefixer-core');
var postcss      = require('postcss');

function processCSS(file, cb){

  fs.readFile(file, {encoding: String}, function (err, css) { 
      if (err) throw err;
      postcss([ autoprefixer ]).process(css).then(function (result) {
          result.warnings().forEach(function (warn) {
              console.warn(warn.toString());
          });
          console.log(result.css);
          cb( result.css );
      });
  });

}

If you decide to use this, it might be a good idea to learn about promises , which can clean up async code.如果您决定使用它,那么了解promises可能是一个好主意,它可以清理异步代码。

Or instead of fs.readFile , you can use fs.readFileSync which will read the file synchronously.或者代替fs.readFile ,您可以使用fs.readFileSync同步读取文件。 Depending on how large your file is, this isn't the best idea.根据您的文件有多大,这不是最好的主意。

var autoprefixer = require('autoprefixer-core'); 
var postcss      = require('postcss');

function processCSS(file, cb){

  var css = fs.readFileSync(file, {encoding: String});
  postcss([ autoprefixer ]).process(css).then(function (result) {
      result.warnings().forEach(function (warn) {
          console.warn(warn.toString());
      });
      console.log(result.css);
      cb( result.css );
  });

}

Hope this helps!希望这可以帮助!

Answer is: css is the result of fs.readFile() .答案是:cssfs.readFile()的结果。

Silly mistake on my part.我犯了一个愚蠢的错误。 I tried to use the function that they show in the docs, and returned the result of it back to being handled by the file server before the postcss() function (async) had completed.我尝试使用他们在文档中显示的函数,并在postcss()函数(异步)完成之前将其结果返回给文件服务器处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM