【问题标题】:How to prepare my code for Azure Functions如何为 Azure Functions 准备代码
【发布时间】: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


【解决方案1】:

基本上,您不需要将代码转换为 ES5,因为在 Azure Functions 中运行的代码是服务器端的 Node.js Javascript,而不是前端 Javascript。并且最新的 Node.js 支持大部分 ES6 (ES2015) 语法和特性。

网站node.green 提供了一个很好的概述 不同版本的 Node.js 中支持的 ECMAScript 功能,基于 kangax 的兼容表。

所以请考虑将 Node/NPM 的版本升级到更高版本。为此,请参见下文。

  1. 在浏览器中转到Azure portal
  2. 导航到您的函数应用并点击函数应用设置
  3. 点击配置应用设置按钮,然后将WEBSITE_NODE_DEFAULT_VERSION设置为7.2.0

  4. 完成后,在开发控制台中检查。

您可以在 Node WebApp 的 URL https://<yourappname>.scm.azurewebsites.net/api/diagnostics/runtime 看到 Azure 支持的所有节点版本。

希望对你有帮助。

【讨论】:

  • 太好了,文档说“节点版本当前锁定在 6.5.0。”并且不认为我可以更改此参数。但我仍然需要代码准备:1. 用于条流注释 2. 修复我的导入路径:我使用类似“import { convertor} from 'Convertors/ConvertorA';”之类的东西使用 webpack 选项 "resolve: { extensions: ['', '.js', '.jsx'], root: [ path.resolve('./src'), path.resolve('./node_modules')] } "
  • 也许你可以告诉我如何在不使用 webpack 处理我的文件的情况下解决这个问题?我已经为此找到了正确的配置,但是,也许在不做任何更改的情况下部署我的代码会更好。
  • 将路径更改为相对路径,仍然出现相同的错误: import { convertor } from '../Convertors/ConvertorA'; ^^^^^^ SyntaxError: Unexpected token import 也许你能解释一下它有什么问题?
  • 不幸的是,WEBSITE_DEFAULT_NODE_VERSION 未被 azure 函数使用,因为它依赖 edgejs 运行节点脚本,该脚本使用 6.5.0 版本。
  • Matt Mason 是对的,此信息具有误导性。您可以设置版本,但它对 Azure 函数使用的节点版本没有影响
【解决方案2】:

对于大多数网络应用,Aaron 是正确的,WEBSITE_NODE_DEFAULT_VERSION 将设置您的应用将运行的节点版本。

不幸的是,azure 函数没有以相同的方式运行 node,并且由于对 edgejs 的依赖而被锁定在 6.5.0。

其中一个 github 问题有一个很好的 comment 说明他们如何为 azure 函数启用捆绑。在您的具体情况下,我认为您需要在包本身中包含您的函数的 require,而不是简单地导出函数:

global.deps = { calc: require('./calc') };

然后,在你的函数中使用捆绑包:

// require your bundle
require('<nameofyourbundle>');

// dependencies are stored in the global object under 'deps'
var calc = global.deps.calc;

// your exported function
module.exports = (context, req) => {
  context.done(null, calc(2));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2011-06-21
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多