简体   繁体   English

Babel插件错误未知类型为undefined的构造函数“ String”的节点

[英]Babel plugin error unknown node of type undefined with constructor “String”

I am building a shared component library that will be used by both React and Vue. 我正在构建一个共享组件库,React和Vue都将使用它。

I am using Styletron, which requires a framework-specific adapter, but otherwise works pretty much the same. 我正在使用Styletron,它需要特定于框架的适配器,但其他方面的工作原理几乎相同。

So from my source code (a bunch of functions) I need to generate a folder with the code transpiled as normal and then another folder where the functions are modified slightly. 因此,从我的源代码(一堆函数)中,我需要生成一个文件夹,并按正常方式编译代码,然后再生成另一个文件夹,其中对函数进行了一些修改。

This is my code: 这是我的代码:

const MyComponent = styled('div', ({ isActive, hasBorder}) => ({
  color: 'red'
}))

// should not change
const OtherComponent = styled('div', {
  background: 'blue'
})

And it should become: 它应该变成:

const MyComponent = styled('div', ({ isActive, hasBorder}) => ({
  color: 'red'
}), ['isActive', 'hasBorder'])

const OtherComponent = styled('div', {
  background: 'blue'
})

I actually have a working example of this working in ASTExplorer , but when I try to make a plugin out of it, I encounter the error Babel plugin error unknown node of type undefined with constructor "String" 我实际上有一个在ASTExplorer中工作的工作示例,但是当我尝试从中制作插件时,遇到Babel plugin error unknown node of type undefined with constructor "String"的错误Babel plugin error unknown node of type undefined with constructor "String"

This is my first plugin, and I KNOW that I am doing some stuff wrong, but right now I just need to find out what I have to do to make this work outside of ASTExplorer . 这是我的第一个插件,我知道我做错了一些事情,但是现在我只需要找出在ASTExplorer之外进行这项工作所需要做的事情

This is the plugin I have written in ASTExplorer: 这是我在ASTExplorer中编写的插件:

export default function(babel) {
  const { types: t } = babel;
  return {
    name: "ast-transform",
    visitor: {
      CallExpression: (path) => {
        if (
          path.node.callee.name === 'styled' && 
          path.node.arguments.length === 2 && 
          t.isArrowFunctionExpression(path.node.arguments[1])
        ) {
          if (t.isObjectPattern(path.node.arguments[1].params[0])) {
            const props = []
            path.node.arguments[1].params[0].properties.map(prop => props.push(prop.value.name))
            path.node.arguments.push(JSON.stringify(props)) // I suspect that the error is from here
          }
        } 

      }
    }
  };
}

Babel transforms work with AST nodes, so stringifying the props and pushing them into the arguments list in this way won't work quite right. Babel变换可与AST节点一起使用,因此将道具串化并以这种方式将其推入参数列表将无法正常工作。 You'll want to actually create an AST structure from your object. 您实际上要从对象创建AST结构。

In this case, Babel offers a helper for this, so you can change 在这种情况下,Babel为此提供了一个帮助者,因此您可以更改

path.node.arguments.push(JSON.stringify(props))

to

path.node.arguments.push(t.valueToNode(props))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM