Gatsby 本地插件构建崩溃

问题描述

我是 Gatsby 新手,我正在学习本教程:https://www.gatsbyjs.com/docs/creating-plugins/ 以学习使用 react-intl 创建 gatsby 本地插件。我将插件放在项目根目录的 plugins 文件夹中。

这是插件的演示代码(Demo Project Repo: https://github.com/85Ryan/gatsby-local-plugin-demo.git

// ./src/wrap-page-element.js
import * as React from 'react'
import { IntlProvider } from 'react-intl'

const message = {
  welcome: 'Welcome',home: 'Home',}

const wrapPageElement = ({ element }) => {
  const locale = 'en'
  return (
    <IntlProvider locale={locale} key={locale} messages={message}>
      {element}
    </IntlProvider>
  )
}

export { wrapPageElement }


// gatsby-browser.js
export { wrapPageElement } from './src/wrap-page-element'

// gatsby-ssr.js
export { wrapPageElement } from './src/wrap-page-element'

当我构建 Gatsby 站点时,它崩溃并显示错误 Webpackerror: [React Intl] Could not find requiredintlobject. <IntlProvider> needs to exist in the component ancestry.

Error Screenshot

但是当我将插件发布到 npm 时,它工作正常。

谁能帮我找出我的问题在哪里?

谢谢!

解决方法

您正在第二次重新导出文件中的 wrapPageElement

// gatsby-browser.js
export { wrapPageElement } from './src/wrap-page-element'

这取代了包含 <IntlProvider /> 的早期导出。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...