webpack 一、webpack 1 与grunt/gulp对比
2 webpack官网及安装
webpack官网
webpack安装
安装前提:先安装Node.js
全局安装
在终端直接执行webpack命令,使用的全局安装的webpack
npm install webpack@版本号 -g
局部安装 —- 切换到项目根目录下
当在package.json中定义了scripts时,其中包含了webpack命令,此时使用的是局部webpack
1 npm insatll webpack@版本号 --save-dev
3 webpack起步 3.1 构建目录
dist (用于存放之后打包的文件)
src (用于存放自己写的源文件)
index.html(浏览器打开展示的首页html)
package.json(npm包管理的文件,通过终端npm init生成的,此文件不是手动创建)
3.2 打包js文件
1 2 webpack 源文件路径 打包后文件路径 webpack ./src/main.js ./dist/bundle.js
3.3 使用打包后的文件
在./index.html中引入打包的bundle.js
1 2 3 4 // ./index.html <body > <script src ="./dist/bundle.js" > </script > </body >
4 优化js打包操作
优化webpack ./src/main.js ./dist/bundle.js每次执行都要输入文件路径的操作
4.1 配置入口和出口
1 2 3 4 5 6 7 8 9 10 11 12 const path = require ('path' )module .exports = {entry:'./src/main.js' , output:{ path: path.resolve(__dirname,'dist' ), filename: 'bundle.js' } }
4.2 局部(本地)安装webpack
1 npm install webpack@版本号 --save-dev
1 2 终端命令 .\node_modules\.bin\webpack
4.3 package.json中定义打包启动 1 2 3 4 "scripts" : { "build" : "webpack" },
注意:
最新版webpack在执行4.2 、4.3中的命令时还有如提示:
选择安装webpack-cli 安装后即可命令打包了
二、loader 1 loader介绍 1.1 webpack与loader
webpack主要用于打包js文件,可以自动处理js之间相关的依赖
loader用于执行将ES6转成ES5代码,将TypeScript转成ES5代码,将scss、less转成css,将.jsx、.vue文件转成js文件、加载图片等操作
1.2 loader使用流程
通过npm安装需要使用的loader
在webpack.config.js中的module关键字下进行配置
2 css文件处理
在src/css文件夹下创建css文件
1 2 3 4 //src/css/normal.css body { baackground-red }
在入口文件main.js文件中引入 css文件
1 require ('./css/normal.css' )
安装css-loader
1 npm install --save-dev css-loader
安装style-loader
1 npm install style-loader --save-dev
配置css-loader和style-loader
1 2 3 4 5 6 7 8 module : { rules: [ { test: /\.css$/ , use: [ 'style-loader' , 'css-loader' ] } ] }
注意:
1 use: [ 'style-loader' , 'css-loader' ]
3 less文件处理
在src/css文件夹下创建less文件
1 2 3 4 5 6 7 8 //src/css/special.less @fontSize: 50px; @fontColor: red; body { color : @fontColor; font-size : @fontSize; }
在入口文件main.js文件中引入 less文件
1 require ('./css/special.less' )
安装css-loader
1 npm install --save-dev css-loader
安装style-loader
1 npm install style-loader --save-dev
安装less-loader
1 npm install --save-dev less-loader less
配置css-loader、style-loader、less-loader
1 2 3 4 5 6 7 8 9 10 11 12 module : { rules: [{ test: /\.less$/ , use: [{ loader: "style-loader" }, { loader: "css-loader" }, { loader: "less-loader" }] }] }
注意:
1 use: [ 'style-loader' , 'css-loader' , 'less-loader' ]
4 图片处理
安装url-loader
1 npm install --save-dev url-loader
安装file-loader
1 npm install --save-dev file-loader
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 module : { output:{ path: path.resolve(__dirname,'dist' ), filename: 'bundle.js' , publicPath: 'dist/' }, rules: [ { test: /\.(png|jpg|gif)$/ , use: [ { loader: 'url-loader' , options: { limit: 8192 , name: 'img/[name].[hash:8].[ext]' } } ] } ] }
6 ES6转ES5
安装bable-loader
除了安装babel-loader还需要安装babel-core和babel-preset-****
babel-preset-es2015 : 指转ES6(es2015就是ES6) — 简单写法 不需要再配置babel
babel-preset-env: 也是转ES6 —– 需要再做babel相配置
1 2 3 4 npm install babel-loader@7 babel-core babel-preset-es2015 --save-dev npm install babel-loader babel-core babel-preset-env --save-dev
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 module : { rules: [ { test: /\.js$/ , exclude: /(node_modules|bower_components)/ , use: { loader: 'babel-loader' , options: { presets: ['es2015' ] presets: ['@babel/preset-env' ] } } } ] }
三、webpack配置Vue 1 使用vue流程
安装vue
配置
因为使用的是runtime-only版本,所以需要在webpack.config.js中做如下配置
1 2 3 4 5 resolve: { alias: { 'vue$' : 'vue/dist/vue.esm.js' } }
使用vue
1 2 3 4 5 6 7 8 9 import Vue from 'vue' new Vue({ el: '#app' , data: { name: 'Chuckie' } })
1 2 3 4 5 6 7 8 9 //index.html <body > <div id ="app" > <h2 > {{name}}</h2 > </div > <script src ="./dist/bundle.js" > </script > </body >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import Vue from 'vue' const App = { template: `<h2>{{message}}</h2>` , data(){ return { message: '我是APP组件' } } } new Vue({ el: '#app' , template: ` //template会直接替换index.html中<div id="app"></div>包裹的内容 <div id="app"> <h2>{{fname}}</h2> <c-cpn/> </div> ` , data: { fname: 'Chuckie' }, components: { 'c-cpn' : App } })
1 2 3 4 5 6 7 //index.html <body > <div id ="app" > </div > <script src ="./dist/bundle.js" > </script > </body >
3.3 .vue文件封装处理
使用.vue文件需要做如下安装和配置
安装1 npm install vue-loader vue-template-compiler --save-dev
配置
1 2 3 4 5 6 7 8 9 module : { rules: [ { test: /\.vue$/ , use: ['vue-loader' ] } ] }
* BUG处理
* 问题:` Vue-loader`在15.*之后的版本 vue-loader的使用都是需要伴有 `VueLoaderPlugin `
* 解决: webpack.config.js中做如下配置1 2 3 4 5 6 7 const VueLoaderPlugin = require ('vue-loader/lib/plugin' );module .exports = { plugins: [ new VueLoaderPlugin() ] }
* 使用:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <template> <div> <h2 class ="title" >{{message}}</h2> </ div></template> <script> export default { name: 'App', data(){ return { message: '我是.vue文件封装的APP组件', } } } </ script><style scoped> .title{ color: green; } </style>
1 2 3 4 5 6 7 8 9 10 11 12 import Vue from 'vue' import App from './vue/App.vue' new Vue({ el: '#app' , template: '<App/>' , comments: { App } })
1 2 3 4 5 6 7 //index.html <body > <div id ="app" > </div > <script src ="./dist/bundle.js" > </script > </body >
2 el和template的区别
el: 用于和index.html中的#app进行绑定,让Vue实例之后可以管理它其中的内容
template: template模板的内容会替换掉挂载的对应el的模板。
用template覆盖el之后就不需要在开发中再次操作index.html,只需要在template中写入对应的标签即可
1 2 3 4 5 6 7 8 9 10 11 12 import Vue from 'vue' import App from './vue/App.vue' new Vue({ el: '#app' , template: '<App/>' , comments: { App } })
1 2 3 4 5 6 7 //index.html <body > <div id ="app" > </div > <script src ="./dist/bundle.js" > </script > </body >
四、plugin 1 plugin介绍 1.1 plugin与loader
plugin是插件,它是对webpack现有功能的各种扩展,比如打包优化,文件压缩等等,是一个扩展器。
loader主要用于转换某些类型的模块,它是一个转换器。
1.2 plugin的使用流程
通过npm安装需要使用的plugins(某些webpack已经内置的插件不需要安装)
在webpack.config.js中的plugins中配置插件。
2 添加版权的plugin
1 2 3 4 5 6 7 8 const webpack = require ('webpack' )module .export = { plugins: [ new webpack.BannerPlugin('最终版权归Chuckie所有' ) ] }
效果: 在bundle.js文件开头会添加最终版权归Chuckie所有的声明
3 打包html的plugin
1 npm install html-webpack-plugin --save-dev
1 2 3 4 5 6 7 8 9 10 11 const HtmlWebpackPlugin = require ('html-webpack-plugin' )module .exports = { plugins: [ new HtmlWebpackPlugin({ template: './src/index.html' , filename: 'index.html' }) ] }
4 js压缩的Plugin
1 npm install uglifyjs-webpack-plugin --save-dev
1 2 3 4 const uglifyJsPlugin = require ('uglifyjs-webpack-plugin' ) plugins: [ new uglifyJsPlugin() ]
五、本地服务器 1 搭建本地服务器
1 npm install --save-dev webpack-dev-server@版本号
1 2 3 4 5 devServer: { contentBase: './dist' , inline: true , port:3000 }
package.js中配置scripts
该配置是设置启动本地服务器的命令
--open: 表示直接打开浏览器
1 2 3 "scripts" : { "dev" : "webpack-dev-server --open" },
2 webpack配置分离
项目根目录下新建build文件夹 文件夹下新建3个配置文件:
base.config.js: webpack基本配置(开发时和发布时都使用的配置)
dev.config.js: 开发时需要的webpack配置
prod.config.js: 发布时需要的webpack配置
安装合并插件
1 npm install webpack-merge --save-dev
使用webpack-merge
dev.config.js中配置
1 2 3 4 5 6 7 8 9 const webpackMerge = require ('webpack-merge' ) const baseConfig = require ('./base.config' ) module .exports = webpackMerge(baseConfig, { devServer: { contentBase: './dist' , inline: true } })
prod.config.js中配置
1 2 3 4 5 6 7 8 9 const webpackMerge = require ('webpack-merge' )const baseConfig = require ('./base.config' )module .exports = webpackMerge(baseConfig, { plugins: [ ], })
删除之前的配置文件:webpack.config.js
更改config.js中的配置
1 2 3 4 "scripts" : { "build" : "webpack --config ./build/prod.config.js" , "dev" : "webpack-dev-server --open --config ./build/dev.config.js" },
更改打包文件保存的路径
1 2 3 4 5 6 module .exports = { output:{ path: path.resolve(__dirname,'dist' ), filename: 'bundle.js' },
生成dist文件的路径有变化 __dirname拿到的是prod.config.js所在的路径
若不更改 dist文件夹将生成在prod.config.js所在文件中
更改:使dist文件夹生成在项目根目录下
1 2 3 4 5 6 module .exports = { output:{ path: path.resolve(__dirname,'../dist' ), filename: 'bundle.js' },