Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing materialdesignicons package in ReactJS & Webpack

I am trying to add material design icons into my project via the npm "mdi" package. but I have issues.

webpack.config.js

var webpack = require('webpack');
var path = require('path');

var BUILD_DIR = path.resolve(__dirname, 'src/public');
var APP_DIR = path.resolve(__dirname, 'src');

var config = {
  entry: [
      'webpack/hot/dev-server',
      'webpack-dev-server/client?http://localhost:8080/',
      APP_DIR + '/index.jsx'
  ],
  output: {
      path: BUILD_DIR,
      filename: 'bundle.js',
      publicPath : '/'
  },
  devServer : {
      historyApiFallback: true,
      contentBase: 'src/public/'
  },
  module: {
      loaders : [            
          {
              test : /\.jsx?/,
              include : APP_DIR,
              loader : ['react-hot-loader', 'babel-loader']
          },
          {
              test: /\.s?css$/,
              loaders: ["style-loader", "css-loader", "sass-loader"],
          },
          {
              test: /\.(eot|svg|ttf|woff|woff2)(\??\#?v=[.0-9]+)?$/,
              loader: 'file-loader?name=/fonts/[name].[ext]',
          }
      ]
  },
  plugins: [
      new webpack.optimize.OccurrenceOrderPlugin(),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.NoEmitOnErrorsPlugin()
  ],
  resolve: {
      extensions: ['.js', '.jsx']
  }
};

//dev server

module.exports = config;

my App components calls the main scss file

import './scss/App.scss';

then in that App.scss file, I import a settings.scss and the materialdesignicons.scss file

settings.scss

$mdi-font-path: "fonts/";

App.scss

@import "./settings.scss";
@import '~mdi/scss/materialdesignicons.scss';

the project is imported via the main.scss file.

settings.scss

$mdi-font-path: "fonts/";

whenever I try to build, I get an error with it trying to figure out how to handle the webfont items.

ERROR in ./~/css-loader!./~/sass-loader!./src/scss/App.scss
Module not found: Error: Can't resolve './fonts//materialdesignicons-webfont.eot?v=1.9.32' in '/<path>/app/src/scss'
 @ ./~/css-loader!./~/sass-loader!./src/scss/App.scss 7:129-189
 @ ./src/scss/App.scss
 @ ./src/App.jsx
 @ ./src/index.jsx
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server webpack-dev-server/client?http://localhost:8080/ ./src/index.jsx

I thought that the file-loader would handle those files. It seems that maybe since it's importing the scss file, it doesn't use the file loader part, and instead tries to use the sass/css loaders.

I'm trying to figure out how this should be done.

like image 275
Sam Avatar asked Sep 20 '25 12:09

Sam


1 Answers

My approach consists of three steps:

  1. to install material-design-icons package

    npm install material-design-icons
    
  2. to import material-icons.css file into .less or .scss file/ project

    @import "~/node_modules/material-design-icons/iconfont/material-icons.css";
    
  3. to include recommended code into the reactjs .js file/ project

    <i className='material-icons' style={{fontSize: '36px'}}>close</i>
    
like image 111
Roman Avatar answered Sep 22 '25 02:09

Roman