Search code examples
ruby-on-railsreactjswebpackcloud9resolve-url-loader

Module build failed from Sass-Loader when setting up Material UI kit React


My environment I've got an RoR app setup with React Native. I use rails server and webpack for my development environment.

attempt to add UI library I added the material ui files as required in the docs to my ReactJS app.

My problem When running my application after adding the Material UI files to my root ReactJS folder

I get the following error when compiling:

Resolve-url-loader settings are incorrect according this error

WARNING in ./app/javascript/components/assets/scss/material-kit-react.scss (./node_modules/css-loader/dist/cjs.js??ref--7-1!./node_modules/postcss-loader/src??ref--7-2!./node_modules/resolve-url-loader??ref--7-3!./node_modules/sass-loader/lib/loader.js??ref--7-4!./app/javascript/components/assets/scss/material-kit-react.scss)
Module Warning (from ./node_modules/resolve-url-loader/index.js):
resolve-url-loader: loader misconfiguration
  "attempts" option is defunct (consider "join" option if search is needed)

Error as domino effect from the incorrect resolve-url-loader settings, citing that sass/scss doesn't work.

ERROR in ./app/javascript/components/assets/scss/plugins/_plugin-react-datetime.scss (./node_modules/css-loader/dist/cjs.js??ref--7-1!./node_modules/postcss-loader/src??ref--7-2!./node_modules/resolve-url-loader??ref--7-3!./node_modules/sass-loader/lib/loader.js??ref--7-4!./app/javascript/components/assets/scss/plugins/_plugin-react-datetime.scss)
Module build failed (from ./node_modules/sass-loader/lib/loader.js):

  border-color: $white-color !important;
               ^
      Undefined variable: "$white-color".
      in /home/ubuntu/workspace/backend/app/javascript/components/assets/scss/plugins/_plugin-react-datetime.scss (line 116, column 17)

I've tried adding the following node libraries to my app


"devDependencies": {
    "css-loader": "^2.1.1",
    "mini-css-extract-plugin": "^0.6.0",
    "node-sass": "^4.11.0",
    "resolve-url-loader": "^3.1.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.30.0",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.2.1"
  }

created and configuration a webpack.config.js in my main root folder

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: './src/styles.scss',

  mode: 'development',

  module: {
    rules: [
      {
        test: /\.(png|jpg|gif)$/,
        include: [
          path.resolve(__dirname, './static/img'),
        ],
        use: {
          loader: 'file-loader',
          options: {
            name: '[name]-[hash].[ext]',
          },
        },
      },
      {
        test: /\.svg$/,
        use: {
          loader: 'svg-inline-loader',
          options: {
            name: '[name]-[hash].[ext]',
          },
        },
      },
      {test: /\/src\/js\/(?:.*)\.css$/, use: [{loader: 'style-loader'}, {loader: 'css-loader'}]},
      {
        test: [/\.scss$/, /\.sass$/],
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              sourceMap: true,
            },
          },
          {
            loader: 'resolve-url-loader',
            options: {
              debug: true,
              root: path.join(__dirname, './static/img'),
              includeRoot: true,
              absolute: true,
            },
          },
          {
            loader: 'sass-loader',
            options: {
              outputStyle: 'compressed',
              sourceMap: true,
              includePaths: [
                './app/javascript/components/assets/scss',
              ],
            },
          },
        ],
      },
    ],
  },

  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/app/javascript/components/assets/scss',
  },

  plugins: [
    new MiniCssExtractPlugin({
      // Options similar to the same options in webpackOptions.output
      // both options are optional
      filename: '[name]-[chunkhash].css',
      chunkFilename: '[name]-[chunkhash].css', // Ensure each chunk has a unique id
    }),
  ],

  devtool: 'source-map'
};

added sass loader to environment.js file

./config/webpack/environment.js

const { environment } = require('@rails/webpacker')

// resolve-url-loader must be used before sass-loader
environment.loaders.get('sass').use.splice(-1, 0, {
  loader: 'resolve-url-loader',
  options: {
    attempts: 1
  }
});

module.exports = environment

I've also tried npm rebuild node-sass

After my attempts to configure webpack and resolve-url-loader I still can't get it to work. I've read trough their respective documents, other questions and solutions, but even resolve-url-loader's own document says that it's difficult to configure.

I expect to be able to compile and process scss/sass files in my app folder

current folder structure

./ (root folder)
  ./Webpack.config.js
  ./Config/webpack/environment
  ./App/javascript ( root folder react app)

please ask me if you need any more information


Solution

  • I had one file where all the other scss while got imported and where connected with one and other. Unfortunately the compiler did not take this into in account and read each file sepertly stating that files where missing so I had to add the import the missing files into each separate scss file, using @import. Sloppy solution but it worked.

    Any better answer can will be marked as answer after mine