简体   繁体   English

webpack 开发模式

[英]webpack for development mode

Hi i am using webpack in react application.嗨,我在 React 应用程序中使用 webpack。 how to differentiate development and production mode in webpack, when npm start dev mode also minifying files i dont want to minify in development mode, Below is my package.json and webpack config file.如何在 webpack 中区分开发和生产模式,当 npm 启动开发模式时也会缩小我不想在开发模式下缩小的文件,下面是我的 package.json 和 webpack 配置文件。 i tried using below code but no use:我尝试使用以下代码但没有用:

optimization: {
   minimize: false
}

, ,

{
  "name": "webpack-react-starter",
  "version": "1.0.0",
  "description": "A Webpack + Babel + React Starter bolerplate ",
  "repository": {
    "type": "git",
    "url": "https://github.com/mlomboglia/webpack-react-starter"
  },
  "keywords": [
    "Autoprefixer",
    "PostCSS",
    "Webpack",
    "React",
    "Babel"
  ],
  "author": "Marcos Lomboglia <marcos.lomboglia@gmail.com> (https://marcoslombog.com)",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --mode development",
    "build": "webpack --mode production"
  },
  "license": "MIT",
  "browserslist": "> 1%,last 2 versions",
  "devDependencies": {
    "@babel/core": "^7.7.5",
    "@babel/preset-env": "^7.7.6",
    "@babel/preset-react": "^7.7.4",
    "autoprefixer": "^9.7.3",
    "babel-loader": "^8.0.6",
    "css-loader": "^3.6.0",
    "file-loader": "^5.0.2",
    "html-webpack-plugin": "^3.2.0",
    "postcss": "^8.1.0",
    "postcss-loader": "^4.0.2",
    "style-loader": "^1.2.1",
    "url-loader": "^3.0.0",
    "webpack": "^4.41.3",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.11.0"
  },
  "dependencies": {
    "babel-plugin-react-css-modules": "^5.2.6",
    "react": "^16.12.0",
    "react-dom": "^16.12.0"
  }
}

and webpack config is below:和 webpack 配置如下:

const path = require('path');
const autoprefixer = require('autoprefixer');
const HtmlWebpackPlugin = require('html-webpack-plugin');

    module.exports = {
        entry: './src/index.js',
        output: {
            path: path.resolve(__dirname, 'dist'),
            filename: 'bundle.js',
            chunkFilename: '[id].js',
            publicPath: ''
        },
        resolve: {
            extensions: ['.js', '.jsx']
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    loader: 'babel-loader',
                    exclude: /node_modules/
                },
                {
                    test: /\.css$/,
                    exclude: /node_modules/,
                    use: [
                        { loader: 'style-loader' },
                        { 
                            loader: 'css-loader',
                            options: {
                                modules: {
                                    localIdentName: "[path]___[name]__[local]___[hash:base64:5]",
                                },                                                      
                                sourceMap: true
                            }
                         },
                         { 
                            loader: 'postcss-loader',
                            options: {
                                postcssOptions: {
                                    plugins: [
                                        [ 'autoprefixer', {}, ],
                                    ],
                                },
                            }
                          }
                    ]
                },
                {
                    test: /\.(png|jpe?g|gif)$/,
                    loader: 'url-loader?limit=10000&name=img/[name].[ext]'
                }
            ]
        },
        plugins: [
            new HtmlWebpackPlugin({
                template: __dirname + '/src/index.html',
                filename: 'index.html',
                inject: 'body'
            })
        ]
    };

Instead of adding development/production mode with webpack cli, how about controlling webpack mode in webpack.config.js with node env?与其使用 webpack cli 添加开发/生产模式,不如使用 node env 在 webpack.config.js 中控制 webpack 模式如何?

Node env can be added with NODE_ENV=production in the script command in package.json. Node env可以在package.json的脚本命令中添加NODE_ENV=production Also NODE_ENV=development is possible. NODE_ENV=development也是可能的。

// webpack.config.js
module.exports = {
  mode: process.env.NODE_ENV,
  output: ...
}

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

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