I'm using webpack 3.10.0
in a React project started with create-react-app
. I'm trying to adjust my Webpack configuration so that I can import scss files without relative paths.
For example @import ../../scss_variables/vars.scss
works perfectly fine in myComponent.module.scss
but I'd like to be able to simply use @import scss_variables/vars.scss
. I thought the following configuration would do the trick but after many attempts I haven't been able to get the @import
to work. I'm getting the following error:
Module build failed:
@import "scss_variables/vars.scss";
^
File to import not found or unreadable: scss_variables/vars.scss.
in /path/to/app/src/components/myComponent/myComponent.module.scss (line 1, column 1)
File Structure:
app
-- config
---- webpack.config.dev.js
---- paths.js
-- src
---- components
------ myComponent
-------- myComponent.jsx
-------- myComponent.module.scss
---- scss_variables
------ vars.scss
webpack.config.dev.js
:
const paths = require('./paths');
{
test: /\.module\.scss/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]',
includePaths: [paths.appSrc]
},
},
'sass-loader'
]
},
{
test: /^((?!\.module).)*\.scss/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
includePaths: [paths.appSrc]
},
},
'sass-loader'
]
},
paths.js
:
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
module.exports = { appSrc: resolveApp('src') };