使用 requirejs 加载 firebase js 时出错

Error loading firebase js with requirejs

我像这样用 RequireJS 加载 Firebase js 库:

require([
    'firebase-app',
    'firebase-storage'
], function (firebase) {
    firebase.initializeApp({...});
    firebase.storage();
});

直到 Firebase js 发布 4.12.1 一切正常,自 4.13.0 发布以来我在 "firebase.storage()" 调用时遇到这些错误:

TypeError: Cannot read property 'registerService' of undefined at firebase (index.esm.js:3442) at index.esm.js:3434

Uncaught Error: Cannot instantiate firebase-storage - be sure to load firebase-app.js first. at firebase (index.esm.js:3444) at index.esm.js:3434

在 Firebase 4.13.0 发行说明中,我认为这个更改破坏了我的代码: https://firebase.google.com/support/release-notes/js#version_4130_april_19_2018

[CHANGED] Refactored to use core-js for our Polyfills/Shims.

"core-js" 这个库是:https://github.com/zloirock/core-js

这些是 jsfiddle 示例:

在此页面找到解决方案(仅适用于 >= 5.5.6 的 firebase 版本):

https://github.com/firebase/firebase-js-sdk/issues/1266#issuecomment-441405513

你必须像“@firebase/app”那样调用firebase.app.js,所以:

var config = {
    paths: {
        '@firebase/app': 'https://www.gstatic.com/firebasejs/5.7.1/firebase-app',
        '@firebase/storage': 'https://www.gstatic.com/firebasejs/5.7.1/firebase-storage'
    }
};
require([
    '@firebase/app',
    '@firebase/storage'
], function (firebase) {
    firebase.initializeApp({
        ...
    });

    firebase.storage();
});

最终 jsfiddle:https://jsfiddle.net/ptmw0srq/125/