簡體   English   中英

如何使用 Webpack CLI 指定 output 文件名?

[英]How to specify an output filename with the Webpack CLI?

我想在我的build文件夾中創建一個名為background.js的文件。 這是我使用的命令:

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

但這會在我的構建文件中創建一個名為background.js的文件夾。 我在webpack-cli 文檔中沒有找到任何其他標志。 那么如何使用 Webpack CLI 指定 output 文件名?

你可以試試--output-filenamehttps://v4.webpack.js.org/api/cli/#output-options

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

根據文檔,這是 v5 中支持的 v4 核心標志列表: https://github.com/webpack/webpack-cli/tree/next/packages/webpack-cli#webpack-5

webpack.config.js文件夾的頂部添加這個,

const path = require("path");

那么您的模塊導出將是:

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.
}, };

最后你的package.json構建腳本將是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM