[英]I am trying to import SASS variables into my javascript file
I an attempt to get my .js
file to see SASS variables, I've followed this article .我试图让我的
.js
文件查看 SASS 变量,我已经关注了这篇文章。 This question features an error I get from attempting this method.这个问题的特点是我尝试这种方法时遇到的错误。 But even if you have had success importing SASS variables in javascript in another way, please do share your success with me!
但是,即使您以另一种方式成功地在 javascript 中导入 SASS 变量,也请与我分享您的成功!
I will show you the code I've written and the error I get.我会告诉你我写的代码和我得到的错误。 First the code:
先上代码:
My webpack.config.js
file:我的
webpack.config.js
文件:
module.exports = {
entry: `./sass.js`,
output: {
path: `${__dirname}`,
filename: 'bundle.js'
},
mode: 'development',
// ...
module: {
rules: [
{
test: /\.scss$/,
use: ["style-loader", "css-loader", "sass-loader"]
},
// ...
]
}
}
My .scss
file:我的
.scss
文件:
$kiwi: red;
:export {
kiwi: $kiwi,
}
My .js
file:我的
.js
文件:
import variables from './webpack_sass.scss';
My .html
file (which I've checked does correctly see bundle.js
):我的
.html
文件(我检查过的文件正确地看到了bundle.js
):
<head>
<script type="module" src="./bundle.js"></script>
</head>
I have installed webpack
& webpack-cli
.我已经安装了
webpack
和webpack-cli
。 Here's a picture of my package.json
file:这是我的
package.json
文件的图片:
The error I get is:我得到的错误是:
:export
syntax is a sugar that css-loader
adds, all you need to do is to enable the css-modules feature. :export
语法是css-loader
添加的糖,您需要做的就是启用css-modules功能。
You have 2 choices:您有 2 个选择:
.module.scss
suffix to your file (since css-module is enabled by default for it).module.scss
后缀添加到您的文件中(因为默认情况下启用了 css-module).scss
imports, by specifying.scss
导入启用 css-modules// webpack.config.js
module.exports = {
entry: `./sass.js`,
output: {
path: `${__dirname}`,
filename: 'bundle.js',
},
mode: 'development',
// ...
module: {
rules: [
{
test: /\.scss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: true, // <- this enables css-modules for all imports
},
},
'sass-loader',
],
},
// ...
],
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.