首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gatsby 3-“Gatsby构建”在“生成带有错误的静态HTML页面”上失败,“WebpackError: ReferenceError:.未定义”(firebase/app)

Gatsby 3-“Gatsby构建”在“生成带有错误的静态HTML页面”上失败,“WebpackError: ReferenceError:.未定义”(firebase/app)
EN

Stack Overflow用户
提问于 2021-04-03 18:30:55
回答 2查看 2.1K关注 0票数 3

运行gatsby build时,进程在Gatsby v3上失败。

--一个上下文.--火基模块包含在定制钩子中(代码显示如下所示)。

gatsby节点中有一个自定义的webpack配置函数,它使用webpack外部特性将构建阶段的模块加载推迟到用户浏览器中的运行时(下面的代码显示这一点)。

我已经将所有相关的包更新到当前版本,并且没有相关的依赖错误或过时的包。

如果需要任何其他内容,请告诉我,下面有更多的信息和例子。

gatsby build 会产生以下错误:

代码语言:javascript
复制
success Building HTML renderer - 17.646s
failed Building static HTML for pages - 0.562s

 ERROR #95313 

Building static HTML failed

See our docs page for more info on this error: https://gatsby.dev/debug-html


> 1 | module.exports = __WEBPACK_EXTERNAL_MODULE_firebase_app__;
    | ^


  WebpackError: ReferenceError: __WEBPACK_EXTERNAL_MODULE_firebase_app__ is not defined
  
  - app":1 
    /external "firebase/app":1:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - useFirebase.js:7 
    /src/hooks/useFirebase.js:7:18
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - sync-requires.js:7 
    /.cache/_this_is_virtual_fs_path_/$virtual/sync-requires.js:7:49
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - static-entry.js:11 
    /.cache/static-entry.js:11:22
  
  - utils.js:283 
    /[@gatsbyjs]/reach-router/lib/utils.js:283:1
  
  - plugin.js:5 
    /[@savvywombat]/tailwindcss-grid-areas/src/plugin.js:5:1
  
  - utils.js:163 
    /[@gatsbyjs]/reach-router/lib/utils.js:163:1

在从盖茨比2到盖茨比3的更新之后,我修正了一个错误,即自盖茨比2到盖茨比3更新后,定制webpack外部函数被错误地调用。

gatsby节点中配置webpack外部的代码可能是直接相关的,目前看起来如下:

代码语言:javascript
复制
exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
  if (stage === 'build-html' || stage === 'develop-html') {
    actions.setWebpackConfig({
      externals: getConfig().externals.concat(({ context, request }, callback) => {
        const regex = /^@?firebase(\/(.+))?/
        if (regex.test(request)) {
          return callback(`umd ${request}`)
        }
        callback()
      })
    })
  }
}

由于这似乎也与firebase模块直接相关,下面的是来自我的自定义useFirebase钩子的导入行(以前是从另一个堆栈溢出线程或github问题中提取的,并在gatsby 2中工作。

代码语言:javascript
复制
...
import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/firestore'
import 'firebase/storage'
import 'firebase/messaging'

let app
let messaging

const config = {
...
}

// don't initialize untill you're in the browser - required to work with Gatsby
if (typeof window !== 'undefined') {
  app = app || firebase.initializeApp(config)

相关模块版本和package.json的解决方案(我认为这将是全部):

代码语言:javascript
复制
  "dependencies": {
    "gatsby": "^3.2.1",
    "@firebase/app": "^0.6.18",
    "@firebase/auth": "^0.16.4",
    "@firebase/firestore": "^2.2.2",
    "@firebase/messaging": "^0.7.7",
    "@firebase/storage": "^0.4.6",
    ...
  }
  "resolutions": {
    "graphql": "^15.4.0",
    "graphql-compose": "^7.25.0",
    "webpack": "^5.24.2"
  }

如果有人在这个问题上有任何经验或想法,我将不胜感激,谢谢!

Update I尝试用以下备用代码替换onCreateWebpackConfig:

代码语言:javascript
复制
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
  if (stage === 'build-html') {
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /^@?firebase(\/(.+))?/,
            use: loaders.null()
          }
        ]
      }
    })
  }
}

,它消除了旧错误,并创建了以下错误:

代码语言:javascript
复制
failed Building static HTML for pages - 0.737s

 ERROR #95313 

Building static HTML failed

See our docs page for more info on this error: https://gatsby.dev/debug-html


  85 | ]);
  86 |
> 87 | proxyRequestMethods(Index, '_index', IDBIndex, [
     | ^
  88 |   'get',
  89 |   'getKey',
  90 |   'getAll',


  WebpackError: ReferenceError: IDBIndex is not defined
  
  - idb.mjs:87 
    /[idb]/lib/idb.mjs:87:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - bootstrap:19 
    /webpack/bootstrap:19:1
  
  - sync-requires.js:7 
    /.cache/_this_is_virtual_fs_path_/$virtual/sync-requires.js:7:49
  
  - bootstrap:19 
    /webpack/bootstrap:19:1

但是,我不确定这是否比上次的尝试更远.

EN

回答 2

Stack Overflow用户

发布于 2021-04-03 21:53:04

您似乎进一步尝试了最后一次尝试,因为Firebase实现不是主要方法失败,而是实现本身的失败。

几周前,我也面临着同样的问题,我遵循了这个GitHub螺纹,在这里提供的解决方案帮助了我。在您的系统中,由于它是一个自定义实现,因此您可能需要对其进行稍微修改。

这种思想依赖于延迟加载Firebase模块导入(动态导入)。在使用Firebase的最高组件中,按如下方式导入:

代码语言:javascript
复制
import('firebase').then(firebase => {
  firebase.initializeApp({ /* firebaseConfig goes here */});
  firebase.firestore().collection('items').doc('yJd1Fs5Ampttq6QKBoYF').get()
    .then(doc => {
      // do stuff with Firestore data
    });
});

我建议阅读以下内容:https://kyleshevlin.com/firebase-and-gatsby-together-at-last,您可以给出一些建议。

票数 1
EN

Stack Overflow用户

发布于 2021-07-28 18:23:51

请试试这个设置。

代码语言:javascript
复制
exports.onCreateWebpackConfig = ({
  stage,
  actions,
  getConfig
}) => {
  if (stage === 'build-html') {
    actions.setWebpackConfig({
      externals: getConfig().externals.concat(function(context, request, callback) {
        const regex = /^@?firebase(\/(.+))?/;
        // exclude firebase products from being bundled, so they will be loaded using require() at runtime.
        if (regex.test(request)) {
          return callback(null, 'commonjs ' + request); // <- use commonjs!
        }
        callback();
      })
    });
  }
};

我提到以下意见。重点是要使用常识。https://github.com/firebase/firebase-js-sdk/issues/2222#issuecomment-538072948

我已经回答了同样的问题。没有定义带有Firebase的GatsbyJS - WebpackError: ReferenceError: IDBIndex

祝你好运。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66934602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档