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

时间:2018-03-31 20:43:13

标签: javascript reactjs webpack webpack-dev-server build-tools

我正在学习反应,我关注的在线课程使用webpack。我没有添加任何缩小或丑陋的选项到我的webpack.config但仍然我的bundle.js缩小。我不知道为什么或如何关闭它。我已经附加了我的webpack.config.js和package.json。谢谢你的帮助。

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"
  }
}

3 个答案:

答案 0 :(得分:6)

正如您使用webpack 4

其中一种方式是:

package.json内设置scriptsdevelopmentproduction模式

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

现在当你运行npm run dev时,它会给你bundle.js不会缩小

但是当你运行npm run build时,它会为你提供缩小的捆绑

答案 1 :(得分:2)

您正在使用没有模式设置的webpack 4。通常,您应该有以下警告:

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/

在你的conf中添加mode: development以禁用缩小和此警告。

答案 2 :(得分:2)

默认情况下,Webpack 4最小化。

尝试将其添加到module.exports中的webpack.config.js中。

optimization: {
    minimize: false,
}