admin管理员组文章数量:1221299
In our original setup of react + webpack, when running npm start we are facing "Can't find stylesheet to import" issue. But this happens only in Windows and not in MacOS.
To replicate this issue on a small codebase, I have created following setup.
In this setup you will get the error when running npm run dev:
ERROR in ./src/main.scss (./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/dist/cjs.js??ruleSet[1].rules[1].use[2]!./src/main.scss) Module build failed (from ./node_modules/sass-loader/dist/cjs.js): Can't find stylesheet to import. ╷ 1 │ @use "D:\Work\Playground\webpack-sass\stylesheets\_utils.scss" as *; │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
package.json:
{
"name": "webpack-sass",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server --open",
"build": "webpack"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@babel/preset-env": "^7.26.0",
"babel-loader": "^9.2.1",
"css-loader": "^7.1.2",
"html-webpack-plugin": "^5.6.3",
"sass": "^1.80.7",
"sass-loader": "^16.0.3",
"style-loader": "^4.0.0",
"webpack": "^5.96.1",
"webpack-cli": "^6.0.1",
"webpack-dev-server": "^5.2.0"
}
}
webpack.config.js
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const sassAdditionalData = `@use "${path.join(__dirname, "./stylesheets/_utils.scss")}" as *;`;
const stylesPath = path.resolve(__dirname, "stylesheets");
module.exports = {
mode: "development",
entry: "./src/index.js",
devServer: {
static: "./dist",
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
},
{
test: /\.scss$/,
use: [
"style-loader",
"css-loader",
{
loader: "sass-loader",
options: {
sassOptions: {
includePaths: [stylesPath],
},
additionalData: sassAdditionalData,
},
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
title: "Development",
}),
],
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist"),
},
};
.babelrc
{
"presets": ["@babel/preset-env"]
}
src/index.js
import "./main.scss";
const foo = (name) => {
console.log(name);
};
foo("Nimish");
main.scss
$body-color: red;
body {
background-color: $body-color;
}
stylesheets/_utils.scss
$body-color-new: green;
I have tried upgrading webpack, sass-loader, adding include paths and referencing with just file name but nothing seems to be working.
Also when I control+click the exact filepath shown in error, it opens the actual file.
So I am not really sure why sass-loader does not find it.
Any help is highly appreciated.
版权声明:本文标题:reactjs - Facing issue of Can't find stylesheet to import when using webpack and sass loader - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739321175a2158041.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论