0

I am using webpack to pack my css file ,my webpack version is 2.4.1 ,I follow the official document webconfig method to use loader;

my file list

my webconfig file:

  var path=require("path");
module.exports={
    entry:"./js/main.js",
    output:{
        path:path.join(__dirname,"js/build"),
        filename:"bundle.js"
    },
    module: {
        loaders: [
          {test:/\.css$/,loader:"css-loader!style-loader"},
          {test: /\.js$/, loader: 'jsx-loader'}
        ]
    }
}

main.js

// my entry file main.js
var React=require("react");
var path=require("path")
var ReactDOM=require("react-dom");
var MessageBoard=require("../component/messageBoard");

require("../css/main.css"); //I require the css file there

ReactDOM.render(<MessageBoard title="message"/>,
      document.getElementById("container")
)

the Errorlog: ERR

then,I dont know why,so I try the another way

  module: {
        loaders: [
          //{test:/\.css$/,loader:"css-loader!style-loader"}, I delete this config
          {test: /\.js$/, loader: 'jsx-loader'}
        ]
    }

main.js

var React=require("react");
var path=require("path")
var ReactDOM=require("react-dom");
var MessageBoard=require("../component/messageBoard");

require("style-loader!css-loader!../css/main.css");//use this way

ReactDOM.render(<MessageBoard title="message"/>,
      document.getElementById("container")
)

in this way ,It work well,so I must did something wrong in the first method,thx~

1 Answer 1

2

your loaders are in a reversed order.

change this:

{test:/\.css$/,loader:"css-loader!style-loader"}  

to this:

{test:/\.css$/,loader:"style-loader!css-loader"}

the order of loaders is from right to left.

this works because its in the right order:

require("style-loader!css-loader!../css/main.css");//use this way
Sign up to request clarification or add additional context in comments.

2 Comments

another question,why this order must style-loader before? and how can I know this part of knowledge?
style-loader responsible for embedding the style to the page. css-loader resolve @import and url's etc. you can read more about the difference here stackoverflow.com/a/34237524/3148807. i found out the same way you did, after much time of struggle and then read it somewhere (can't find it now). you can see the docs and comments about the lack of explanation about the importance of the order. webpack.github.io/docs/stylesheets.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.