官方解释:
但是他到底是什么呢?(两个关键字)
前段模块化:
如何理解打包的概念
和grunt/gulp的对比
什么时候使用grunt/gulp?
所以,grunt/gulp 和webpack 有什么不同呢?
# 安装webpack
# @ 后面加版本号
# -g 指的是全局安装
npm install webpack@3.6.0 -g
# 利用webpack将 main.js 打包成 dist文件夹下的bundle.js
# webpack 会帮你处理各个模块间的依赖
webpack ./src/main.js ./dist/bundle.js
我们正常一个vue项目会有 一个src文件和一个dist文件夹
# --save-dev 开发时依赖 webpack一旦打包好就不需要了,所以属于开发时的依赖
npm install webpack@3.6.0 --save-dev
但是每次执行都需要敲一长串的命令,并不是很方便
Package,json 中的script 的脚本在示性式,会按照一定顺序寻找对应的命令
如何执行我们的buld 指令:
npm run build
{
"name": "meetwebpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^3.6.0"
}
}
npm install --save-dev babel-loader babel-core babel-preset-es2015
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: ‘babel-loader‘,
options: {
presets: [‘es2015‘]
}
}
}
]
}
// 或者你可以使用options属性来给loader传递参数
module: {
rules: [
{
test: /\.js$/,
// exclude 排除 下面文件夹中的文件不需要传唤
exclude: /(node_modules|bower_components)/,
use: {
loader: ‘babel-loader‘,
options: {
presets: [‘@babel/preset-env‘],
plugins: [require(‘@babel/plugin-transform-object-rest-spread‘)]
}
}
}
]
}
注意:通过 npm install vue —save 直接安装vue 后,在 项目中使用vue ,然后通过webpack打包后运行会报错。
[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
(found in <Root>)
上链接:
// package.json
{
"name": "meetwebpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^3.6.0"
},
"dependencies": {
"vue": "^2.6.11"
}
}
// wepack.config.js
// 这个文件是配置webpack的入口和出口文件
// node.js 语法获取绝对路径
// 1. 导入path模块,聪node包中找的
const path = require(‘path‘);
module.exports = {
entry:‘./src/main.js‘,
output:{
path:path.resolve(__dirname,‘dist‘), // 必须是绝对路径,这里是通过node.js 的path 实现的路径的拼接
filename:‘bundle.js‘ // bundle 打包
},
// vue 配置这个可以使用template
resolve:{
// alias: 别名
alias:{
// 当我们 导入 from vue 的时候,指向执行的路径去找文件 vue.esm.js 中包含 complier
‘vue$‘:‘vue/dist/vue.esm.js‘
}
}
};
作用:在bundle.js 中 增加版权信息,即为打包的文件添加版权申明
const path = require(‘path‘)
const webpack = require(‘webpack‘)
module.exports = {
....
plugins:[
new webpack.BannerPlugin(‘版权相关申明信息‘)
]
}
npm install html-webpack-plugin@3.2.0 --save-dev
# 注:这里如果在使用的过程中报错,则看 一下 package.json 文件中的版本号,我这里改成3.2.0 就不报错了
配置文件如下:(webpack.config.js)
// 这个文件是配置webpack的入口和出口文件
// node.js 语法获取绝对路径
// 1. 导入path模块,从node包中找的
const path = require(‘path‘);
const webpack = require(‘webpack‘);
const HtmlWebpackPlugin = require(‘html-webpack-plugin‘);
module.exports = {
entry:‘./src/main.js‘,
output:{
path:path.resolve(__dirname,‘dist‘), // 必须是绝对路径,这里是通过node.js 的path 实现的路径的拼接
filename:‘bundle.js‘ // bundle 打包
},
module:{
rules:[
{
test: /\.css$/,
// css-loader 只负责将css文件进行加载
// style-loader 负责将样式添加到dom中进行操作
// webpack 在使用多个loader的时候,是从右向左读的
use: [ ‘style-loader‘, ‘css-loader‘ ]
},
{
test:/\.vue$/,
use:[‘vue-loader‘]
},
]
},
resolve:{
// alias: 别名
alias:{
// // 当我们 导入 from vue 的时候,指向执行的路径去找文件 vue.esm.js 中包含 complier
// 该配置项可以让js以 不是runtime-complier 发行版运行,此时可以用template
‘vue$‘:‘vue/dist/vue.esm.js‘,
// // 配置完如下配置可以在导入的时候,可以省略后缀名
// extensions:[‘.js‘,‘.vue‘,‘.css‘],
}
},
plugins:[
new webpack.BannerPlugin(‘最终版权信息归qzk所有‘),
// ########### 生成html #############
new HtmlWebpackPlugin(
{
// 指定模板
template: ‘index.html‘,
}
),
]
};
npm install uglifyjs-webpack-plugin@1.1.1 --save-dev
const path = require(‘path‘);
const webpack = require(‘webpack‘);
const uglifyJsPlugin = require(‘uglifyjs-webpack-plugin‘);
module.exports = {
...
plugins:[
new webpack.BannerPlugin(‘版权申明‘),
new uglifyJsPlugin()
]
}
执行重新 npm run build 后 查看打包后就是被重新压缩过
npm install --save-dev webpack-dev-server@2.9.1
{
"name": "meetwebpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config ./build/prod.config.js",
"dev": "webpack-dev-server --open --config ./build/dev.config.js"
},
"author": "qzk",
"license": "ISC",
"devDependencies": {
"css-loader": "^3.5.3",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^1.2.1",
"uglifyjs-webpack-plugin": "^1.1.1",
"vue-loader": "^13.7.3",
"vue-template-compiler": "^2.6.11",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1",
"webpack-merge": "^4.2.2"
},
"dependencies": {
"vue": "^2.6.11"
}
}
# 相对路径(不是最终的方案)
./node_modules/.bin/webpack-dev-server
// 这个文件是配置webpack的入口和出口文件
// node.js 语法获取绝对路径
// 1. 导入path模块,从node包中找的
const path = require(‘path‘);
const webpack = require(‘webpack‘);
const HtmlWebpackPlugin = require(‘html-webpack-plugin‘);
const uglifyJsPlugin = require(‘uglifyjs-webpack-plugin‘);
module.exports = {
...
devServer:{
// 这个server服务于哪一个文件夹
contentBase:‘./dist‘,
// 实时监听变化,更新
inline:true,
// 指定端口
port:8008
}
};
最后就可以通过如下命令来执行了
npm run dev
npm install webpack-merge --save-dev
在实际开发与生产中,我们所依赖的配置文件,放在与src同级的build文件夹下一般分为:
// 这个文件是配置webpack的入口和出口文件
// node.js 语法获取绝对路径
// 1. 导入path模块,从node包中找的
const path = require(‘path‘);
const webpack = require(‘webpack‘);
const HtmlWebpackPlugin = require(‘html-webpack-plugin‘);
module.exports = {
entry:‘./src/main.js‘,
output:{
path:path.resolve(__dirname,‘../dist‘), // 必须是绝对路径,这里是通过node.js 的path 实现的路径的拼接
filename:‘bundle.js‘ // bundle 打包
},
module:{
rules:[
{
test: /\.css$/,
// css-loader 只负责将css文件进行加载
// style-loader 负责将样式添加到dom中进行操作
// webpack 在使用多个loader的时候,是从右向左读的
use: [ ‘style-loader‘, ‘css-loader‘ ]
},
{
test:/\.vue$/,
use:[‘vue-loader‘]
},
]
},
resolve:{
// alias: 别名
alias:{
// // 当我们 导入 from vue 的时候,指向执行的路径去找文件 vue.esm.js 中包含 complier
// 该配置项可以让js以 不是runtime-complier 发行版运行,此时可以用template
‘vue$‘:‘vue/dist/vue.esm.js‘,
// // 配置完如下配置可以在导入的时候,可以省略后缀名
// extensions:[‘.js‘,‘.vue‘,‘.css‘],
}
},
plugins:[
new webpack.BannerPlugin(‘最终版权信息归qzk所有‘),
new HtmlWebpackPlugin(
{
// 指定模板
template: ‘index.html‘,
}
),
],
};
// node.js 语法获取绝对路径
// 1. 导入path模块,从node包中找的
const uglifyJsPlugin = require(‘uglifyjs-webpack-plugin‘);
const webpackMerge = require(‘webpack-merge‘);
const baseConfig = require(‘./base.config‘);
module.exports = webpackMerge(baseConfig,{
plugins:[
new uglifyJsPlugin(),
],
});
const webpackMerge = require(‘webpack-merge‘);
const baseConfig = require(‘./base.config‘);
module.exports = webpackMerge(baseConfig,{
// 开发时依赖,但是生产时不依赖,所以不能放在base里面
devServer:{
// 这个server服务于哪一个文件夹
contentBase:‘./dist‘,
// 实时监听变化,更新
inline:true,
// 指定端口
port:8008
}
});
{
"name": "meetwebpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
# 配置生产时的依赖的 webpack.config.js
"build": "webpack --config ./build/prod.config.js",
# 配置 开发时的依赖的 webpack.config.js
"dev": "webpack-dev-server --open --config ./build/dev.config.js"
},
"author": "qzk",
"license": "ISC",
"devDependencies": {
"css-loader": "^3.5.3",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^1.2.1",
"uglifyjs-webpack-plugin": "^1.1.1",
"vue-loader": "^13.7.3",
"vue-template-compiler": "^2.6.11",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1",
"webpack-merge": "^4.2.2"
},
"dependencies": {
"vue": "^2.6.11"
}
}
原文:https://www.cnblogs.com/qianzhengkai/p/13034823.html