摘要:前言微前端理論篇上一篇介紹了微前端的理念,本片將開始介紹項目。先實現公共依賴的引入吧。在上一步我們沒有引入的依賴包,是因為的依賴包是作為公共依賴導入的。里面全是我的公共依賴文件在下新建文件夾,新建文件,作為我們整個項目的頁面文件。
前言
微前端 —— 理論篇
上一篇介紹了微前端的理念,本片將開始介紹portal項目。
????????portal項目包括兩個功能:
????????1. 路由分發與應用加載;
????????2. 抽離公共依賴;
目錄結構如下:
新建一個文件夾,命名隨便,我的是portal
????????執行npm init -y初始化package.json
????????現在我們要做什么呢?肯定是先引入依賴包啊
????????webpack打包相關和babel和簡單幾個需要的loader就行了
????????npm i webpack webpack-cli webpack-dev-server style-loader css-loader copy-webpack-plugin clean-webpack-plugin babel-loader babel-core @babel/preset-env @babel/plugin-syntax-dynamic-import @babel/core -D
????????由于我們還需要同時運行公共依賴引入和應用加載兩個服務,所以再 npm i concurrently -D
在引入所需的依賴之后,開始實現相關功能。先實現公共依賴的引入吧。在上一步我們沒有引入single-spa的依賴包,是因為single-spa的依賴包是作為公共依賴導入的。
????????先創建一個公共依賴的文件夾,這個文件夾在任何地方都可以,因為我們是通過遠程鏈接引入的,要是你認為的single-spa.js文件可以通過遠程cdn或者其他鏈接取到,可以跳過這一步了,下面是我創建的公共依賴庫文件夾。
????????里面全是我的公共依賴文件
????????
????????在portal下新建src文件夾,新建文件index.html,作為我們整個項目的頁面文件。代碼如下
// 此處為從遠程導入各個項目的js文件 // (react、vue項目打包和運行都會產生一個js文件,此文件就是動態渲染頁面用的) // config.js是portal項目產生的,用來進行路由分發與組件狀態管理等
????????接下來實現common-deps.js,將我們所有的公共依賴文件統一引入,并命名
window.SystemJS = window.System function insertNewImportMap(newMapJSON) { const newScript = document.createElement("script") newScript.type = "systemjs-importmap" newScript.text = JSON.stringify(newMapJSON) const allMaps = document.querySelectorAll("script[type="systemjs-importmap"]") allMaps[allMaps.length - 1].insertAdjacentElement( "afterEnd", newScript ) } const devDependencies = { imports: { react: "http://localhost:8000/react.development.js", "react-dom": "http://localhost:8000/react-dom.development.js", "react-dom/server": "http://localhost:8000/react-dom-server.browser.development.js", "single-spa": "http://localhost:8000/single-spa.min.js", lodash: "http://localhost:8000/lodash.js", rxjs: "http://localhost:8000/rxjs.umd.js", } } const prodDependencies = { imports: { react: "http://localhost:8000/react.production.min.js", "react-dom": "http://localhost:8000/react-dom.production.min.js", "react-dom/server": "http://localhost:8000/react-dom-server.browser.production.min.js", "single-spa": "http://localhost:8000/single-spa.min.js", lodash: "http://localhost:8000/lodash.min.js", rxjs: "http://localhost:8000/rxjs.umd.min.js", } } const devMode = true // you will need to figure out a way to use a set of production dependencies instead if (devMode) { insertNewImportMap(devDependencies) } else { insertNewImportMap(prodDependencies) }
????????公共依賴的抽取代碼已經實現了,下面就配置webpack,將這些依賴進行打包,在項目根目錄創建 webpack.common-deps.config.js、webpack.common-deps.config.dev.js
????????webpack.common-deps.config.js
const path = require("path") const CleanWebpackPlugin = require("clean-webpack-plugin") const CopyWebpackPlugin = require("copy-webpack-plugin"); module.exports = { entry: "./src/common-deps.js", output: { filename: "common-deps.js", path: path.resolve(__dirname, "build/common-deps"), chunkFilename: "[name].js", }, mode: "production", node: { fs: "empty", }, resolve: { modules: [ __dirname, "node_modules", ], }, devtool: "sourcemap", plugins: [ new CleanWebpackPlugin(["build/common-deps/"]), CopyWebpackPlugin([ {from: path.resolve(__dirname, "src/common-deps.js")} ]), ], module: { rules: [ {parser: {System: false}}, { test: /.js$/, exclude: /(node_modules|bower_components)/, use: { loader: "babel-loader", } } ] } }
????????webpack.common-deps.config.dev.js
/* eslint-env node */ const config = require("./webpack.common-deps.config.js"); const webpack = require("webpack"); config.plugins.push(new webpack.NamedModulesPlugin()); config.plugins.push(new webpack.HotModuleReplacementPlugin()); config.devServer = { headers: { "Access-Control-Allow-Origin": "*", }, } config.mode = "development" module.exports = config;
????????上面配置dev-server跨域,很重要!?。》駝t會因為無法跨域,訪問不到js文件
????????最后在package.json的scripts中添加命令
"scripts": { "test": "echo "Error: no test specified" && exit 1", "start:common-deps": "webpack-dev-server --config ./webpack.common-deps.config.dev.js --port 8234", "build:common-deps": "webpack --config ./webpack.common-deps.config.js -p" },
????????至此,公共依賴的抽取已經完成,執行npm run start:common-deps,公共依賴就被打包到了portal項目下的build/common-deps/目錄,通過import也可以正常導入使用(歸功于system)
公共依賴抽取實現、配置完畢之后,開始利用single-spa構建我們的路由分發系統
????????在src中創建文件activityFns.js,實現路由的正確匹配,內容如下:
export function prefix(location, ...prefixes) { return prefixes.some( prefix => ( location.href.indexOf(`${location.origin}/${prefix}`) !== -1 ) ) } // return true 則加載 false則不加載 // 這里的menu是菜單,按理應該一直加載出來的,因此return true export function menu(location) { return true } export function project1(location) { return prefix(location, "", "page1", "page2") } export function project2(location) { return prefix(location, "page3", "page4") }
????????在src中創建文件config.js,通過single-spa實現路由的注冊
import * as isActive from "./activityFns.js" import * as singleSpa from "single-spa" singleSpa.registerApplication("menu", () => SystemJS.import("@portal/menu"), isActive.menu) singleSpa.registerApplication("project1", () => SystemJS.import("@portal/project1"), isActive.project1) singleSpa.registerApplication("project2", () => SystemJS.import("@portal/project2"), isActive.project2) singleSpa.start()
????????配置webpack打包,在項目根目錄創建文件webpack.config.config.js,內容如下:
/* eslint-env node */ const webpack = require("webpack") const path = require("path"); const CleanWebpackPlugin = require("clean-webpack-plugin"); const CopyWebpackPlugin = require("copy-webpack-plugin"); module.exports = { entry: "./src/config.js", output: { filename: "config.js", library: "config", libraryTarget: "amd", path: path.resolve(__dirname, "build"), }, mode: "production", module: { rules: [ {parser: {System: false}}, { test: /.js?$/, exclude: [path.resolve(__dirname, "node_modules")], loader: "babel-loader", }, { test: /.css$/, exclude: [path.resolve(__dirname, "node_modules"), /.krem.css$/], use: [ "style-loader", { loader: "css-loader", options: { localIdentName: "[path][name]__[local]", }, }, { loader: "postcss-loader", options: { plugins() { return [ require("autoprefixer") ]; }, }, }, ], }, { test: /.css$/, include: [path.resolve(__dirname, "node_modules")], exclude: [/.krem.css$/], use: ["style-loader", "css-loader"], }, ], }, resolve: { modules: [ __dirname, "node_modules", ], }, plugins: [ CopyWebpackPlugin([ {from: path.resolve(__dirname, "src/index.html")}, {from: path.resolve(__dirname, "src/styles.css")}, ]), new CleanWebpackPlugin(["build"]), ], devtool: "source-map", externals: [ /^lodash$/, /^single-spa$/, /^rxjs/?.*$/, ], };
????????上面的配置中,最后的externals屬性值配置不需要webpack打包的依賴,因為這些在公共依賴中已經打包好了,不需要多帶帶打包
????????在項目根目錄創建文件webpack.config.config.dev.js,內容如下:
const config = require("./webpack.config.config.js") const webpack = require("webpack") config.plugins.push(new webpack.NamedModulesPlugin()); config.plugins.push(new webpack.HotModuleReplacementPlugin()); config.devServer = { contentBase: "./build", historyApiFallback: true, headers: { "Access-Control-Allow-Origin": "*", }, proxy: { "/common/": { target: "http://localhost:8234", pathRewrite: {"^/common" : ""} } } } config.mode = "development" module.exports = config;
????????同樣配置跨域
????????在package.json中添加命令,最終內容如下
{ "name": "portal", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1", "start": "concurrently "npm run start:config" "npm run start:common-deps"", "start:config": "webpack-dev-server --config ./webpack.config.config.dev.js --port 8233", "start:common-deps": "webpack-dev-server --config ./webpack.common-deps.config.dev.js --port 8234", "build": "npm run build:config && npm run build:common-deps", "build:config": "webpack --config ./webpack.config.config.js -p", "build:common-deps": "webpack --config ./webpack.common-deps.config.js -p" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@babel/core": "^7.4.3", "@babel/plugin-syntax-dynamic-import": "7.0.0", "@babel/preset-env": "^7.4.3", "babel-core": "6.26.3", "babel-loader": "8.0.0", "clean-webpack-plugin": "0.1.19", "concurrently": "^4.1.1", "copy-webpack-plugin": "4.5.2", "css-loader": "1.0.0", "style-loader": "0.23.0", "webpack": "4.17.1", "webpack-cli": "3.1.0", "webpack-dev-server": "^3.1.14" } }
????????至此,整個portal項目就搭建完畢了,下一篇,我們實現menu項目和project1項目
????????項目源碼
????????Portal源碼
????????微前端 —— 理論篇
????????微前端 —— menu&&project1(React)
????????微前端 —— project2項目(VUE)
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/106341.html
摘要:現在開始從頭搭建我們的微前端架構。項目源碼微前端項目微前端微前端項目 1. 微前端 ????????基于spa類的單頁應用,隨著企業工程項目的體積越來越大,開發的功能越來越多,頁面也越來越多,項目隨之也變得越來越臃腫,維護起來十分困難,往往改一個logo,或者改一個小樣式,都要將項目全部重新打包一遍,維護困難,部署也困難。 ????????因此前端在借鑒后端的微服務架構模式后,衍生了...
摘要:前言微前端理論篇微前端項目微前端前一篇文章講解了項目在微前端架構中的應用,本篇為最后一篇,項目在此架構中的應用。項目我們就不自己搭建了,直接使用腳手架生成。 前言 ????????微前端 —— 理論篇????????微前端 —— portal項目????????微前端 —— menu&&project1(React)????????前一篇文章講解了react項目在single-spa微...
摘要:前言微前端理論篇微前端項目上一篇中,我們完成了項目的搭建,算是完成了整個微前端架構的一半工程了。項目項目是作為頁面的菜單顯示的,主要用于路由的控制。源碼地址源碼地址項目源碼地址微前端理論篇微前端項目微前端項目 前言 ????????微前端 —— 理論篇????????微前端 —— portal項目????????上一篇中,我們完成了portal項目的搭建,算是完成了整個微前端架構的一半...
摘要:前端中的計算機領域的通常認為起源于。并對其主要內容作了自己的解讀。搬到另一個地區會導致名氣降低。年度報告,年最受歡迎的編程語言年上最流行的種編程語言及前十最火熱的項目排行榜,分別由及登頂。技術周刊由小組出品,匯聚一周好文章,周刊原文。 showImg(https://segmentfault.com/img/bVWHC4?w=1000&h=710); 本期推薦 反擊爬蟲,前端工程師的腦...
閱讀 1561·2021-11-24 09:39
閱讀 1042·2021-11-22 15:11
閱讀 2167·2021-11-19 11:35
閱讀 1627·2021-09-13 10:37
閱讀 2453·2021-09-03 10:47
閱讀 2134·2021-08-30 09:47
閱讀 1626·2021-08-20 09:39
閱讀 2901·2019-08-30 14:13