googleapis npm module and webpack bundle error



我尝试在扩展后台从Firebase获取远程配置。

我用npm安装了googleapis模块,并使用webpack来编译和捆绑文件。 当我尝试在我的 js 文件中导入谷歌 apis 时:

import {google} from 'googleapis';

并使用它:const jwtClient = new google.auth.JWT(...);

我在控制台中显示了几个错误:

ERROR in ./~/googleapis/build/src/googleapis.js
Module parse failed: /node_modules/googleapis/build/src/googleapis.js Unexpected token (81:10)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (81:10)
ERROR in ./~/googleapis-common/build/src/authplus.js
Module parse failed: /node_modules/googleapis-common/build/src/authplus.js Unexpected token (32:10)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (32:10)

我试图在 webpack 配置中使用外部函数从捆绑包编译中排除 googleapis,但没有成功

externals: {
googleapis: 'googleapis'
},

带有 Babel 配置加载器的 webpack :

loaders: [{
loader: "babel-loader",
test: /.jsx?$/,
query: {
plugins: ['transform-runtime'],
presets: ['es2015', 'react'],
}
}]

这似乎是googleapis的一个已知问题: https://github.com/googleapis/google-api-nodejs-client/issues/1270#issuecomment-412361803

似乎有一年前的 PR 可以修复它,但同样......它不知道它是否/何时会甚至可以合并到 master 分支。

如果可能的话,我建议尝试将此库用作 CDN 托管依赖项: https://unpkg.com/googleapis@42.0.0/build/src/googleapis.js

跟: https://www.npmjs.com/package/webpack-cdn-inject

最新更新