1. webpack 概念
[文档地址](https://www.webpackjs.com/concepts/)
2. webpack 命令使用及相关工具包
1. webpack 安装和打包命令:
$ npm i webpack --save-dev
$ webpack [source] [destination]
2. webpack 热部署工具安装和使用命令:
$ npm i webpack-dev-server --save-dev //安装
$ webpack-dev-derver --open --port [port] --contentBase [content] --hot
--open //自动打开浏览器
--port //指定端口号
--contentBase //指定根目录
--hot //打开热部署
3. webpack 需要的第三方包:
$ npm i vue-loader vue-template-complier
$ npm i babel-core babel-loader babel-plugin-transform-runtime
$ npm i babel-preset-env babel-preset-stage-0
3. webpack 配置文件模板
//webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const htmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: path.join(__dirname, './src/main.js'), //入口文件
output: { //指定输出项
path: path.join(__dirname, './dist'), //输出路径
filename: 'bundle.js' //指定输出文件名
},
plugins: [
new webpack.HotModuleReplacementPlugin(), //配置热部署
new htmlWebpackPlugin({
template: path.join(__dirname, './src/index.html'), //指定模板文件路径
filename: 'index.html' //指定浏览器打开的文件名
})
],
module: {
rules: [
{ test: /.css$/, use: ['style-loader', 'css-loader'] }, //处理.css文件的loader
{ test: /.css$/, use: ['style-loader', 'css-loader', 'less-loader'] }, //处理.less文件的loader
{ test: /.css$/, use: ['style-loader', 'css-loader', 'sass-loader'] }, //处理.scss的loader
{ test: /.(jpg|png|gif|jpeg|bmp)$/, use: 'url-loader?limit=[limit]&name=[hash:num]-[name].[ext]' }, //处理图片的loader
{ test: /.(ttf|eot|svg|woff|woff2)$/, use: 'url-loader' }, //处理字体的loader
{ test: /.js$/, use: 'babel-loader', exclude: /node_modules/ }, //配置babel转换高级的ES语法
{ test: /.vue$/, use: 'vue-loader' }, //处理.vue文件的loader
]
},
resolve: {
"alias": {
"vue$": "vue/dist/vue.js"
}
}
};