如何在云功能中使用ES6(esm)导入/导出

如何在云功能中使用ES6(esm)导入/导出

问题描述:

import functions from 'firebase-functions';
import UtilModuler from '@utilModuler'

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Firebase!");
});




从 @utilModuler导入UtilModuler;
^^^^^^^^^

import UtilModuler from '@utilModuler'; ^^^^^^^^^

SyntaxError:Module._compile处的意外标识符
(内部/模块/cjs/loader.js :721:23)

SyntaxError: Unexpected identifier at Module._compile (internal/modules/cjs/loader.js:721:23)

注意事项

Caveats

我正在使用通过导入/导出编写的第三方库(@utilModuler)。可能的解决方法:

I'm using third party libraries(@utilModuler) which were written via import/exports. Possible workarounds:


  1. 叉库并使用汇总生成cjs文件。

  2. esm 的作用很吸引人,但会引起不必要的内存消耗

  1. Fork library and generate cjs file with rollup.
  2. esm works like a charm but it cause unnesecary memory consumptions

问题:有没有一种方法可以在Google云功能中使用混合导入cjs和esm?(我上面所述的选项除外)

Question: is there are a way how to use hybrid import cjs and esm in google cloud function?(except options which I described above)

很好用在部署功能中,例如 --experimental-modules

Would be nice to use in deploy function something like --experimental-modules

"devDependencies": {
  "@babel/core": "^7.2.0",
  "@babel/preset-env": "^7.2.0",
  "@babel/register": "^7.0.0"
}

.babelrc

{
  "presets": ["@babel/preset-env"]
}

入口点node.js应用

entry point node.js app

require("@babel/register")({})

// Import the rest of our application.
module.exports = require('./index.js')