来自https://medium.com/a-man-with-no-server/deploying-a-serverless-application-using-webpack-and-babel-to-support-es2015-to-aws-2f61cff8bafb的Giting 提示,我修改了一个 serverless azure functions 启动测试项目serverless-webpack
,似乎满足您的要求。
我src
在 serverless azure functions 项目的根目录下建了一个文件夹,作为开发源代码文件夹。有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
夹中的文件打包,然后打包并部署到天蓝色功能。
希望能帮助到你。