码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 手写小程序摇树工具(四)——遍历wxml、wxss、wxs文件


    不自见,故明;不自是,故彰;不自伐,故有功;不自矜,故长;夫唯不争,故天下莫与之争

    github: miniapp-shaking

    上一章我们介绍了遍历json文件的方法,接下来我们介绍其他文件的遍历。

    1. wxml文件的遍历

    wxml文件有三种方式可以引入外部依赖:import,include,wxs,解析wxml文件需要用到htmlparser2,需要注意htmlparser2解析出来的标签名全部会转化为小写字母,例如,解析之后的标签是detail。

    /**
       * 搜集wxml依赖
       * @param file
       * @returns {[string]}
       */
      wxmlDeps(file) {
        const deps = [];
        const dirName = path.dirname(file);
        const content = fse.readFileSync(file, 'utf-8');
        const htmlParser = new htmlparser2.Parser({
          onopentag(name, attribs = {}) {
            // wxml中包括了这三种导入
            if (attribs.src && (name === 'import' || name === 'include' || name === 'wxs')) {
              const { src } = attribs;
              let wxmlFile;
              if (src.startsWith('/')) {
                // 处理绝对路径
                wxmlFile = path.join(this.config.sourceDir, src.slice(1));
              } else {
                // 处理相对路径
                wxmlFile = path.resolve(dirName, src);
              }
              if (fse.existsSync(wxmlFile)) {
                deps.push(wxmlFile);
              }
            }
          },
        });
        htmlParser.write(content);
        htmlParser.end();
        return deps;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    2. 遍历wxss文件

    wxss文件直接使用以下正则表达式匹配即可
    /@import\s+['"](.*)['"];?/g

    /**
       * 搜集wxss依赖
       * @param file
       * @returns {[]}
       */
      wxssDeps(file) {
        const deps = [];
        const dirName = path.dirname(file);
        const content = fse.readFileSync(file, 'utf-8');
        // wxss导入依赖的正则匹配表达式
        const importRegExp = /@import\s+['"](.*)['"];?/g;
        let matched;
        while ((matched = importRegExp.exec(content)) !== null) {
          if (matched[1]) {
            let wxssFile;
            if (matched[1].startsWith('/')) {
              // 处理绝对路径
              wxssFile = path.join(this.config.sourceDir, matched[1].slice(1));
            } else {
              // 处理相对路径
              wxssFile = path.resolve(dirName, matched[1]);
            }
    
            if (fse.existsSync(wxssFile)) {
              deps.push(wxssFile);
            }
          }
        }
        return deps;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    3. 遍历wxs文件

    wxs只支持相对路径导入,在一章我有提到过。

    /**
       * 搜集wxs文件依赖
       * wxs文件只支持require导入相对路径
       * @param filePath
       * @returns {[]}
       */
      wxsDeps(filePath) {
        const deps = [];
        const dirname = path.dirname(filePath);
        // 读取js内容
        const content = fse.readFileSync(filePath, 'utf-8');
        // 将代码转化为AST
        const ast = parse(content, {
          sourceType: 'module',
          plugins: ['exportDefaultFrom'],
        });
        // 遍历AST
        traverse(ast, {
          CallExpression: ({ node }) => {
            if (
              node.callee.name && node.callee.name === 'require'
              && node.arguments.length >= -1
            ) {
              const [{ value }] = node.arguments;
              const jsFile = this.transformWxs(dirname, value);
              if (jsFile) {
                deps.push(jsFile);
              }
            }
          },
        });
        return deps;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    4. 处理静态文件

    对于静态文件我们直接拷贝,以保证其相对于其它文件路径的正确性。原因第一章有过说明,这里不在重复。

    至此,我们的所有文件的依赖收集的方法就已经完成了,下一章我们从主包和子包入手,分别处理这些依赖。

    欲知后文请关注下一章。

    连载文章链接:
    手写小程序摇树工具(一)——依赖分析介绍
    手写小程序摇树工具(二)——遍历js文件
    手写小程序摇树工具(三)——遍历json文件
    手写小程序摇树工具(四)——遍历wxml、wxss、wxs文件
    手写小程序摇树工具(五)——从单一文件开始深度依赖收集
    手写小程序摇树工具(六)——主包和子包依赖收集
    手写小程序摇树工具(七)——生成依赖图
    手写小程序摇树工具(八)——移动独立npm包
    手写小程序摇化工具(九)——删除业务组代码

  • 相关阅读:
    spark集群部署(第三弹)
    WPF使用MVVM(三)-事件转命令
    python scanpy spatial空转全流程
    大二下个人发展小结
    Office技巧(持续更新)(Word、Excel、PPT、PowerPoint、连续引用、标题、模板、论文)
    Google Chrome 旧版本下载地址
    Android kotlin内联函数(inline)的详解与原理
    MongoDB 数组查询($elemMatch)、更新操作(占位符$)详解
    PAM从入门到精通(二十三)
    若要对多态类进行深拷贝,应使用虚函数的clone,而不是公开的拷贝构造赋值
  • 原文地址:https://blog.csdn.net/qq_28506819/article/details/127715455
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号