react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Reccomended way to require CSS in webpack?

Open drobtravels opened this issue 11 years ago • 35 comments

What is the recommended way to bring in the CSS from this packaged in webpack?

Just doing require('react-select') appears to only bring in the javascript. I was able to get it working by adding require('react-select/less/default.less') or require('react-select/dist/default.css') to my javascript file.

I'm new to node packages so I'm not clear if that is by design or not.

drobtravels avatar May 07 '15 22:05 drobtravels

I use the same way...

barsukov avatar May 10 '15 20:05 barsukov

@droberts84 can you show your webpack config

evandegr avatar May 11 '15 19:05 evandegr

See below. Is require('react-select/dist/default.css') not necessary? I looked at package.json and the JS files it references, and didn't see any references to the CSS.

var path = require('path');
var webpack = require('webpack');
var rootPath = path.join(__dirname, '../', '../');

var config = module.exports = {
  // the base path which will be used to resolve entry points
  context: path.join(rootPath, 'frontend'),
  // the main entry point for our application's frontend JS
  entry: './javascripts/bootstrap-app',
};

config.output = {
  // this is our app/assets/javascripts directory, which is part of the Sprockets pipeline
  path: path.join(rootPath, 'app', 'assets', 'javascripts'),
  // the filename of the compiled bundle, e.g. app/assets/javascripts/bundle.js
  filename: 'bundle.js',
  // if the webpack code-splitting feature is enabled, this is the path it'll use to download bundles
  publicPath: '/assets',
  // this all related to source maps
  devtoolModuleFilenameTemplate: '[resourcePath]',
  devtoolFallbackModuleFilenameTemplate: '[resourcePath]?[hash]',
};

config.resolve = {
  // tell webpack which extensions to auto search when it resolves modules. With this,
  // you'll be able to do `require('./utils')` instead of `require('./utils.js')`
  extensions: ['', '.js', '.coffee', '.js.coffee', '.jsx'],
  root: rootPath,
  modulesDirectories: ['node_modules'],
  alias: {
    app: path.join(rootPath, 'frontend', 'javascripts'),
    components: path.join(rootPath, 'frontend', 'javascripts', 'components'),
    flux: path.join(rootPath, 'frontend', 'javascripts', 'flux'),
  }
};

config.resolveLoader = {
  root: path.join(rootPath, "node_modules")
};

config.plugins = [

];

// use jQuery from the rails gem
config.externals = {
  jquery: 'var jQuery'
};

config.module = {
  loaders: [
    { test: /\.coffee$/, loader: 'coffee-loader' },
    { test: /\.es6$/, loader: 'babel-loader' },
    { test: /\.jsx$/, loader: 'babel-loader'},
    // handle stylesheets required from node packages
    { test: /\.css$/, loader: 'style-loader!css-loader'},
    // need to load all react-infinte modules via babel since it contains es6
    { test: /\.js$/, include: path.join(rootPath, 'node_modules', 'react-infinite'), loader: 'babel-loader' },
    // expose flux instance globally as $flux... must use coffe-loader if coffee-script
    { test: path.join(rootPath, 'frontend', 'javascripts', 'flux'), loader: 'expose?$flux!coffee-loader'},
  ],
};

drobtravels avatar May 12 '15 13:05 drobtravels

This should really be included in the documentation.

GabeMedrash avatar Jul 13 '15 20:07 GabeMedrash

my less-loader is failing @import my config has

        {
            test: /\.less$/,
           loader: 'style!css!less'
       }

how exactly should we import this with webpack?

Agamennon avatar Sep 20 '15 08:09 Agamennon

+1 styles not appearing for me without manual require as well

GuruM avatar Sep 25 '15 18:09 GuruM

:+1: thanks for the tip @droberts84

ptim avatar Oct 02 '15 06:10 ptim

Still no proper way to require the CSS. @JedWatson should we separate the CSS entirely and leave it to the user however they want to style it?

sample-usr avatar Oct 07 '15 09:10 sample-usr

I agree that this is not clear from the documentation and should be more explicit.

tsheaff avatar Nov 05 '15 22:11 tsheaff

+1 Had to look for issues to figure out best practices of including CSS, that's how I ended up here. Not obvious.

NeXTs avatar Nov 19 '15 22:11 NeXTs

+1. Not a great initial experience!

jonpincus avatar Nov 29 '15 23:11 jonpincus

I'm using webpack too and I tried doing import 'react-select/scss/default.scss'; however the styles are not getting loaded. Am I missing something?

seeya avatar Dec 27 '15 07:12 seeya

For me it fixed doing import 'style!react-select/scss/default.scss'

felipeleusin avatar Jan 11 '16 12:01 felipeleusin

This one worked for me: require('style!css!sass!react-select/scss/default.scss');

josephtactica avatar Jan 19 '16 12:01 josephtactica

I'm also having the styling issue. Is there an official way to resolve this? It would be greatly appreciated if the styling is mentioned in readme.

ttyao avatar Feb 03 '16 21:02 ttyao

Me too, it would be nice if this was documented, I Thought I'd missed a step, so I guess I'll manually include the file like above.

christhomas avatar Feb 21 '16 22:02 christhomas

This way worked for me: require('react-select/less/default.less') my webpack file has: { test: /\.less$/, loader: 'style!css!less' },

aarkerio avatar Mar 22 '16 22:03 aarkerio

In 1.0.0, the require should be require('react-select/dist/react-select.css').

xie-qianyue avatar Apr 06 '16 14:04 xie-qianyue

If it's not in node_modules and you've installed it with JSPM? @import "react-select/scss/default.scss" doesn't work for me, I also tried putting <link rel="stylesheet" type="text/css" href="jspm_packages/npm/[email protected]/scss/select.scss" /> in my index.html, but it doesnt work either. This really should be documented

conor909 avatar Apr 26 '16 18:04 conor909

import 'react-select/scss/default.scss'; worked for me with this in my webpack config file:

{
    test: /\.scss$/,
    loader: 'style!css!sass?outputStyle=expanded',
}

markmur avatar May 19 '16 18:05 markmur