簡體   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