首页 > 解决方案 > 控制 webpack 编译进度输出 - Laravel Mix/Envoy

问题描述

通过 Laravel Envoy使用命令npm run development,使每一行输出回来,在控制台中形成数百行这样的行:

[remote@server]: <s> [webpack.Progress] 65% building 145/150 modules 5 active /home/remote/example.com/node_modules/date-fns/sub_years/index.js
[remote@server]: <s> [webpack.Progress] 65% building 146/150 modules 4 active /home/remote/example.com/node_modules/date-fns/sub_years/index.js
[remote@server]: <s> [webpack.Progress] 65% building 147/150 modules 3 active /home/remote/example.com/node_modules/date-fns/sub_years/index.js
[remote@server]: <s> [webpack.Progress] 65% building 147/151 modules 4 active /home/remote/example.com/node_modules/array-includes/index.js
[remote@server]: <s> [webpack.Progress] 65% building 148/151 modules 3 active /home/remote/example.com/node_modules/array-includes/index.js
[remote@server]: <s> [webpack.Progress] 65% building 148/152 modules 4 active /home/remote/example.com/node_modules/vue-google-charts/index.js
[remote@server]: <s> [webpack.Progress] 65% building 148/153 modules 5 active /home/remote/example.com/node_modules/@deveodk/vue-toastr/dist/@deveodk/vue-toastr.js
[remote@server]: <s> [webpack.Progress] 65% building 148/154 modules 6 active /home/remote/example.com/node_modules/pluralize/pluralize.js
[remote@server]: <s> [webpack.Progress] 65% building 148/155 modules 7 active /home/remote/example.com/node_modules/vue-js-modal/dist/index.js
[remote@server]: <s> [webpack.Progress] 65% building 148/156 modules 8 active /home/remote/example.com/node_modules/vuex/dist/vuex.esm.js

有没有办法将进度输出最小化到几行?

标签: laravelwebpackprogress-barlaravel-mixlaravel-envoy

解决方案


这里有几件事很重要:

  • package.json 包含 npm 命令,例如npm run developmentnpm run production
  • 以下是其中之一的样子:

"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",

  • 进度输出由--progress上述命令中的参数启动
  • 进度输出在后台使用ProgressPlugin

这个想法是从命令中删除--progress参数,所以它看起来像

"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",

但是要手动将其实例化为插件,如下所示webpack.mix.js

mix.webpackConfig({
    plugins: [
        new webpack.ProgressPlugin((percentage, message) => {
            // An idea to show the line only if percentage is divisible by 5.
            if (percentage * 100 % 5 === 0) {
                console.log(`${(percentage * 100).toFixed()}% ${message}`);
            }
        })
    ],
}); 

修改后的输出如下所示:

0% compiling
10% building
10% building
25% building
40% building
40% building
70% building
70% building
70% finish module graph
70% finish module graph
75% module optimization
70% building
70% building
80% chunk modules optimization
85% chunk reviving
85% chunk reviving
95% emitting
95% emitting

这个想法是从这篇解释ProgressPlugin 如何工作的文章中借来的


推荐阅读