4

Using style-loader and css-loader in my webpack config , below is an extract from my webpack.config.js

var wpconfig = {
    devtool: "source-map",
    entry : [
        './src/client/index.js'
    ]
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js',
        publicPath: '/assets/'
    },
    module:{
        loaders:[
            { test: /\.(jpg|gif)$/, loader: 'url-loader?limit=10000'},
            { test: /\.css$/, loader: 'style-loader!css-loader'   }

        ]
    },
    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.NoErrorsPlugin()
    ]
};

module.exports = wpconfig ;

When JS is disabled, i can see that the styles are not getting written to the <head> of the document and thus the page is rendered as plain text.

Digging around i heard about extract-text-webpack-plugin , and changing the config a little was able to bundle the *.css files to a single app.css and effectively get it loaded.

Question :

  • Am i right to assume that either style-loader or css-loader is failing to write the style chunks to head when JS is disabled?
  • Is using the extract-text-webpack-plugin a valid solution for this scenario?
  • Are there any other ways to load CSS in such a scenario?

P.S : Just beginning with webpack , so in case of any glaring issues in webpack.config.js , do pointout.

1 Answer 1

3

Using ExtractTextPlugin for production is a valid solution indeed, so if you don't need any chunking, then it'll do fine. Also allows for the best minification of CSS.

I personally use ExtractText for production, and raw style loader in development.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks . But what about the first bit? why doesn't style-loader not work when js is disabled?.
You are correct in your assumption, style-loader uses js to inject style tags - how else would they get there? ;)
Yup. Just had to confirm it :)
I was wondering the same about what happens when JS is disabled. This answers my question.

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.