无法使用Gatsby和Typescript从gatsby-config.js文件中读取siteMetadata对象

时间:2019-06-30 17:45:51

标签: node.js reactjs typescript babel gatsby

我正在尝试读取在gatsby-config.js文件中定义的siteMetadata对象的内容,但是每次尝试都会出现此错误

event queue

我正在将Gatsby与打字稿一起使用。我在function main { begin { $global:latestPortMessage = '' $form = createForm $port = createAndOpenPort } process { subscribeToPortEvents $port [void]$form.ShowDialog() } end { } } ##### GUI CREATION HELPERS function createForm { Add-Type -AssemblyName System.Windows.Forms [Windows.Forms.Application]::EnableVisualStyles() $FormStartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen $form = New-Object system.Windows.Forms.Form $form.AutoSize = $true $form.text = "Serial-test" $form.TopMost = $false $form.StartPosition = $FormStartPosition $clickButton = createClickButton $closeButton = createCloseButton $form.controls.add($clickButton) $form.controls.add($closeButton) $form } function createClickButton { $button = New-Object system.Windows.Forms.Button $button.BackColor = "#e66b6b" $button.text = "Click" $button.width = 100 $button.height = 100 $button.Font = 'Microsoft Sans Serif,10,style=Bold' $button.margin = 0 $button.Add_Click({ onClickButton }) $button } function createCloseButton { $button = New-Object system.Windows.Forms.Button $button.BackColor = "#e66b6b" $button.text = "Close" $button.width = 100 $button.height = 100 $button.location = '100,0' $button.Font = 'Microsoft Sans Serif,10,style=Bold' $button.margin = 0 $button.Add_Click({ onCloseButton }) $button } function onClickButton { Write-Host "Click" } function onCloseButton { closePort unsubscribeFromPortEvents $form.close() } ##### PORT RELATED HELPERS function createAndOpenPort { param($portName = 'COM7', $baudRate = 9600) $port = New-Object System.IO.Ports.SerialPort $portName,$baudRate,None,8,one $port.open() $port } function subscribeToPortEvents { param($port) Register-ObjectEvent -InputObject $port -EventName "DataReceived" -Action { # $value = $Sender.ReadExisting() $value = $Sender.ReadLine() if ($value) { Write-Host "GOT VALUE FROM READLINE: $value" $global:latestPortMessage = $value.substring(0, $value.length - 2) } } | Out-Null } function unsubscribeFromPortEvents { $subscribers = Get-EventSubscriber $subscribers | foreach-object { if ($_.SourceObject.GetType().FullName -eq "System.IO.Ports.SerialPort") { Unregister-Event -SourceIdentifier $_.SourceIdentifier } } } function closePort { try { if ($port) { if ($port.isOpen) { $port.Close() } } } catch { logError $_ dumpError $_ } } . main 上安装了插件ERROR in ./src/pages/index.tsx Module build failed (from ./node_modules/gatsby/dist/utils/babel-loader.js): TypeError: Cannot read property 'type' of null at VariableDeclarator (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/babel-plugin-remove-graphql-queries/index.js:277:81) at NodePath._call (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/path/context.js:53:20) at NodePath.call (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/path/context.js:40:17) at NodePath.visit (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/path/context.js:88:12) at TraversalContext.visitQueue (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/context.js:118:16) at TraversalContext.visitMultiple (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/context.js:85:17) at TraversalContext.visit (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/context.js:144:19) at Function.traverse.node (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/index.js:94:17) at NodePath.visit (/mnt/d/Repositories/FnStacks/HandyTrade/handytrade-store-web/node_modules/@babel/traverse/lib/path/context.js:95:18) at TraversalContext.visitQueue ℹ 「wdm」: Failed to compile. 和所需的设置。我正在尝试在项目的起始页中读取siteMetadata对象。我尝试使用gatsby-plugin-ts-loader库中的gatsby-config.jsgraphql,但失败了。这是我的前进方式

useStaticQuery

这是我在gatsby-config.js中定义SiteMetada对象的方式

gatsby

如果我删除所有读取siteMetadata的查询,则应用程序将正常运行。我正在Linux的Windows子系统上使用gatsby-Ubuntu 18.04.1

1 个答案:

答案 0 :(得分:1)

当我在您的根目录中运行tsc -p .并检查输出时,我发现tsc将其编译:

  const data = useStaticQuery(graphql`
    query IndexQuery {
      site {
        siteMetadata {
          title
        }
      }
    }
  `);

对此:

var data = useStaticQuery(graphql(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n    query IndexQuery {\n      site {\n        siteMetadata {\n          title\n        }\n      }\n    }\n  "], ["\n    query IndexQuery {\n      site {\n        siteMetadata {\n          title\n        }\n      }\n    }\n  "]))));

Gatsby期望graphql查询使用模板文字,但是tsc将其编译为常规函数。

要解决此问题,您需要将tsconfig更改为至少输出es6

// tsconfig.json
{
  compileOptions: {
    "module": "esnext",
    "target": "esnext", <-- or es6 & above
  }
}

顺便说一句,我最近为gatsby发布了一个ts插件,旨在使在打字稿中使用gatsby尽可能流畅。它做的另一件事是自动为graphql查询生成类型。如果您尝试一下,不胜感激,请告诉我: