What is the current best way to handle a CSS entry file in Webpack?

Viewed 98

This should be simple, but I'm kind of stumped over how complex it's turning out to be.

What I would like to do is have a simple reference to my CSS in my Webpack config's entry list:

    entry: {
        'app.js': './src/app/index.js',
        'app.css': './src/app/app.css',
    },
    output: {
        filename: 'web/[name]',
    },

app.css can import other files through the standard CSS syntax: @import url('./morestuff.css'); I would like these to be inlined as Webpack does for JS imports.

I would like that to be exported as normal. For the config above that means outputting it into 'dist/web/app.css'.

I don't want:

  • To import/require CSS in my JS
  • To have it output a JS file that either includes the CSS as a string, or dynamically loads the CSS file itself
  • To do any HTML rewriting

What I've tried/considered:

  • Using css-loader outputs JS instead of CSS.

  • Using css-loader along with mini-css-extract-plugin will import/concat the files as I'd like, but the actual entrypoint output file is JS, and the CSS file it makes is in the wrong folder and renamed to app.css.css. If I try mini-css-extract-plugin without css-loader then it can't handle @ characters (I think it's trying to load the file as JS.)

  • Using the sequence of loaders ['file-loader', 'extract-loader', 'css-loader'] as indicated in the extract-loader documentation, but it behaves as above (except the CSS is output with a hashed filename.)

  • Lots of old SO questions refer to extract-text-webpack-plugin, but that is now an archived project.

  • style-loader is about HTML rewriting, not what I want.

Now this may not be the "proper" way to handle CSS in Webpack, which does seem to be based on importing everything from your JS, but it's surely a common enough pattern. Any ideas?

0 Answers
Related