摘要:相關(guān)鏈接詳細(xì)教程,從無到有搭建腳手架一詳細(xì)教程,從無到有搭建腳手架二詳細(xì)教程,從無到有搭建腳手架三管理打包后目錄結(jié)構(gòu)打包結(jié)構(gòu)如下修改配置通過相對(duì)目錄對(duì)資源命名前加上目錄名,效果后面的步驟在這里需要安裝一個(gè)大型的包,以為例安裝為第三
相關(guān)鏈接
webpack4詳細(xì)教程,從無到有搭建react腳手架(一)
webpack4詳細(xì)教程,從無到有搭建react腳手架(二)
webpack4詳細(xì)教程,從無到有搭建react腳手架(三)
管理打包后目錄結(jié)構(gòu) 打包結(jié)構(gòu)如下build/ js/ xxx.js css/ xxx.css images/ xxx.jpg index.html修改配置
config/webpack.common.js
function webpackCommonConfigCreator(options){ ... return { ... output: { - filename: "bundle.js", + filename: "js/bundle.js", path: path.resolve(__dirname, "../build"), }, module: { rules: [ ... { test: /.(jpg|png|svg|gif)$/, use: [ { loader: "url-loader", options: { limit: 10240, - name: "[hash].[ext]", + name: "images/[hash].[ext]", } }, ] }, ] }, plugins: [ ... new ExtractTextPlugin({ - filename: "[name][hash].css" + filename: "css/[name][hash].css" }), ] } }
通過相對(duì)output目錄對(duì)資源命名前加上目錄名
yarn build, 效果: 后面的步驟在這里需要安裝一個(gè)大型的包,以Ant-design為例 安裝yarn add antd為第三方包配置css解析,將樣式表直接導(dǎo)出
config/webpack.common.js
... modules: { rules: [ { test: /.(css|scss)$/, include: path.resolve(__dirname, "../src"), use: ExtractTextPlugin.extract({ ... }) }, + { + test: /.(css|scss)$/, + exclude: path.resolve(__dirname, "../src"), + use: [ + "style-loader/url", + { + loader: "file-loader", + options: { + name: "css/[name].css" + } + } + ] + }, ] } ...使用antd組件
引入antd樣式表
src/index.js
import React from "react"; import ReactDom from "react-dom"; import App from "./App.js"; + import "antd/dist/antd.css"; ReactDom.render(, document.getElementById("root"));
創(chuàng)建目錄 src/component
src/component/Btn.js
import React from "react"; import {Button} from "antd"; export default function Btn(){ return () }
引入組件
src/App.js
+ import Btn from "./components/Btn"; function App(){ return (yarn start,成功, 但是bundle.js體積非常的大,需要優(yōu)化 分割bundle 配置... +) } ...
對(duì)chunks屬性我在一個(gè)思否回答中答了答案,鏈接: https://segmentfault.com/q/10...
config/webpack.common.js
function webpackCommonConfigCreator(options){ return { ... output: { - filename: "js/bundle.js", + filename: "js/[name].js", path: path.resolve(__dirname, "../build"), }, ... + optimization: { + splitChunks: { + chunks: "all", + minSize: 50000, + minChunks: 1, + } + } } }yarn build, 打包如下 緩存 為了在每次修改代碼后,瀏覽器都能獲取到最新的js,通常會(huì)對(duì)output的名添加hash值
config/webpack.common.js
output: { - filename: "js/[name].js", + filename: "js/[name][hash].js", path: path.resolve(__dirname, "../build"), },效果
yarn build
修改開發(fā)代碼后再次打包
可以看到修改代碼后,打包的js文件名hash值變了,瀏覽器請(qǐng)求總能夠獲取到最新的代碼
但是分割出來的antd和react的代碼沒有變化,名字也變了,則瀏覽器也會(huì)再次請(qǐng)求這個(gè)模塊,應(yīng)該沒有發(fā)生改變的模塊保持名稱以使瀏覽器從緩存中獲取,在生產(chǎn)模式下使用[chunkhash]替代[hash]config/webpack.common.js
output: { - filename: "js/[name][hash].js", path: path.resolve(__dirname, "../build"), },
config/webpack.prod.js
+ output: { + filename: "js/[name][chunkhash].js", + },
config/webpack.dev.js
+ output: { + filename: "js/[name][hash].js", + },效果
yarn build
修改開發(fā)代碼后再次打包
配置source-map和manifest.json 在打包后的文件中,如果出現(xiàn)異常,堆棧追蹤異常不能定位到打包前的單個(gè)文件,所以使用source-map。官方推薦開發(fā)模式下使用inline-source-map, 生產(chǎn)模式使用source-mapconfig/webpack.dev.js
const config = { ... + devtool: "inline-source-map", ... }
config/webpack.prod.js
const config = { ... + devtool: "source-map", ... }manifest
安裝
yarn add webpack-manifest-plugin -D
配置
config/webpack.prod.js
... const ManifestPlugin = require("webpack-manifest-plugin"); const config = { ... plugins: [ ... new ManifestPlugin(), ] }配置公共路徑
當(dāng)我們使用vue-cli或者create-react-app腳手架打包項(xiàng)目后,未修改publicPath的情況下直接打開index.html會(huì)報(bào)錯(cuò)無法找到j(luò)s、css靜態(tài)資源
因?yàn)槟_手架默認(rèn)的publicPath設(shè)置為 /,則對(duì)應(yīng)的資源外鏈都是從服務(wù)器路徑/開始尋找資源
配置config/webpack.common.js
function webpackCommonConfigCreator(options){ return { ... output: { ... + publicPath: "/" }, ... module: { rules: [ ... { test: /.(jpg|png|svg|gif)$/, use: [ { loader: "url-loader", options: { limit: 10240, name: "images/[hash].[ext]", + publicPath: "/" } }, ] }, ] } } }yarn build, 打包完成后推薦使用http-server
yarn global add http-server npm install http-server -ghttp-server build
源碼github倉(cāng)庫: https://github.com/tanf1995/W...
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/105476.html
摘要:相關(guān)鏈接詳細(xì)教程,從無到有搭建腳手架一配置插件,這兩個(gè)插件基本上是必配的了介紹每次打包時(shí)清理上次打包生成的目錄官網(wǎng)指南關(guān)于這個(gè)插件部分內(nèi)容已經(jīng)過時(shí)沒有更新,按照官網(wǎng)配置會(huì)出錯(cuò),所以參考上這個(gè)插件文檔來配置,文檔地址生成打 相關(guān)鏈接 webpack4詳細(xì)教程,從無到有搭建react腳手架(一) 配置插件 clean-webpack-plugin、 html-webpack-plugin...
摘要:是一個(gè)現(xiàn)代應(yīng)用程序的靜態(tài)模塊打包器,前端模塊化的基礎(chǔ)。作為一個(gè)前端工程師切圖仔,非常有必要學(xué)習(xí)。官網(wǎng)的文檔非常的棒,中文文檔也非常給力,可以媲美的文檔。建議先看概念篇章,再看指南,然后看和配置總覽。 webpack 是一個(gè)現(xiàn)代 JavaScript 應(yīng)用程序的靜態(tài)模塊打包器,前端模塊化的基礎(chǔ)。作為一個(gè)前端工程師(切圖仔),非常有必要學(xué)習(xí)。 showImg(https://segment...
摘要:相關(guān)鏈接詳細(xì)教程,從無到有搭建腳手架一詳細(xì)教程,從無到有搭建腳手架二安裝配置創(chuàng)建,效果安裝配置創(chuàng)建效果配置模式修改配置現(xiàn)在編譯后的由動(dòng)態(tài)內(nèi)聯(lián)在中,需要分離到單獨(dú)的文件安裝插 相關(guān)鏈接 webpack4詳細(xì)教程,從無到有搭建react腳手架(一) webpack4詳細(xì)教程,從無到有搭建react腳手架(二) Css 安裝loader yarn add style-loader css...
摘要:寫在前面準(zhǔn)備學(xué)習(xí)一下和相關(guān)的東西,官方的腳手架看起來太繁瑣,所以打算自己來搭建一個(gè),參考了這個(gè)文檔從零搭建全家桶框架教程步驟上都差不多第一步,依賴總覽完成到我現(xiàn)在半成品的過程中,目前完成開發(fā)模式的相關(guān)操作,添加了按需加載的地步。 寫在前面 準(zhǔn)備學(xué)習(xí)一下react和webpack相關(guān)的東西,官方的腳手架看起來太繁瑣,所以打算自己來搭建一個(gè),參考了這個(gè)文檔從零搭建React全家桶框架教程;...
摘要:對(duì)的工作流程有點(diǎn)模糊,以及據(jù)官方文檔稱的升級(jí),性能得到了極大的提升,而還是用的,于是決定從頭開始搭建一個(gè)適合團(tuán)隊(duì)的腳手架。保證各文件獲得一致的文件編碼和縮進(jìn)效果。這些在后面文章中,都會(huì)一個(gè)個(gè)涉及到,此處不做詳細(xì)展開。 前言 寫前端項(xiàng)目這么久了,以前用的 dva 框架,后來用過 create-react-app 框架,都需要針對(duì)團(tuán)隊(duì)做一些定制化的修改。對(duì) webpack 的工作流程有點(diǎn)模...
閱讀 3599·2021-09-13 10:28
閱讀 1948·2021-08-10 09:43
閱讀 1022·2019-08-30 15:44
閱讀 3193·2019-08-30 13:14
閱讀 1850·2019-08-29 16:56
閱讀 2947·2019-08-29 16:35
閱讀 2854·2019-08-29 12:58
閱讀 872·2019-08-26 13:46