【发布时间】:2021-10-08 07:37:27
【问题描述】:
我正在尝试从我的 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' });
}
package.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",
【问题讨论】:
-
你为什么要使用 terser ?直到现在我才听说过。 Terser 辅助的 Webpack (Laravel Mix) 带来了什么劣势?
-
我并没有特别尝试使用任何东西,terser 只是 laravel mix 使用的默认压缩模块。 cf laravel-mix.com/docs/4.0/options 如果您知道另一种有效的方法,我会采用它
-
我不知道如何使用它,也不会修改它,但是阅读source code,我在
src/config.js看到有一个默认配置(也许你想看看它是什么是)。所以任何你想添加或修改的东西,我认为你应该按照它上面的 URL this one 这样你就可以知道在那里直接编辑什么作为terser webpack's config。 -
我认为this 会帮助你。
-
感谢您的指点,但我仍然无法使其正常工作。我看到 terser 正在我的构建终端中运行,但日志仍然存在于最终应用程序中。我清除了我的缓存并重新安装了我的节点模块以确保它没有任何改变。
标签: reactjs laravel webpack laravel-mix terser