得到https://medium.com/a-man-with-no-server/deploying-a-serverless-application-using-webpack-and-babel-to-support-es2015-to-aws-2f61cff8bafb的提示,我用serverless-webpack修改了一个serverless azure functions启动测试项目,似乎满足了你的要求。
我在 serverless azure functions 项目的根目录下建了一个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函数中。
希望对你有帮助。