面向高级之webpack基础1
安装nvm与node
- nvm 的安装
https://www.cnblogs.com/gaozejie/p/10689742.html
- 查看node有多少个版本
nvm list - 安装其他node版本
nvm install v10.15.3 - webpack与webpack-cli版本 –
"webpack": "^4.35.0", "webpack-cli": "^3.3.5"
–save-dev || -D 与 --save || -S的区别
- –save-dev || -D //开发依赖(辅助)
- –save || -S // 运行依赖(发布)
简单初始化项目之基础配置
-
初始化项目 npm init -y 会生成package.json -
按照webpack与脚手架 npm install webpack webpack-cli --save-dev 输入 ./node_modules/.bin/webpack -v 查看安装的webpack版本
webpack 5.53.0
webpack-cli 4.8.0
初始化的打包 ./node_modules/.bin/webpack
-
项目根目录下创建 webpack配置文件 webpack.config.js -
配置打包指令 package.json "scripts": {
"build":"webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
-
安装react基础东西 npm i react react-dom @babel/preset-react -D
webpack4之基础2 webpack.config.js配置
0:mode的模式
1:enter的使用方式 - 打包的入口
2:output打包的出口
```js
'use strict';
const path = require('path');
module.exports = {
// // 单文件入口
// entry: './src/index.js',
// // 单文件出口
// output: {
// path: path.resolve(__dirname, 'dist'),
// filename: 'bundle.js',
// }
// 多入口
entry: {
index: './src/index.js',
hello2: './src/hello2.js'
},
// 多出口
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js', // 占位符合实现文件的唯一性
},
// 打包的模式 - 生产
mode: 'production'
};
```
3:loader加载器 - scss - less 预编译处理器等处理 es6等语法转化
解析ES6
- 安装依赖
npm i @babel/core @babel/preset-env babel-loader -D - 根目录下创建 .babelrc.js文件
{
"presets": [["@babel/preset-env"]]
}
- 在webpack.config.js 文件中配置 module 加载es6语法
'use strict';
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
}
mode: 'production',
module: {
rules: [{
test: /.js$/,
use: 'babel-loader'
}]
}
};
对react语法的解析
- 在前面 .babelrc.js 对es6的基础上增添东西
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
对css相关的解析
- 解析css
- 安装:
npm i style-loader@1.0.1 css-loader@3.3.2 -D - 解析scss:
- 安装:
npm install node-sass@4.14.1 -D 与 npm install sass-loader@10.1.1 -D
'use strict';
const path = require('path');
module.exports = {
entry: 'XXX',
output: {}
mode: 'production',
module: {
rules: [{
test: /.js$/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.s[ac]ss$/i,
use: [
"style-loader",
"css-loader",
"sass-loader",
],
},
]
}
};
对图片和文字的解析
- 解析img
npm i url-loader@4.1.1 file-loader@6.2.0 -D
module: {
{
test: /.(jpn|png|gif|webp|jpeg)$/,
use: [{
loader: 'url-loader',
options: {
limit: 10240
}
}]
},
}
插入篇 脚本指令
- 源代码更新后,自动打包 package.json 执行
npm run watch
"scripts": {
"build": "webpack",
"watch": "webpack --watch",
"test": "echo \"Error: no test specified\" && exit 1"
},
4:plugin插件 -
自动更新前端代码插件
- 安装:
npm install webpack-dev-server@3.1.4 -D - package.json配置
"scripts": {
"build": "webpack",
"watch": "webpack --watch",
"dev": "webpack-dev-server --open",
"test": "echo \"Error: no test specified\" && exit 1"
}
'use strict';
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: {
index: './src/index.js',
hello2: './src/hello2.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},
mode: 'development',
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader'
},
]
},
plugins: [
new webpack.HotModuleReplacementPlugin()
],
devServer: {
contentBase: path.join(__dirname, "dist"),
hot: true,
inline: true,
port: 9292,
}
};
注意点:以上的修改为 开发时配置 webpack.dev.js
'use strict';
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: {
index: './src/index.js',
hello2: './src/hello2.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},
mode: 'development',
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.s[ac]ss$/i,
use: [
"style-loader",
"css-loader",
"sass-loader",
],
},
{
test: /.(jpn|png|gif|webp|jpeg)$/,
use: [{
loader: 'url-loader',
options: {
limit: 10240
}
}]
},
]
},
plugins: [
new webpack.HotModuleReplacementPlugin()
],
devServer: {
contentBase: path.join(__dirname, "dist"),
hot: true,
inline: true,
port: 9292,
}
};
针对webpack.prod.js 生产时配置
"scripts": {
"build": "webpack --config webpack.prod.js",
"watch": "webpack --watch",
"dev": "webpack-dev-server --config webpack.dev.js --open",
"test": "echo \"Error: no test specified\" && exit 1"
},
文件的名称 取前哈希值几位 文件指纹
- css
npm install mini-css-extract-plugin@0.8.0 -D
'use strict';
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: {
index: './src/index.js',
hello2: './src/hello2.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name]_[chunkhash:8].js',
},
mode: 'production',
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader",
],
},
{
test: /.(jpn|png|gif|webp|jpeg)$/,
use: [{
loader: 'file-loader',
options: {
name: '[name]_[hash:8].[ext]',
}
}]
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name]_[chunkhash:8].css',
chunkFilename: "[id].css"
})
]
};
代码压缩
- html压缩
- 安装:
npm install html-webpack-plugin@3.2.0 -D - css压缩
- 安装:
npm install optimize-css-assets-webpack-plugin@5.0.1 -D npm install cssnano@4.1.10 -D - js压缩
'use strict';
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const htmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
index: './src/index.js',
hello2: './src/hello2.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name]_[chunkhash:8].js',
},
mode: 'production',
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader",
],
},
{
test: /.(jpn|png|gif|webp|jpeg)$/,
use: [{
loader: 'file-loader',
options: {
name: '[name]_[hash:8].[ext]',
}
}]
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name]_[chunkhash:8].css',
chunkFilename: "[id].css"
}),
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.(sa|sc|c)ss$/g,
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
preset: ['default', {
discardComments: {
removeAll: true
},
normalizeUnicode: false
}]
},
canPrint: true
}),
new htmlWebpackPlugin({
template: path.join(__dirname, "./src/index.html"),
filename: "index.html",
chunks: ['index'],
inject: true,
minify: {
html5:true,
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
}
}),
new htmlWebpackPlugin({
template: path.join(__dirname, "./src/search.html"),
filename: "search.html",
chunks: ['search'],
inject: true,
minify: {
html5:true,
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
}
}),
]
};
自动清除打包后的dist目录
- 安装:
npm install clean-webpack-plugin@2.0.2 -D
- 引入const clearWebpackPlugin = require(“clean-webpack-plugin”)
- 使用 new clearWebpackPlugin(),
- prod.js && dev.js 都需要
'use strict';
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const htmlWebpackPlugin = require('html-webpack-plugin');
const clearWebpackPlugin = require("clean-webpack-plugin")
module.exports = {
entry: {
index: './src/index.js',
hello2: './src/hello2.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name]_[chunkhash:8].js',
},
mode: 'production',
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader",
],
},
{
test: /.(jpn|png|gif|webp|jpeg)$/,
use: [{
loader: 'file-loader',
options: {
name: '[name]_[hash:8].[ext]',
}
}]
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name]_[chunkhash:8].css',
chunkFilename: "[id].css"
}),
new clearWebpackPlugin(),
]
};
自动补全css3前缀
- 安装:
npm install postcss-loader@3.0.0 autoprefixer@9.5.1 -D
'use strict';
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const htmlWebpackPlugin = require('html-webpack-plugin');
const clearWebpackPlugin = require("clean-webpack-plugin")
module.exports = {
entry: {
index: './src/index.js',
hello2: './src/hello2.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name]_[chunkhash:8].js',
},
mode: 'production',
module: {
rules: [
{
test: /.js$/,
use: 'babel-loader'
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader",
{
loader:'postcss-loader',
options:{
plugins: () => [
require('autoprefixer')({
browsers:['last 2 version','>1%','ios 7']
})
]
}
}
],
},
{
test: /.(jpn|png|gif|webp|jpeg)$/,
use: [{
loader: 'file-loader',
options: {
name: '[name]_[hash:8].[ext]',
}
}]
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name]_[chunkhash:8].css',
chunkFilename: "[id].css"
}),
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.(sa|sc|c)ss$/g,
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
preset: ['default', {
discardComments: {
removeAll: true
},
normalizeUnicode: false
}]
},
canPrint: true
}),
new htmlWebpackPlugin({
template: path.join(__dirname, "./src/index.html"),
filename: "index.html",
chunks: ['index'],
inject: true,
minify: {
html5:true,
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
}
}),
new htmlWebpackPlugin({
template: path.join(__dirname, "./src/search.html"),
filename: "search.html",
chunks: ['search'],
inject: true,
minify: {
html5:true,
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
}
}),
new clearWebpackPlugin(),
]
};
|