手写笔无法解析@import

问题描述 投票:0回答:1

我遇到了将stylus .styl文件编译为css的问题。如果手写笔文件包含@import,那么我会“找不到@import文件”错误。例如,我有两个简单的手写笔文件:

root
  - specific
     - particularButton.styl
  - button.styl

// --- button.styl ---
.button
  // some styles

// --- specific/particularButton.styl ---
@import "../button.styl"
.particular-button
    // some styles

我正在尝试使用此代码将它们转换为css:

const stylus = require('stylus');
const fs = require('fs');

const filePath = // path to particularButton.styl

stylus(fs.readFileSync(filePath, 'utf8'))
    .set('paths', [
        // path to a folder that contain "button.styl"
    ])
    .render(function(err, css) {
        console.log(err);
        // <some action like> fs.writeFileSync(cssFileName, css);
    })

根据stylus API,我尝试使用.set('path' ...并且没有这个设置。但没有成功。

有人可以帮忙吗?

附:环境:OSX Mohave,节点:6.9.1,npm:6.4.1,手写笔:0.54.5

UPD

问题在于@import "../button.styl"的相对路径。如果我用button.styl的绝对路径替换它就变成了工作。但这似乎是一个非常糟糕的解决方案......

node.js stylus
1个回答
0
投票

好的,我刚刚发现了。

我的问题是我试图在.set()方法中添加错误的路径。可能它有点不清楚,idk。

如果在particularButton.styl中进行相对导入,则需要添加此文件本身的路径。不是导入文件。所以它应该是:

stylus(fs.readFileSync(filePath, 'utf8'))
    .set('paths', [
        // path to a folder that contain "particularButton.styl"
    ])
© www.soinside.com 2019 - 2024. All rights reserved.