3

I'm trying to get Webpack to build Javascript and CSS files to their respective folders. I'm fairly new to Webpack and I can't seem to find the exact plugin that'll help me do this.

Here's what I'm trying to achieve:

dist
   |___js
   |     |__app.js
   |     |__vendor.js
   |
   |___css
   |      |__style.css
   |
   |___index.html

enter image description here

The config file:

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
    mode: "development",

    entry: {
        app: path.resolve(__dirname, "src/js/app.js")
    },

    output: {
        path: path.resolve(__dirname, "dist/js"),
        filename: "[name].[contenthash].bundle.js",
        clean: true
    },

    devtool: "inline-source-map",

    devServer: {
        contentBase: path.resolve(__dirname, "dist"),
        port: 5001,
        open:true,
        hot: true,
        watchContentBase: true
    },

    plugins: [new HtmlWebpackPlugin({
        filename: "main.html",
        template: path.resolve(__dirname, "src/index.html")}), 

        new MiniCssExtractPlugin({
            filename: "main.css"
        })
            ],

    module: {
        rules: [    
            {
                test: /\.js$/,
                exclude: /node_modules/
            },

            {
                test:/\.css$/,
                use: [MiniCssExtractPlugin.loader, "css-loader"]
            }
        ]
    }
}

If I haven't made myself clear, Please feel free to ask me any questions.

3
  • What do you mean with "build Javascript and CSS files"? Commented Aug 10, 2021 at 11:59
  • @Thomas Sabik, Thank you for responding. Essentially, I want all my Javascript and CSS files from my src folder to be bundled and outputted into their respective folders which is under the dist folder meaning the "js" folder should have all the javascript files and the "css" folder should have all the css files. Am I being clear? Commented Aug 10, 2021 at 12:43
  • Didn't know Webpack creates CSS files. Usually the styles are included in the JavaScript modules. Commented Aug 10, 2021 at 20:08

1 Answer 1

4

You can set the filename property that you're passing to MiniCssExtractPlugin to "css/style.css"

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

4 Comments

Thanks for responding but, That doesn't seem to work...
What happens when you try? Does it build the files properly, but not import the css, or something else entirely?
The js files do get built but not the css files. So I added another entry pointing to my css file like so: styles: path.resolve(__dirname, "src/css/style.css"). This does get the job done but, It also adds a "styles.js" file to my js folder :/ Here's the updated config file
After a few more hours of banging my head against the wall.... I realized I was never importing my style.css file into my app.js file. Thank you for your answer, I'll go flush myself down the toilet :)

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.