简体   繁体   English

Webpack - 为什么我的 bundle.js 文件默认会缩小?

[英]Webpack - Why is my bundle.js file minified by default?

I am learning react, and the online course I am following uses webpack.我正在学习 react,我正在关注的在线课程使用 webpack。 I didn't add any minifying or uglyfying options to my webpack.config but still my bundle.js is minified.我没有在我的 webpack.config 中添加任何缩小或丑陋的选项,但我的 bundle.js 仍然被缩小了。 I am not sure why or how to turn it off.我不确定为什么或如何将其关闭。 I have attached my webpack.config.js and package.json.我附上了我的 webpack.config.js 和 package.json。 Thanks for your help.谢谢你的帮助。

 const path = require('path'); module.exports = { entry: './src/app.js', output: { path: path.join(__dirname, 'public'), filename: 'bundle.js' }, module: { rules: [{ loader: 'babel-loader', test: /\\.js$/, exclude: /node_modules/ }, { test: /\\.s?css$/, use: [ 'style-loader', 'css-loader', 'sass-loader' ] }] }, devtool: 'cheap-module-eval-source-map', devServer: { contentBase: path.join(__dirname, 'public'), historyApiFallback: true } };

 { "name": "expensify", "version": "1.0.0", "description": "learn react", "main": "index.js", "author": "powpowpow", "license": "MIT", "scripts": { "serve": "live-server public", "build": "webpack", "dev-server": "webpack-dev-server" }, "dependencies": { "babel-cli": "^6.26.0", "babel-core": "^6.26.0", "babel-loader": "^7.1.4", "babel-plugin-transform-class-properties": "^6.24.1", "babel-preset-env": "^1.6.1", "babel-preset-react": "^6.24.1", "css-loader": "^0.28.11", "live-server": "^1.2.0", "node-sass": "^4.8.3", "normalize.css": "^8.0.0", "react": "^16.2.0", "react-dom": "^16.2.0", "react-modal": "^3.3.2", "react-router-dom": "^4.2.2", "sass-loader": "^6.0.7", "style-loader": "^0.20.3", "webpack": "^4.2.0", "webpack-cli": "^2.0.13", "webpack-dev-server": "^3.1.1" } }

As you are using webpack 4 正如您使用webpack 4

One of the ways is : 其中一种方法是:

Inside package.json set scripts to either development or production mode package.json中将scripts设置为developmentproduction模式

"scripts": {
  "dev": "webpack --mode development",
  "build": "webpack --mode production"
}

And now when you run npm run dev it will give you bundle.js but not minified . 现在,当你运行npm run dev它会给你bundle.js不会缩小

But when you run npm run build it will give you a minified bundle 但是当你运行npm run build它会给你一个缩小的包

You are using webpack 4 without a mode setting. 您正在使用没有模式设置的webpack 4。 Normally, you should have the following warning: 通常,您应该有以下警告:

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn 
more: https://webpack.js.org/concepts/mode/

Add mode: development in your conf to disable minification and this warning. 添加mode: development在您的conf中mode: development以禁用缩小和此警告。

Webpack 4 minimizes by default. Webpack 4默认最小化。

Try adding this to your module.exports in webpack.config.js . 尝试将此添加到module.exports中的webpack.config.js

optimization: {
    minimize: false,
}

Because you didn't specify the mode in your config file, by default webpack set mode: production .因为您没有在配置文件中指定mode ,所以默认情况下 webpack 设置mode: production
In production mode, the webpack minify the bundle by default.在生产模式下,webpack 默认压缩包。 Read more : https://webpack.js.org/guides/production/#minification阅读更多: https ://webpack.js.org/guides/production/#minification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM