简体   繁体   English

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

[英]serverless with azure functions and webpack

I'm wondering if there is anyone using serverless framework with azure functions and how you handle sharing code across functions & bundling? 我想知道是否有人使用具有azure函数的无服务器框架,以及如何处理跨函数共享和捆绑的代码?

I'm converting hapi.js app to serverless + serverless-azure-functions and I'm trying to bundle my code before deploying so I can use various require for reusable modules. 我正在将hapi.js应用程序转换为无服务器 + 无服务器天蓝色功能,并且在部署之前尝试捆绑我的代码,以便可以将各种require用于可重用模块。

I found serverless-webpack and It create bundles that probably works on AWS Lambda but there is a problem on azure because of lack of function.json files (ex. list-function.json ), so the functions aren't visible at all inside azure-portal nor I can't invoke them. 我发现serverless-webpack并创建了可能在AWS Lambda上运行的捆绑软件,但是由于缺少function.json文件(例如list-function.json ),天蓝色存在问题,因此这些功能在内部完全不可见天蓝色门户,我也不能调用它们。

Also found article about this problem but It shows how to handle this with azure-functions-cli which only support Windows platform. 还找到了有关此问题的文章 ,但它显示了如何使用仅支持Windows平台的azure-functions-cli处理此问题。

Best, JH 最好,JH

Giting hints from https://medium.com/a-man-with-no-server/deploying-a-serverless-application-using-webpack-and-babel-to-support-es2015-to-aws-2f61cff8bafb , I modified a serverless azure functions start-up test project with serverless-webpack , which seems to be satified with your requirement. 来自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功能启动测试项目,这似乎可以满足您的要求。

I built a src folder in the root directory of serverless azure functions project, as the develop source code folder. 我在无服务器azure函数项目的根目录中构建了一个src文件夹,作为开发源代码文件夹。 With 2 test files: 带有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 in root directory: 根目录中的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"]
         }
      ]
   }
};

With which configuration file, the out bundled file will be located in service/handler.js in root directory. 使用哪个配置文件,捆绑文件将位于根目录中的service/handler.js中。

So I modified serverless.yml as well, now it partly looks like: 所以我也修改了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'

After these modified, use serverless deploy will bundle the files in src folder then package and deploy to azure function. 这些修改后,使用serverless deploy将文件捆绑在src文件夹中,然后打包并部署到azure功能。

Hope it helps. 希望能帮助到你。

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

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