Rob Bauer
Rob Bauer

Reputation: 847

How to specify an output filename with the Webpack CLI?

I want to create a file named background.js inside my build folder. This is the command I use:

webpack --mode production ./src/background -o ./build/background.js

But this creates a folder named background.js inside my build folder. I didn't find any other flag on the webpack-cli documentation. So how do I specify an output filename with the Webpack CLI?

Upvotes: 5

Views: 3392

Answers (2)

sham
sham

Reputation: 86

in the webpack.config.js folder add this on the top,

const path = require("path");

then your Module exports will be:

module.exports = {
module: {
    rules: [ 
        // your rules, eg, babel loder.
    ],
},
    // the main solution
entry: "./src/index.js", // your entry file that you want it to be bundled for production
output: { // the output file path,
    path: path.resolve(__dirname, "build"),// build is your folder name.
    filename: "background.js", // your specific filename to be built in the build folder.
}, };

and finally your package.json build script will be

"build": "webpack --mode production",

Upvotes: 1

Jay Kariesch
Jay Kariesch

Reputation: 1482

You could try --output-filename: https://v4.webpack.js.org/api/cli/#output-options

webpack --mode production --entry src/background/index.js --output-filename background.js -o ./build

Per the docs, here's a list of core flags from v4 that are supported in v5: https://github.com/webpack/webpack-cli/tree/next/packages/webpack-cli#webpack-5

Upvotes: 7

Related Questions