快速配置webpack

來源:互聯網
上載者:User

標籤:highlight   edm   mod   重新整理   project   dex   asc   new   process   

區分開發模式和生產模式:

  npm run start——開發模式,啟用devServer,檔案的改動即時更新、重新整理

  npm run build——生產模式,打包檔案到dist檔案夾

 

// package.json{  "name": "test",  "version": "1.0.0",  "description": "simple project",  "private": true,  "scripts": {    "build": "webpack --config webpack.config.js --color --progress --mode=production",    "start": "webpack-dev-server --open --mode=development"  },  "author": "yangxiang",  "license": "ISC",  "devDependencies": {    "babel-core": "^6.26.3",    "babel-loader": "^7.1.5",    "babel-preset-env": "^1.7.0",    "copy-webpack-plugin": "^4.5.2",    "css-loader": "^1.0.0",    "html-webpack-plugin": "^3.2.0",    "uglifyjs-webpack-plugin": "^1.3.0",    "webpack": "^4.17.2",    "webpack-cli": "^3.1.0",    "webpack-dev-server": "^3.1.7"  },  "dependencies": {    "mockjs": "^1.0.1-beta3"  }}

  

// webpack.config.jsconst path = require(‘path‘);const webpack = require(‘webpack‘);const HtmlWebpackPlugin = require(‘html-webpack-plugin‘);const UglifyJsPlugin = require(‘uglifyjs-webpack-plugin‘);let webpackConfig = {    entry: ‘./index.js‘,    output: {        filename: ‘main.js‘,        path: path.resolve(__dirname, ‘dist‘)    },    module: {        rules: [{            test: /\.js$/,            exclude: /node_modules/,            use: {                loader: ‘babel-loader‘,                options: {                    presets: [‘env‘]                }            }        }, {            test: /\.(png|jpe?g|gif)(\?.*)?$/,            use: [{                loader: ‘url-loader‘,                options: {                    limit: 4096,                    name: ‘img/[name].[hash:8].[ext]‘                }            }]        }, {            test: /\.css$/,            use: [‘style-loader‘, ‘css-loader‘]        }]    },    plugins: [        new HtmlWebpackPlugin({            template: ‘./index.html‘        })    ]};if (process.env.NODE_ENV == "development") {    // 開發模式下的配置    webpackConfig.devServer = {        hot: true,        port: 8888    };    webpackConfig.plugins.concat(        new webpack.HotModuleReplacementPlugin(),        new webpack.NamedModulesPlugin(),        new webpack.NoEmitOnErrorsPlugin()    )} else {    // 生產模式下的配置    webpackConfig.plugins.concat(        new UglifyJsPlugin({            uglifyOptions: {                compress: {                    warnings: false                }            },            sourceMap: true,            parallel: true        })    )}module.exports = webpackConfig;

  

快速配置webpack

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.