DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • BFC 块级格式化上下文
  • Note
  • WebAssembly
  • public api
  • 位运算
  • bitwise operator
  • css实现隐藏效果
  • css snippets
  • 抖音点赞
  • js 相等判断
  • fetch ReadableStream
  • git
  • Github Actions 工作流
  • google search
  • RPC vs HTTP
  • gravatar
  • hhkb
  • Init project
  • input 文件上传
  • mac

    • Mac 使用技巧
    • alfred
    • mac shortcuts
    • shortcuts text edit
    • mac 修改host
  • 微前端
  • mock
  • nginx dump
  • nginx
  • NirCmd
  • npm
  • Operator Precedence
  • package.json
  • url query 解析
  • pnpm
  • JavaScript Precise countdown
  • react 模版
  • regexp
  • setup web development
  • telegram

    • telegram bot
  • timeFunction ease
  • 视频裁剪
  • vscode

    • vscode 高级指南
    • bracketPairs
    • jsconfig.json
    • vscode pipe into code
    • social project
    • vscode tasks
  • draggable resizable
  • windows 激活
  • 前端截图实现
  • 文本配音 富文本实现
  • 图片处理
  • 前端坐标
  • 定时任务
  • work efficient
  • 微信小程序动画实现方案
  • 排列组合
  • 数列
  • 语音驱动文字
  • 浏览器
  • 状态管理
  • 移动盒子
  • 移动端开发常用snippets
  • 设计模式
  • web performance

微前端

html entry

html 解析获取资源文件

1. 需要提取的资源类型

  • CSS:通过 link 标签中的 rel="stylesheet" 来识别,属性 href 为 CSS 文件路径。
  • JS:通过 script 标签,属性 src 为 JS 文件路径。
  • 图片:通过 img 标签,属性 src 为图片路径。

2. 浏览器环境下的实现

如果你在浏览器环境下,可以直接通过 DOM 解析 HTML 字符串。下面是 JavaScript 的实现代码:

function extractResources(htmlString: string) {
    // 创建一个临时的 DOM 元素,加载 HTML 字符串
    const parser = new DOMParser();
    const doc = parser.parseFromString(htmlString, 'text/html');

    // 获取所有的 link 标签(CSS)
    const cssLinks = Array.from(doc.querySelectorAll('link[rel="stylesheet"]'))
        .map(link => link.getAttribute('href'))
        .filter(Boolean); // 过滤掉空的 href

    // 获取所有的 script 标签(JS)
    const jsScripts = Array.from(doc.querySelectorAll('script[src]'))
        .map(script => script.getAttribute('src'))
        .filter(Boolean); // 过滤掉空的 src

    // 获取所有的 img 标签(图片)
    const imgSources = Array.from(doc.querySelectorAll('img[src]'))
        .map(img => img.getAttribute('src'))
        .filter(Boolean); // 过滤掉空的 src

    // 返回一个包含所有资源的对象
    return {
        css: cssLinks,
        js: jsScripts,
        images: imgSources,
    };
}

// 测试 HTML 字符串
const htmlString = `
    <html>
      <head>
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" href="theme.css">
        <script src="script.js"></script>
      </head>
      <body>
        <img src="image1.png">
        <img src="image2.jpg">
      </body>
    </html>
`;

// 提取资源
const resources = extractResources(htmlString);
console.log(resources);

3. Node.js 环境下的实现

在 Node.js 环境中,可以使用 jsdom 或类似库来解析 HTML 字符串,提取资源。

步骤:

  1. 安装 jsdom:

    npm install jsdom
    
  2. 使用 jsdom 解析 HTML,提取资源:

import { JSDOM } from 'jsdom';

function extractResourcesFromHtml(htmlString: string) {
    // 使用 JSDOM 解析 HTML 字符串
    const dom = new JSDOM(htmlString);
    const document = dom.window.document;

    // 获取所有的 link 标签(CSS)
    const cssLinks = Array.from(document.querySelectorAll('link[rel="stylesheet"]'))
        .map(link => link.getAttribute('href'))
        .filter(Boolean); // 过滤掉空的 href

    // 获取所有的 script 标签(JS)
    const jsScripts = Array.from(document.querySelectorAll('script[src]'))
        .map(script => script.getAttribute('src'))
        .filter(Boolean); // 过滤掉空的 src

    // 获取所有的 img 标签(图片)
    const imgSources = Array.from(document.querySelectorAll('img[src]'))
        .map(img => img.getAttribute('src'))
        .filter(Boolean); // 过滤掉空的 src

    // 返回一个包含所有资源的对象
    return {
        css: cssLinks,
        js: jsScripts,
        images: imgSources,
    };
}

// 测试 HTML 字符串
const htmlString = `
    <html>
      <head>
        <link rel="stylesheet" href="style.css">
        <link rel="stylesheet" href="theme.css">
        <script src="script.js"></script>
      </head>
      <body>
        <img src="image1.png">
        <img src="image2.jpg">
      </body>
    </html>
`;

// 提取资源
const resources = extractResourcesFromHtml(htmlString);
console.log(resources);

Refer

qiankun 实现解析

Last Updated:
Contributors: rosendo
Next
mock