8.webpack 資源內聯
阿新 • • 發佈:2020-07-30
- 資源內聯:將js,css的程式碼內聯到Html程式碼中去,它可以減少Http網路請求,有利於頁面初始化以及css內聯可以避免頁面閃動等意義
- Html內聯和js內聯
- 安裝npm i [email protected] -D 目前0.5版本的比較穩定
- 在頁面中引入
- Css內聯
- 多頁面應用:每一次跳轉,後臺都會返回一個新HTML檔案
- 多頁面打包思路:每一個頁面對應一個entry,一個html-webpack-plugin。每次增加和修改頁面都需要修改webpack配置
- 通用方案:使用glob.sync動態獲取entry和設定html-webpack-plugin
- 安裝npm i glob -D
-
'use strict'; const glob = require('glob'); 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 CleanWebpackPlugin
- 安裝npm i glob -D