1

I'm using pug and scss with webpack 5. I have kept main.js and main.css both bundled by webpack but still the path is not resolved tried specific js path in src folder but same thing, they are not loading.

structure is like:

  • src:
    • index.js
  • style:
    • index.scss
  • views:
    • pages:
      • home.pug
    • base.pug

Please help it out

error picture

webpack.config.js:-

const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCSSExtractPlugin = require('mini-css-extract-plugin')
const path = require('path')

const IS_DEVELOPMENT = 'dev'

module.exports = {
entry: [
    path.resolve(__dirname, 'src/index.js'),
    path.join(__dirname, 'styles/index.scss')
],
output:
{
    filename: 'bundle.[contenthash].js',
    path: path.resolve(__dirname, 'dist')
},
devtool: 'source-map',
plugins:
[
    new CopyWebpackPlugin({
        patterns: [
            { from: path.resolve(__dirname, 'static') }
        ]
    }),
    new HtmlWebpackPlugin({
        filename: 'index.html',
        template: path.resolve(__dirname, './views/pages/home.pug'),
        minify: true
    }),
    new MiniCSSExtractPlugin({
        filename: IS_DEVELOPMENT ? '[name].css' : '[name].[contenthash].css',
        chunkFilename: IS_DEVELOPMENT ? '[id].css' : '[id].[contenthash].css',
    })
],
module:
{
    rules:
    [
        
        {
            test: /\.(html)$/,
            use: ['html-loader']
        },
        {
            test: /\.(pug)$/,
            use: ['html-loader', 'pug-html-loader']
        },
        {
            test: /\.js$/,
            exclude: /node_modules/,
            use:
            [
                'babel-loader'
            ]
        },
        
        {
            test: /\.css$/,
            use:
            [
                MiniCSSExtractPlugin.loader,
                'css-loader'
            ]
        },

        {
            test: /\.scss$/,
            use:
            [
                {
                    loader: MiniCSSExtractPlugin.loader,
                    options: {
                        publicPath: ''
                    }
                },
                {
                    loader: 'css-loader'
                }, {
                    loader: 'resolve-url-loader'
                }, {
                    loader: 'sass-loader'
                }
            ]
        },

        {
            test: /\.(gltf|glb)$/,
            use:
            [
                {
                    loader: 'file-loader',
                    options:
                    {
                        outputPath: 'assets/models/'
                    }
                }
            ]
        },

        {
            test: /\.(jpg|png|gif|svg)$/,
            use:
            [
                {
                    loader: 'file-loader',
                    options:
                    {
                        outputPath: 'assets/images/'
                    }
                }
            ]
        },

        {
            test: /\.(ttf|eot|woff|woff2)$/,
            use:
            [
                {
                    loader: 'file-loader',
                    options:
                    {
                        outputPath: 'assets/fonts/',
                        name: 'fonts/[name].[ext]',
                        mimetype: 'application/font-woff',
                        publicPath: '../'
                    }
                }
            ]
        },

        {
            test: /\.(glsl|vs|fs|vert|frag)$/,
            exclude: /node_modules/,
            use: [
                'raw-loader',
                'glslify-loader'
            ]
        }
    ]
}
}
  • Maybe you can provide the webpack config ? – Brewal Oct 18 '21 at 18:23
  • updated @Brewal – Dhruval Raval Oct 18 '21 at 18:55
  • @DhruvalRaval you don't have `main.js` `main.css` , check your code or restart the server. The entry points are `index.js` and `index.scss`. Use `style-loader` instead of `MiniCSSExtractPlugin` in development. And you better import the scss files straight into index.js using `import "index.scss"` on top level, `MiniCSSExtractPlugin` will extract the css in a separate file in production. – darklightcode Oct 18 '21 at 19:05
  • Thanks @darklightcode, will try that – Dhruval Raval Oct 18 '21 at 19:07
  • @darklightcode i tried that, js and css both loads but images are broken although the path is correct – Dhruval Raval Oct 18 '21 at 19:22
  • And it's not better to keep everything in one place in the src folder and not have problems like the above. Like everyone else does react, angular, vue etc ;) Find webpack boilerplate on github and it will help you. – Grzegorz T. Oct 18 '21 at 20:13

0 Answers0