0

我正在尝试从我的 laravel + react 应用程序的生产版本中删除控制台日志和控制台调试。我找到了许多不同的解决方案,但似乎没有一个适合我的环境。

我仍在尝试了解整个 mix / webpack / terser 管道。

我错过了什么?

webpack.mix.js

let mix = require('laravel-mix');
const TerserPlugin = require('terser-webpack-plugin');

mix.react('resources/assets/js/app.js', 'public/js').version()
  .sass('resources/sass/app.scss', 'public/css').version()
  .copy('resources/assets/img', 'public/assets/img').version()
  .copy('resources/assets/font', 'public/assets/fonts').version()
  .copy('resources/assets/logos', 'public/assets/logos').version()
  .copy('storage/app/public', 'public/storage').version();

mix.webpackConfig({
  resolve: {
    extensions: ['.js', '.jsx', '.scss'],
    modules: [
      'node_modules'
    ]
  },
  // anything below this point is attempts that did not work.
  stats: "errors-only",
  watchOptions: {
    ignored: /node_modules/
  },
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          mangle: true,
          compress: {
            drop_console: true,
            drop_debugger: true
          },
          output: { comments: false, beautify: false }
        },
      }),
    ],
  }
});

if (!mix.inProduction()) {
  mix.webpackConfig({ devtool: 'eval-cheap-module-source-map' });
}

包.json

{
  "scripts": {
    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "npm run development -- --watch",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --disable-host-check --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
4

1 回答 1

0

而不是mix.webpack试试这个:

mix.options({
  terser: {
    terserOptions: {
      mangle: true,
      compress: {
        drop_console: true,
        drop_debugger: true
      },
      output: {
        comments: false,
        beautify: false
      }
    },
  },
});

您必须使用mix.options设置config.js.


这只是我刚刚发现的另一个 SO 帖子:如何从生产中删除 console.log?

于 2021-08-03T05:18:36.250 回答