【发布时间】:2017-01-24 07:07:58
【问题描述】:
我有一些关于 ES6 的项目。例如一个文件:
export default function (a, b)
{
return a+b;
}
我使用 webpack 和 babel 将其转换为一个带有 ES2015 代码的文件。得到这样的东西:
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(1);
/***/ },
/* 1 */
/***/ function(module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (a, b) {
// return aCalc(a) + bCalc(b);
return a + b;
};
/***/ }
/******/ ]);
现在我想在 Azure Function 中使用我的函数。为此,我创建了文件 calc.js 并用 webpack 输出填充它,并尝试从我的主函数中调用它:
var calc = require('./calc');
module.exports = function (context, req) {
var result = calc(2);
context.done(null, result);
};
得到错误
执行函数时出现异常:Functions.HttpTriggerJS1。 mscorlib:TypeError:calc 不是函数 在 module.exports (D:\home\site\wwwroot\HttpTriggerJS1\index.js:11:30) 在 D:\Program Files (x86)\SiteExtensions\Functions\1.0.10690\bin\Content\Script\functions.js:83:24。
现在是 Questin:我应该如何准备我的代码以在 Azure Function 中调用它?
【问题讨论】:
-
你为什么使用 webpack ? azure 函数执行服务器端 javascript。
-
您可以通过将服务器端代码合并到最小化的单个文件中来缩短冷启动时间。大多数服务器端开发人员不会对此考虑太多,因为他们的服务器寿命很长,但在无服务器世界中,文件的加载时间会影响不太活跃的功能的冷启动,从毫秒到秒,在某些情况下值得考虑。
-
@Thomas,我无法在 Azure Function 上运行 ES6 代码,所以我需要转换它,如果结果会更小 - 那也很好。
-
@Chris Anderson-MSFT 也许你知道任何现成的代码转换和压缩解决方案?
-
我们有人使用 browserify 并将所有内容加载到全局对象上,这不是我推荐的最佳实践,但它确实有效。 Webpack 是我接下来可能会调查的东西,但我现在没有一个好的答案。希望在不久的将来通过提供 Azure Files 的替代方案来解决这个问题。
标签: javascript node.js azure webpack azure-functions