簡體   English   中英

在 React Typescript Firebase 項目上未定義導航器

[英]Navigator undefined on React Typescript Firebase project

我已經用谷歌搜索了幾個小時,似乎無法解決我的問題。

我有一個 webpack/React/Typescript/Mobx 設置並且正在嘗試使用 firebase。

這是我的 webpack 配置:(來自 這個repo 的樣板)

var webpack = require('webpack');
var path = require('path');

// variables
var isProduction = process.argv.indexOf('-p') >= 0;
var sourcePath = path.join(__dirname, './src');
var outPath = path.join(__dirname, './dist');

// plugins
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var WebpackCleanupPlugin = require('webpack-cleanup-plugin');

module.exports = {
  context: sourcePath,
  entry: {
    main: './main.tsx'
  },
  output: {
    path: outPath,
    filename: 'bundle.js',
    chunkFilename: '[chunkhash].js',
    publicPath: '/'
  },
  target: 'web',
  resolve: {
    extensions: ['.js', '.ts', '.tsx'],
    // Fix webpack's default behavior to not load packages with jsnext:main module
    // (jsnext:main directs not usually distributable es6 format, but es6 sources)
    mainFields: ['module', 'browser', 'main'],
    alias: {
      app: path.resolve(__dirname, 'src/app/'),
      assets: path.resolve(__dirname, 'src/assets/')
    }
  },
  module: {
    rules: [
      // .ts, .tsx
      {
        test: /\.tsx?$/,
        use: [
          isProduction
            ? 'ts-loader'
            : {
                loader: 'babel-loader',
                options: {
                  babelrc: false,
                  plugins: ['react-hot-loader/babel']
                }
              },
          'ts-loader'
        ],
        // : ['babel-loader?plugins=react-hot-loader/babel&presets=', 'ts-loader'],
        exclude: /node_modules/
      },
      // css
      {
        test: /\.css$/,
        exclude: /node_modules/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              query: {
                modules: true,
                sourceMap: !isProduction,
                importLoaders: 1,
                localIdentName: '[local]__[hash:base64:5]'
              }
            },
            {
              loader: 'postcss-loader',
              options: {
                ident: 'postcss',
                plugins: [
                  require('postcss-import')({ addDependencyTo: webpack }),
                  require('postcss-url')(),
                  require('postcss-cssnext')(),
                  require('postcss-reporter')(),
                  require('postcss-browser-reporter')({
                    disabled: isProduction
                  })
                ]
              }
            }
          ]
        })
      },
      {
        test: /\.css$/,
        include: /node_modules/,
        use: ['style-loader', 'css-loader']
      },
      // static assets
      { test: /\.html$/, use: 'html-loader' },
      { test: /\.(png|jpg)$/, use: 'url-loader?limit=10000' },
      { test: /\.webm$/, use: 'file-loader' }
    ]
  },
  optimization: {
    splitChunks: {
      name: true,
      cacheGroups: {
        commons: {
          chunks: 'initial',
          minChunks: 2
        },
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          chunks: 'all',
          priority: -10
        }
      }
    },
    runtimeChunk: true
  },
  plugins: [
    new WebpackCleanupPlugin(),
    new ExtractTextPlugin({
      filename: 'styles.css',
      disable: !isProduction
    }),
    new HtmlWebpackPlugin({
      template: 'assets/index.html'
    })
  ],
  devServer: {
    contentBase: sourcePath,
    hot: true,
    inline: true,
    historyApiFallback: {
      disableDotRule: true
    },
    stats: 'minimal'
  },
  devtool: 'cheap-module-eval-source-map',
  node: {
    // workaround for webpack-dev-server issue
    // https://github.com/webpack/webpack-dev-server/issues/60#issuecomment-103411179
    fs: 'empty',
    net: 'empty'
  }
};

僅僅通過在我的應用程序中包含 firebase,我就會不斷地遇到這個錯誤:

Uncaught TypeError: Cannot read property 'navigator' of undefined    auth.esm.js?69b5:10 

我已經通過包含一個簡單的組件進行了測試,如下所示:

import * as React from 'react';
import * as Styles from './styles.css';
import 'app/utils/FirebaseUtil';

interface TestProps {}

export const Test: React.StatelessComponent<TestProps > = () => (
    <div className={Styles.root}>
        {'Hello World'}
    </div>
);

FirebaseUtil:

import * as firebase from 'firebase';

const config = {
  apiKey: '**my key here**',
  authDomain: '** my domain here **'
};

firebase.initializeApp(config);

export const fbAuth = firebase.auth;

無論我似乎做什么,我都會收到導航器錯誤。 即使我不導出 auth 對象。 據我所知,它與根據這個SO 問題添加嚴格模式的 babel-loader 相關,我想? 所有其他相關搜索似乎都與 firebase-ui 有關,我沒有以任何方式使用它。

但我不知道他是如何設法關閉嚴格模式的,更不用說 OP 沒有使用打字稿,在這種情況下我使用的是 ts-loader。 我一生都無法弄清楚如何讓它發揮作用。 除了所有這些,如果我嘗試將 firebase 對象用於 auth() 例如,我會從 webpack 收到一堆關於 firebase 對象上不存在 auth 的警告。 完全被難住了。

所以萬一其他人遇到這個問題。 看來是包版本問題。 我假設我使用的 樣板文件中特別包含的軟件包版本與 firebase 不兼容。

我更新了typescriptreact-hot-loader和最有可能的問題webpack3.0.44.12.1版本,現在看起來一切正常。 還有更新,我現在像這樣導入 firebase:

import firebase from '@firebase/app';
import '@firebase/auth';

希望這可以幫助某人。

在我的情況下,我修復了這個導入功能

import firebase from 'firebase/app'
import 'firebase/functions'
import 'firebase/analytics'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM