Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing vendor css when using webpack + css components

I'm trying to use things like font-awesome and foundation with React and css modules but I can't figure it out how to import them into my project properly.

Note that I'm using HotModuleReplacementPlugin and ExtractTextPlugin to get a single css file.

Somewhere in the top-level component's css file I'd like to be able to just do @import 'font-awesome'; but how can I do it so that the file will be treated as-is and the classes won't end up localised to font_awesome__fa-check and such?

This is my webpack config:

var paths = {
  app: path.join(__dirname, './app/'),
  dist: path.join(__dirname, './dist/'),
  external: path.join(__dirname, './node_modules/'),
}
module.exports = {
  entry: {
    site: [
      'webpack-dev-server/client?http://localhost:5000',
      'webpack/hot/dev-server',
      path.join(paths.app, '/index.js'),
    ]
  },
  output: {
    path: paths.dist,
    publicPath: '/',
    filename: 'scripts/[name].[hash].js',
    chunkFilename: '[name].[chunkhash].js'
  },
  resolve: {
    extensions: ['', '.js'],
    root: paths.app,
    alias: {
      'font-awesome.css': path.join(paths.external, '/font-awesome/css/font-awesome.min.css')
    }
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        loaders: ['babel'],
        include: path.join(__dirname, 'app')
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader?modules&localIdentName=[name]__[local]&importLoaders=1', 'postcss-loader')
      }
    ]
  },
  postcss: [
    require('autoprefixer-core'),
  ],
  devtool: 'inline-source-map',
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new ExtractTextPlugin("styles/[name].[chunkhash].css", {allChunks: true}),
    new HtmlWebpackPlugin({
      template: path.join(paths.app, '/site.html'),
      filename: 'index.html',
      inject: 'body',
      chunks: ['site']
    }),
  ]
};

Thanks.

like image 409
syko Avatar asked Nov 08 '15 18:11

syko


1 Answers

You could leverage include / exclude option when configuring loaders.

E.g. given that all of your custom css is somewhere in the /app/ directory, while foundation and font-awesome are not.

...
module: {
    loaders: [

      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader?modules&localIdentName=[name]__[local]&importLoaders=1', 'postcss-loader'),
        include: path.join(__dirname, 'app')
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader!css-loader'),
        exclude: path.join(__dirname, 'app')
      }
    ]
},

Another option is to use different extension for the files that use css-modules syntax. You could use .mcss.

...
module: {
    loaders: [
      {
        test: /\.mcss$/,
        loader: 'css-loader?modules&localIdentName=[name]__[local]&importLoaders=1'
      },
      {
        test: /\.css$/,
        loader: 'css-loader')
      }
    ],
    postLoaders: [
        {
            test: /\.(css|mcss)$/,
            loader: ExtractTextPlugin.extract('style-loader')
        }
    ]
},
like image 119
lanan Avatar answered Dec 18 '22 00:12

lanan