当前位置:首页 » 《随便一记》 » 正文

手写小程序摇树优化工具(四)——遍历wxml、wxss、wxs文件

13 人参与  2022年11月14日 18:14  分类 : 《随便一记》  评论

点击全文阅读


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

github: miniapp-shaking

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

1. wxml文件的遍历

wxml文件有三种方式可以引入外部依赖:importincludewxs,解析wxml文件需要用到htmlparser2,需要注意htmlparser2解析出来的标签名全部会转化为小写字母,例如<Detail/>,解析之后的标签是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;  }

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;  }

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;  }

4. 处理静态文件

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

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

欲知后文请关注下一章。

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


点击全文阅读


本文链接:http://zhangshiyu.com/post/48409.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 苔藓爬满旧日诺言全书+后续+结局顾砚廷慕晚夏免费苔藓爬满旧日诺言全书+后续+结局顾砚廷慕晚夏全书全
  • [我掉蛇女马甲后,点天灯假死丈夫悔疯了]免费试读_婆婆丁若菱蛇女精彩节选推荐
  • 他日若是同淋雪结局+番外(夏尔若林闻舟)他日若是同淋雪结局+番外结局_夏尔若林闻舟列表_笔趣阁(他日若是同淋雪结局+番外)
  • 他心非石反转剧情试读片段_安茗宝宝心掏后续更新+番外
  • 许星森纪冰雪(日暮青山绿渐隐许星森纪冰雪结局+番外)_(许星森纪冰雪)列表_笔趣阁(日暮青山绿渐隐许星森纪冰雪结局+番外)
  • 全文潮痕蚀月光(池清禾***宸)列表_全文潮痕蚀月光
  • 「江月随人处处圆」小说无删减版在线免费阅读_[陆晨小姐孟苒]精彩章节免费试读
  • 阮雾梨闻砚辞阮见微结局+番外全书+后续+结局(闻砚辞阮雾梨)列表_阮雾梨闻砚辞阮见微结局+番外(闻砚辞阮雾梨)阮雾梨闻砚辞阮见微结局+番外全书+后续+结局在线
  • 潮痕蚀月光结局+番外池清禾***宸(潮痕蚀月光结局+番外)全书免费池清禾***宸_潮痕蚀月光结局+番外池清禾***宸列表_笔趣阁(池清禾***宸)
  • 苔藓爬满旧日诺言一口气读完全书+后续全书+后续+结局(慕晚夏顾砚廷)列表_苔藓爬满旧日诺言一口气读完全书+后续(慕晚夏顾砚廷)苔藓爬满旧日诺言一口气读完全书+后续全书+后续+结局在线
  • 「孕弟」反转剧情碎片化试读_[耀祖弟弟子宫]小说精彩节选试读
  • 旧梦随风去结局+番外(姜予宁沈昭寒)列表_旧梦随风去结局+番外(姜予宁沈昭寒)全书+后续+结局在线

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1