快速配置webpack
2018-09-10 01:06:42来源:博客园 阅读 ()
区分开发模式和生产模式:
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.js const 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;
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- JSP session配置对web应用的影响 2019-12-27
- Jquery 快速构建可拖曳的购物车DragDrop 2019-09-30
- 详解jqPlot Option配置对象 2019-08-29
- vue中使用proxy配置不同端口和ip接口 2019-08-14
- webpack打包配置禁止html标签全部转为小写 2019-08-14
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash