0

I am trying to migrate my application from Webpack3 to Webpack4. I am facing issue in dev build.SCSS files or not merging in dev build and its not loading.Please find below my webpack dev configuration.I used mini-css-extract-plugin instead of extract-text-webpack-plugin.

const path = require('path');
const paths = require('./paths');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
process.env.NODE_ENV = 'development';
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const data = {
    devtool: 'source-map',
    context: paths.context,
    mode: 'development',
    entry: [
        'react-hot-loader/patch',
        'webpack-dev-server/client?http://0.0.0.0:3030',
        'webpack/hot/only-dev-server',
        paths.appIndexJs
    ],
    output: {
        path: '/',
        filename: 'static/js/[name].bundle.js',
        publicPath: '/',
        jsonpFunction: 'function'
    },
    optimization: {
        namedModules: true,
        splitChunks: {
            cacheGroups: {
                commons: { test: /[\\/]node_modules[\\/]/, name: "vendors", chunks: "all" }
            },
        },
        noEmitOnErrors: true,
        concatenateModules: true
    },
    module: {
        rules: [{
                test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: 'static/images/[name].[ext]'
                    }
                }]
            },
            {
                test: /\.js$/,
                exclude: [/node_modules/],
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            [
                                'latest',
                                {
                                    es2015: {
                                        modules: false
                                    }
                                }
                            ],
                            'react',
                            'react-hmre'
                        ],
                        plugins: [
                            'transform-class-properties', ['transform-object-rest-spread', { useBuiltIns: true }],
                            // Polyfills the runtime needed for async/await and generatorss
                            [
                                'transform-runtime',
                                {
                                    helpers: false,
                                    polyfill: false,
                                    regenerator: true,
                                    moduleName: path.dirname(require.resolve('babel-runtime/package'))
                                }
                            ],
                            [
                                'transform-regenerator',
                                {
                                    // Async functions are converted to generators by babel-preset-latest
                                    async: false
                                }
                            ]
                        ]
                    }
                }]
            },
            {
                test: /\.(sa|sc|c)ss$/,
                exclude: [/node_modules/],
                use: [
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            modules: true,
                            localIdentName: '[name]__[local]',
                            importLoader: 1
                        }
                    },
                    {
                        loader: `postcss-loader`,
                        options: {
                            options: {},
                        }
                    },
                    { loader: "sass-loader" }
                ]
            },
            {
                test: /\.css$/,
                exclude: [/src/],
                include: [/node_modules/],
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader"
                ]
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': '"development"',
            __IMAGE_PATH__: '""',
            __ENV__: '"development"'
        }),
        new webpack.LoaderOptionsPlugin({
            options: {
                postcss: [
                    require('postcss-import')({
                        path: process.cwd() + '/src/styles/',
                        addDependencyTo: webpack
                    }),
                    require('postcss-modules-resolve-from-alias')({
                        css: process.cwd() + '/src/styles'
                    }),
                    require('postcss-mixins'),
                    require('postcss-nested'),
                    require('postcss-cssnext')
                ]
            }
        }),
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new MiniCssExtractPlugin({
            filename: 'static/css/main.css'
        }),
        new CopyWebpackPlugin([{ from: '../src/styles/images', to: 'static/images' }]),
        new CopyWebpackPlugin([{ from: '../public/images', to: 'static/images' }]),
        new webpack.ContextReplacementPlugin(/moment[\\\/]locale$/, /^\.\/(en)$/),
        new HtmlWebpackPlugin({
            inject: true,
            template: paths.appHtml,
            chunksSortMode: 'none',
            basePath: ''
        }),
        new BundleAnalyzerPlugin()
    ],
    resolve: {
        modules: [paths.context, paths.context + '/styles/', 'node_modules']
    },
    devServer: {
        contentBase: paths.context,
        port: 3040,
        hot: true,
        inli`enter code here`ne: false,
        historyApiFallback: true
    }
};
module.exports = data;

Css files are merged in to the Dev build. But SCSS files are not merging. its not loading in the page. Kindly help me to resolve this issue.

Hariharan Subramanian
  • 1,360
  • 2
  • 12
  • 22
  • are you including your styles from your entry-file? – ippi Jun 20 '18 at 04:08
  • I didn't get your questions?. Other css are loading in the page but SCSS is not loading. – Hariharan Subramanian Jun 20 '18 at 13:38
  • 1
    The config tells webpack what to do when it finds various things while digging through your entries. But if you never reference the styles with for example `import '../styles/styles.scss`; or `require('./someotherstyles.scss`);` webpack will never touch them. – ippi Jun 20 '18 at 13:56
  • I would also run a production build without the devServer to see what files webpack are actually spitting out. – ippi Jun 20 '18 at 13:58
  • But the same configuration was working fine when i am using webpack3. After migrated in to webpack4 ,its not working. I mentioned the /styles/styles.scss . in the loaderoptionplugin as well. – Hariharan Subramanian Jun 20 '18 at 14:15
  • I added the Styles in index.js (entry point JS). import 'styles/base.scss'; import 'styles/icon.scss'; – Hariharan Subramanian Jun 20 '18 at 20:32

0 Answers0