webpack.docschina.org/javascript
webpack是一个现代 JavaScript 应用程序的静态模块打包器(module bundler)。当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(dependency graph),其中包含应用程序须要的每一个模块,而后将全部这些模块打包成一个或多个bundle。css
webpack 自己是基于node.js开发的。html
从 webpack v4.0.0 开始,能够不用引入一个配置文件(零配置文件),然而webpack仍然仍是高度可配置的。vue
目前*@vue/cli和create-react-app基本上采用的是webpack 4.0以上版本,因此本次课程以第四代版本为主;第四代版本须要咱们安装webpack和webpack-cli*(可执行命令);java
为了防止全局安装webpack的版本冲突,咱们真实项目开发的时候基本上以安装在本地项目中为主;node
$ npm init -y
$ npm install webpack webpack-cli --save-dev
OR
$ yarn add webpack webpack-cli -D
复制代码
初步体验(零配置)react
/*
* 默认会打包SRC目录中的JS文件(入口默认index.js)
* 打包完成的目录默认是DIST/MAIN.JS
*
* npx:http://www.ruanyifeng.com/blog/2019/02/npx.html
* 默认执行node_modules/bin/webpack.cmd文件
* webpack默认支持CommonJS和ES6 Module的模块规范,依此进行依赖打包
*/
$ npx webpack
复制代码
自定义基础配置jquery
let path = require('path');
module.exports = {
//=>打包模式 开发环境development 生产环境production
mode: 'production',
//=>入口
entry: './src/index.js',
//=>输出
output: {
//=>输出文件的文件名
filename: 'bundle.js',
//=>输出目录的"绝对路径"
path: path.resolve(__dirname, 'dist')
}
}
复制代码
自定义配置文件名webpack
"scripts": {
"serve": "webpack --config webpack.config.development.js",
"build": "webpack --config webpack.config.production.js"
},
复制代码
webpack.js.org/configurati…git
安装:$ yarn add webpack-dev-server -D
基础配置
/* webpack.config.js */
//=>配置DEV-SERVER
devServer: {
//=>端口
port: 3000,
//=>显示编译进度
progress: true,
//=>指定访问资源目录
contentBase: './dist',
//=>自动打开浏览器
open: true
}
复制代码
/* package.json */
"scripts": {
"serve": "webpack-dev-server",
"build": "webpack"
}
复制代码
代码更改后,会自动从新编译,而后自动刷新页面
www.webpackjs.com/plugins/htm…
安装:$ yarn add html-webpack-plugin -D
在webpack.config.js中使用
let HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
...,
//=>在webpack中使用插件
plugins: [
new HtmlWebpackPlugin({
//=>指定本身的模板
template: './src/index.html',
//=>输出的文件名
filename: 'index.html',
//=>给引入的文件设置HASH戳(清除缓存的),也能够在output中设置 filename: 'bundle.[hash].js' 来生成不一样的文件
hash: true,
//=>控制是否以及以何种方式最小化输出
//=>https://github.com/kangax/html-minifier
minify: {
collapseWhitespace: true,
removeComments: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true
}
})
]
}
复制代码
安装:$ yarn add css-loader style-loader less less-loader autoprefixer postcss-loader ... -D
使用
module.exports = {
//=>配置模块加载器LOADER
module: {
//=>模块规则:使用加载器(默认从右向左执行,从下向上)
rules: [{
test: /\.(css|less)$/, //=>基于正则表达式匹配哪些模块须要处理
use: [
"style-loader", //=>把CSS插入到HEAD中
"css-loader", //=>编译解析@import/URL()这种语法
"postcss-loader", //=>设置前缀
{
loader: "less-loader",
options: {
//=>加载器额外的配置
}
}
]
}]
}
}
复制代码
postcss.config.js
module.exports = {
plugins: [
require('autoprefixer')
]
};
复制代码
package.json
"browserslist": [
"> 1%",
"last 2 versions"
]
复制代码
let MiniCssExtractPlugin = require('mini-css-extract-plugin'),
OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin'),
UglifyjsWebpackPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
//=>设置优化项
optimization: {
//=>设置压缩方式
minimizer: [
//=>压缩CSS(可是必须指定JS的压缩方式)
new OptimizeCssAssetsWebpackPlugin(),
//=>压缩JS
new UglifyjsWebpackPlugin({
cache: true, //=>是否使用缓存
parallel: true, //=>是不是并发编译
sourceMap: true, //=>启动源码映射(方便调试)
})
]
},
plugins: [
//=>使用插件
new MiniCssExtractPlugin({
//=>设置编译后的文件名字
filename: 'main.css'
})
],
module: {
rules: [{
test: /\.(css|less)$/,
use: [
// "style-loader",
//=>使用插件中的LOADER代替STYLE方式
MiniCssExtractPlugin.loader,
"css-loader",
"postcss-loader",
"less-loader"
]
}]
}
}
复制代码
上述JS压缩对于require/import等还存在问题,须要对于ES6中的一些语法进行处理!
module.exports = {
...,
module: {
rules: [...,{
test: /\.js$/,
use: [{
loader: 'babel-loader',
options: {
//=>转换的语法预设(ES6->ES5)
presets: [
"@babel/preset-env"
],
//=>基于插件处理ES6/ES7中CLASS的特殊语法
plugins: [
["@babel/plugin-proposal-decorators", {
"legacy": true
}],
["@babel/plugin-proposal-class-properties", {
"loose": true
}],
"@babel/plugin-transform-runtime"
]
}
}], //=>, "eslint-loader"
//=>设置编译时忽略的文件和指定编译目录
include: path.resolve(__dirname, 'src'),
exclude: /node_modules/
}]
}
}
复制代码
参考https://eslint.org/demo生成 .eslintrc.json
补充知识:在vscode中开启ES7中类的装饰器,项目根目录中设置 jsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true
}
}
复制代码
@log
class A{
a=1;
}
复制代码
//=>内联加载器
import jquery from 'expose-loader?$!jquery';
console.log(window.$);
{
//=>只要引入JQUERY就在全局注入$
test: require.resolve('jquery'),
use: ['expose-loader?$']
}
复制代码
let webpack = require('webpack');
module.exports = {
plugins: [
//=>在每一个模块中都注入$
new webpack.ProvidePlugin({
'$': 'jquery'
})
],
}
//=>页面中
console.log($);
复制代码
安装 $ yarn add file-loader url-loader html-withimg-loader -D
module.exports = {
...,
module: {
//=>模块规则:使用加载器(默认从右向左执行)
rules: [..., {
test: /\.(png|jpg|gif)$/i,
use: [{
//=>把指定大小内的图片BASE64
loader: 'url-loader',
options: {
limit: 200 * 1024,
outputPath:'/images'
}
}],
include: path.resolve(__dirname, 'src'),
exclude: /node_modules/
}, {
test: /\.html$/,
use: ['html-withimg-loader']
}]
}
}
复制代码
最后实现文件分目录发布
module.exports = {
output: {
//=>配置引用前缀(全部资源前加这个地址)
publicPath: './'
},
plugins: [
new MiniCssExtractPlugin({
filename: 'css/main.css'
})
],
module: {
//=>模块规则:使用加载器(默认从右向左执行)
rules: [...,{
test: /\.(png|jpg|gif)$/i,
use: [{
options: {
outputPath: 'images'
}
}]
}]
}
}
复制代码