I’m having a hard time figuring this out. I have all these node_modules which include the css files I need but I can only get the javascript from the require.
For example require(‘bootstrap’) will only return the js file and not the css.
I’ve tried
require("!style!css!bootstrap/dist/css");
but I just get this error:
ERROR in ./app/js/loader.js
Module not found: Error: Cannot resolve module ‘bootstrap.css’ in C:\Users\Administrator\Dropbox\projects\spDash\app\js
@ ./app/js/loader.js 10:0-24ERROR in ./app/js/loader.js
Module not found: Error: Cannot resolve module ‘bootstrap/dist/css’ in C:\Users\Administrator\Dropbox\projects\spDash\app\js
@ ./app/js/loader.js 19:0-40
My config looks like this:
var webpack = require('webpack')
module.exports = {
entry: {
'spdash': './app/js/loader.js'
},
output: {
filename: 'bundle.js',
},
devtool: 'source-map',
module: {
loaders: [
// the url-loader uses DataUrls.
// the file-loader emits files.
{ test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&minetype=application/font-woff" },
{ test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" },
{
test: /\.css$/, // Only .css files
loader: 'style!css' // Run both loaders
},
{ test: /\.less$/, loader: 'style-loader!css-loader!less-loader' }, // use ! to chain loaders
{ test: /\.css$/, loader: 'style-loader!css-loader' },
{test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'} // inline base64 URLs for <=8k images, direct URLs for the rest
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
minimize: true,
compress: {
warnings: false
}
}),
new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/])
]
};
Try
require("!style!css!bootstrap/dist/css/bootstrap.css")
. Also, you don’t need!style!css!
since you already have that specified in the loaders configuration.I still need help… how can I include the css from a node_modules package??
@ArdentKid it’s same as including a local CSS file. You include npm modules with
require('some-module')
, so if that module has a CSS file somewhere, you would dorequire('some-module/path/to/style.css')
. Make sure you have a CSS loader defined.another option here is to provide a resolve alias
aliasing allows you to rename modules https://webpack.github.io/docs/resolving.html#aliasing
@import 'bourbon'
won’t work because a) instead of the main SCSS,bourbon
hasindex.js
defined asmain
(not sure what thatindex.js
is supposed to be), and b) because modules should be imported with the~
prefix, try@import '~bourbon/core/_bourbon.scss'
.Concerning
normalize-css
, try installing normalize.css instead and doingrequire('normalize.css')
.Let me know if that works.
Thank you @silvenon for replying so quickly; I got it to work!
I put this in the stylesheet at ./src/components/Foo/style.scss:
I am resolving extensions via
resolve.extensions
in ./webpack.config.js:UPDATE:
I can make my life easier with aliases.
./webpack.config.js:
./src/components/Foo/style.scss:
It won’t end up in the
bundle.css
because this is the loader you’re using for CSS:Normalize should end up inside
<style>
tags on your page, please check if that happened.Because you’re using CSS Modules, you’ll want better control over which stylesheets are processed as CSS Modules and which aren’t. I had this problem as well and this is what I’ve come up with, I modified it according to your config:
This way you can add
src/styles/global/_base.scss
, which overrides some things in Normalize.css, and all classes there will be global, without having to do all the:global()
nonsense. React Toolbox is just an example here, in case you ever have to install a module and treat it as CSS Modules.As you said, webpack is not that easy.😕
EDIT: I made a lot of edits to this comment, hopefully you didn’t start reading right away.😅
Thank you for all your help, @silvenon! I adopted the “two loader approach” to match the files with the desired loader. You were right about Normalize being rendered inline, too. Didn’t catch that!
TL;DR
Thank you! If you’d like to see what I did, check out my repository.
Long explanation
Here is a brief summary of my new project setup:
Here is the Webpack config file:
For anyone trying to import node_modules packages from SCSS, prefix with
~
:It’s documented here, but it’s not very obvious.
For any one else who might have trouble with
webpack 4
while usingcss-loader
withcss-modules
and are only usingscss
files, I was able to get this config to work:webpack.dev.js
Example on how to use:
src/styles/globals/globals.scss
src/styles/variables/_variables.scss
src/components/App/App.scss
src/components/App/App.js (
globals.scss
can be imported insrc/index.js
instead)