繁体   English   中英

无服务器,具有Azure功能和Webpack

[英]serverless with azure functions and webpack

我想知道是否有人使用具有azure函数的无服务器框架,以及如何处理跨函数共享和捆绑的代码?

我正在将hapi.js应用程序转换为无服务器 + 无服务器天蓝色功能,并且在部署之前尝试捆绑我的代码,以便可以将各种require用于可重用模块。

我发现serverless-webpack并创建了可能在AWS Lambda上运行的捆绑软件,但是由于缺少function.json文件(例如list-function.json ),天蓝色存在问题,因此这些功能在内部完全不可见天蓝色门户,我也不能调用它们。

还找到了有关此问题的文章 ,但它显示了如何使用仅支持Windows平台的azure-functions-cli处理此问题。

最好,JH

来自https://medium.com/a-man-with-no-server/deploying-a-serverless-application-using-webpack-and-babel-to-support-es2015-to-aws-2f61cff8bafb的提示使用serverless-webpack修改了无服务器的Azure功能启动测试项目,这似乎可以满足您的要求。

我在无服务器azure函数项目的根目录中构建了一个src文件夹,作为开发源代码文件夹。 带有2个测试文件:
handler.js

'use strict';
let tool = require("./tool");
/* eslint-disable no-param-reassign */

module.exports.hello = function (context) {
  context.log('JavaScript HTTP trigger function processed a request.');

  context.res = {
    // status: 200, /* Defaults to 200 */
    body: tool.hello(),
  };

  context.done();
};

tool.js

module.exports={
    hello:()=>{
        return "hello world";
    }
}

根目录中的webpack.config.js

var nodeExternals = require('webpack-node-externals')

module.exports = {
   entry: './src/handler.js',
   target: 'node',
   externals: [nodeExternals()],
   output: {
      libraryTarget: 'commonjs',
      path: __dirname,
      filename: 'handler.js', // this should match the first part of function handler in serverless.yml
   },
   module: {
      loaders: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            include: __dirname,
            loaders: ["babel-loader"]
         }
      ]
   }
};

使用哪个配置文件,捆绑文件将位于根目录中的service/handler.js中。

所以我也修改了serverless.yml ,现在它的一部分看起来像:

package:
  include:
    - service/handler.js
  exclude:
    - handler.js

functions:
  hello:
    handler: service/handler.hello
    events:
      - http: true
        x-azure-settings:
          authLevel : anonymous
      - http: true
        x-azure-settings:
          direction: out
          name: res

custom:
  webpackIncludeModules:
    packagePath: './package.json'

这些修改后,使用serverless deploy将文件捆绑在src文件夹中,然后打包并部署到azure功能。

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM