通过 npm 导入 Sass [英] Importing Sass through npm
问题描述
目前在我们的 Sass 文件中,我们有如下内容:
Currently in our Sass files we have something like the following:
@import "../../node_modules/some-module/sass/app";
这很糟糕,因为我们实际上不确定路径:它可能是 ../node_modules
,也可能是 ../../../../../node_modules
,因为 npm 安装东西的方式.
This is bad, because we're not actually sure of the path: it could be ../node_modules
, it could be ../../../../../node_modules
, because of how npm installs stuff.
在 Sass 中有没有一种方法可以搜索直到找到 node_modules?或者甚至是通过 npm 包含 Sass 的正确方法?
Is there a way in Sass that we can search up until we find node_modules? Or even a proper way of including Sass through npm?
推荐答案
如果您正在寻找 2017 年的方便答案并且正在使用 Webpack,这是我找到的最简单的方法.
If you are looking for a handy answer in 2017 and are using Webpack, this was the easiest I found.
假设你的模块路径是这样的:
Suppose your module path is like:
node_modules/some-module/sass/app
然后在你的主 scss 文件中你可以使用:
Then in your main scss file you can use:
@import "~some-module/sass/app";
波浪号运算符应将任何导入解析为模块.
Tilde operator shall resolve any import as a module.
这篇关于通过 npm 导入 Sass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!