美文网首页代码覆盖率(前端/后端)
关于babel-plugin-istanbul与babel-pl

关于babel-plugin-istanbul与babel-pl

作者: sw_saii | 来源:发表于2021-01-04 09:57 被阅读0次

问题:

Does not work with babel-plugin-import

使用babel7后,按需加载执行报错

以上两个问题都是关于同时使用了istanbul与import的babel插件以后导致的问题。


20201218090823471.png

编译后的前端代码运行后就会报上述的错误。

分析

首先通过上面的issue我们先清楚的了解到一个问题。
为什么

const { Option } = Select;

就存在有问题,但是通过

const Option = Select.Option;

就没有问题呢?

这里我们就需要先了解经过istanbul插桩后变成了怎么样内容了, 我们可以来看下。


const {
  Option
} = (cov_1eldv8eouu().s[0]++, Select.Option);

const {
  Option
} = (cov_1eldv8eouu().s[0]++, Select);

以上就是经过babel解析后的结果,那么这两个给到babel-plugin-import解析后有什么区别呢?

这里就需要我们先确认下 这两个地方首先他经过babel解析成ast的结果是什么样的了?

const {
  Option
} = (cov_1eldv8eouu().s[0]++, Select.Option);
在这里插入图片描述

可以看到这里有个MemberExpression

 MemberExpression(path, state) {
    const { node } = path;
    const file = (path && path.hub && path.hub.file) || (state && state.file);
    const pluginState = this.getPluginState(state);

    // multiple instance check.
    if (!node.object || !node.object.name) return;

    if (pluginState.libraryObjs[node.object.name]) {
      // antd.Button -> _Button
      path.replaceWith(this.importMethod(node.property.name, file, pluginState));
    } else if (pluginState.specified[node.object.name] && path.scope.hasBinding(node.object.name)) {
      const { scope } = path.scope.getBinding(node.object.name);
      // global variable in file scope
      if (scope.path.parent.type === 'File') {
        node.object = this.importMethod(pluginState.specified[node.object.name], file, pluginState);
      }
    }
  }

可以看到babel-plugin-import 是否这块相应的处理的。
但是针对

const {
  Option
} = (cov_1eldv8eouu().s[0]++, Select);

babel解析的结果为

在这里插入图片描述

babel-plugin-import并没有单独针对Identifier的处理逻辑的。

针对这个问题的解决如下:

buildDeclaratorHandler(node, prop, path, state) {
    const file = (path && path.hub && path.hub.file) || (state && state.file);
    const { types } = this;
    const pluginState = this.getPluginState(state);

    const checkScope = targetNode =>
      pluginState.specified[targetNode.name] && // eslint-disable-line
      path.scope.hasBinding(targetNode.name) && // eslint-disable-line
      path.scope.getBinding(targetNode.name).path.type === 'ImportSpecifier'; // eslint-disable-line

    if (types.isIdentifier(node[prop]) && checkScope(node[prop])) {
      node[prop] = this.importMethod(pluginState.specified[node[prop].name], file, pluginState); // eslint-disable-line
    } else if (types.isSequenceExpression(node[prop])) {
      node[prop].expressions.forEach((expressionNode, index) => {
        if (types.isIdentifier(expressionNode) && checkScope(expressionNode)) {
          node[prop].expressions[index] = this.importMethod(
            pluginState.specified[expressionNode.name],
            file,
            pluginState,
          ); // eslint-disable-line
        }
      });
    }
  }

VariableDeclarator中做处理。

在这里插入图片描述

这样子就能够解析到上述的问题了。

相关文章

  • 关于babel-plugin-istanbul与babel-pl

    问题: Does not work with babel-plugin-import[https://github...

  • 20170313

    进入优猴的第三个月零一天, 关于丑小鸭与白天鹅 关于读书与旅行 关于生命与信仰 关于刚与柔 关于.......

  • 我们都是宇宙中的微尘

    关于人生存在的意义,关于开悟与尘世的关系,关于爱与美的追寻,关于死亡与快乐,即使与宇宙相比,我们的存在如同蜉蝣般渺...

  • 2020-10-04

    《偷窥一百二十天》这几天,一直在看这本书。关于偷窥与自鉴,关于爱情与背叛。关于暗恋与跟踪。关于白天鹅与黑天鹅,关于...

  • 十日谈(2):圣经之圣

    不喜欢争论。恭喜你我不在一个频道上。 关于光与影,罪与难,迁与遇,罚与惩;关于鹰及隼,关于老幼别,关于成长与成全。...

  • 原魄

    ——作者:落风 关于叛逆少女与奇幻少年,关于人类与自然,关于爱与成长。 【一】 汽车从城市出发,穿过柏油马路,来到...

  • 无标题文章

    关于与

  • 为诗灼伤 | 雨

    诗/耳风 关于,墙角玫瑰 与写给你的信 关于,每次缠绵 与滥情的夜晚 关于,彻夜长聊 与爱情的微妙 我爱你 只字未...

  • 荼蘼花开 看红颜沉浮

    ——读《陆小曼传》有感 关于爱与背叛,生活与生存,关于优雅与庸俗,张扬与隐忍,都是这个叫做陆...

  • 影评|从电影《匹诺曹》看匹诺曹的性格特点和给我们的启示

    文/阿斌 原创 一个关于善良与邪恶的故事 一个关于诚实与谎言的故事 一个关于勤劳与懒惰的故事 一个关于儿童成长的故...

网友评论

    本文标题:关于babel-plugin-istanbul与babel-pl

    本文链接:https://www.haomeiwen.com/subject/qggjoktx.html