终于有这个工作了。巴贝尔7+Jest 23.6.0+Webpack 4+AWS Lambda。今天也有一个AWS Lambda中断,所以我甚至不确定上面的内容是否有区别(太累了,无法检查!)但这行得通。此处回购:
https://github.com/buildbreakdo/lambda-starter
巴贝尔
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "8.10"
}
}
]
]
}
包.json
{
"name": "lambda-starter",
"version": "1.0.0",
"description": "Minimalist AWS API Gateway and AWS Lambda starter kit",
"main": "build/index.js",
"scripts": {
"build": "NODE_ENV=production webpack --display-error-details --display-modules",
"watch": "webpack --watch",
"test": "jest --config ./jest.config.js",
"test:watch": "jest --watch --config ./jest.config.js",
"start": "sam local start-api --port 5000",
"dist": "rm -f dist.zip && zip -jq dist.zip build/index.js",
"update:dev": "aws lambda update-function-code --function-name DevExample --zip-file fileb://dist.zip --publish",
"update:prod": "aws lambda update-function-code --function-name ProdExample --zip-file fileb://dist.zip --publish",
"deploy:dev": "npm run build && npm run test && npm run dist && npm run update:dev",
"deploy:prod": "npm run build && CI=true npm run test && npm run dist && npm run update:prod"
},
"repository": {
"type": "git",
"url": "git+https://github.com/buildbreakdo/lambda-starter.git"
},
"keywords": [
"starter",
"starter-kit",
"aws-api-gateway",
"aws-lambda"
],
"author": "Your Name Here",
"bugs": {
"url": "https://github.com/buildbreakdo/lambda-starter/issues"
},
"homepage": "https://github.com/buildbreakdo/lambda-starter#readme",
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"babel-loader": "^7.1.4",
"jest": "^23.6.0",
"jest-cli": "^23.6.0",
"webpack": "^4.8.1",
"webpack-cli": "^2.0.11"
},
"dependencies": {
"node-fetch": "^2.3.0"
}
}
const path = require('path');
const webpack = require('webpack');
module.exports = {
target: 'node',
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
entry: [ './src/index.js' ],
output: {
path: path.resolve(__dirname, 'build'),
filename: 'index.js',
library: 'index',
libraryTarget: 'commonjs2'
},
plugins: [
new webpack.IgnorePlugin(/^pg-native$/),
new webpack.DefinePlugin({
'process.env.BROWSER': false,
__DEV__: process.env.NODE_ENV !== 'production',
}),
],
module: {
rules: [
{
test: /\.(mjs|js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
],
}
};