1

For the open source site, ReForum, I'm trying to update the site to the latest components (ie: webpack 4, react 16, etc). I started with babel and it upgraded smoothly. Then moved to webpack. I spent more than 10 hours trying various configs until I finally got it to compile using the following:

/** * module dependencies for webpack dev configuration * Proper webpack dev setup: * https://medium.freecodecamp.org/how-to-develop-react-js-apps-fast-using-webpack-4-3d772db957e4 */ const path = require('path'); const webpack = require('webpack'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const autoprefixer = require('autoprefixer'); // define paths const nodeModulesPath = path.resolve(__dirname, '../node_modules'); const buildPath = path.resolve(__dirname, '../public', 'build'); const mainAppPath = path.resolve(__dirname, '../frontend', 'App', 'index.js'); const sharedStylesPath = path.resolve(__dirname, '../frontend', 'SharedStyles'); const componentsPath = path.resolve(__dirname, '../frontend', 'Components'); const containersPath = path.resolve(__dirname, '../frontend', 'Containers'); const viewsPath = path.resolve(__dirname, '../frontend', 'Views'); /** * webpack development configuration */ module.exports = { mode: 'development', target: 'web', devtool: 'inline-source-map', entry: [ 'webpack-hot-middleware/client', mainAppPath, ], output: { filename: 'bundle.js', path: buildPath, publicPath: '/build/', }, plugins: [ new MiniCssExtractPlugin({ // Options similar to the same options in webpackOptions.output // both options are optional filename: '[name].css', chunkFilename: '[id].css', }), new webpack.HotModuleReplacementPlugin(), ], module: { rules: [ { test: /\.js$/, use: [ 'babel-loader' ], exclude: [nodeModulesPath], }, { test: /\.(sa|sc|c)ss$/, use: [ 'style-loader', 'css-loader', // 'postcss-loader', { loader: 'postcss-loader', options: { sourceMap: true, plugins() { return [autoprefixer('last 5 version')]; }, // plugins: () => [require('autoprefixer')({ // 'browsers': ['> 1%', 'last 5 versions'], // })], }, }, 'sass-loader', ], }, { test: /\.(png|jpg)$/, use: ['url-loader?limit=8192'] }, { test: /\.svg$/, use: ['url-loader?limit=10000&mimetype=image/svg+xml'] }, ], }, resolve : { // automatically resolve file extensions (ie import File from '../path/file') extensions: ['.js', '.css'], // alias to call specified folders alias: { SharedStyles: sharedStylesPath, Components: componentsPath, Containers: containersPath, Views: viewsPath, }, }, }; 

Original Webpack 1 dev config

However, the React elements classname disappear, preventing the styles from being applied. It should be like:

expected

But instead is:

bad

Also, the head now have multiple <style>s .

Multiple Unknown styles

Please help me get classnames to reappear and fix the multiple head style elements.


FYI, the only way I was able to get postcss-loader to run is by turning it into an object. It would fail with errors like "Error: No PostCSS Config found in ... "


Update 1:

Tried @noppa and @Alex Ilyaev suggestions the following but it didn't work.

 { test: /\.(sa|sc|c)ss$/, use: [ 'style-loader', // 'css-loader', { loader: 'css-loader', options: { modules: true, loaders: true, importLoaders: 1, localIndentName:'[name]__[local]___[hash:base64:5]', }, }, // 'postcss-loader', { loader: 'postcss-loader', options: { sourceMap: 'inline', options: { ident: 'postcss', plugins: (loader) => [ require('autoprefixer')(), ], }, }, }, ], }, 
9
  • It could be useful to also add the old webpack config. Someone might notice something missing or changed in the new one. Perhaps one of the source files too, to show how the class names are supposed to be linked to elements. Commented Jun 18, 2018 at 0:00
  • @noppa The original config file is shared in the ReForum github. The link is github.com/shoumma/ReForum/blob/master/config/… . Commented Jun 18, 2018 at 0:03
  • 2
    A quick looks shows that you had options for css-loader that are now omitted. Try { loader: 'css-loader', options: { loaders: true, importLoaders: 1, localIndentName: ... } Commented Jun 18, 2018 at 0:07
  • Try plugins: [autoprefixer('last 5 version')] under postcss-loader > options. I'm not sure the function variant is supported here. And @noppa 's comment as well. Commented Jun 18, 2018 at 0:09
  • @noppa Tried your suggestion but it didn't work. Updated question with my changes. Commented Jun 18, 2018 at 0:17

1 Answer 1

2

If you're using Webpack 4, start with no config file - that's right - no config file. WP4 includes sane defaults so most of your non webpack related issues will surface right there.

Regarding the multiple styles block, you need to switch to mini-css-extract-plugin (doesn't support HMR) or extract-css-chunks-webpack-plugin (supports HMR).

Also, note that during dev mode, you'll see multiple style blocks (for HMR). But production build should not have multiple style blocks.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.