Typescript 如何从无服务器aws lambdas更改react应用程序中的redux状态
我想做的事 通过aws lambda,收听dynamoDB流并根据dynamoDB更改更改我的应用程序内状态 我是如何做到这一点的 创建了一个lambda,它从流中为我提供一个事件。在同一个lambda中,我尝试导入我的redux存储和操作,以便能够从lambda中分派,从而更改我的应用程序内状态 我的问题 当我尝试以下操作时,Webpack无法找到外部导入(store.tsx、actions.tsx)并为lambda绑定它们:Typescript 如何从无服务器aws lambdas更改react应用程序中的redux状态,typescript,webpack,aws-lambda,react-redux,Typescript,Webpack,Aws Lambda,React Redux,我想做的事 通过aws lambda,收听dynamoDB流并根据dynamoDB更改更改我的应用程序内状态 我是如何做到这一点的 创建了一个lambda,它从流中为我提供一个事件。在同一个lambda中,我尝试导入我的redux存储和操作,以便能够从lambda中分派,从而更改我的应用程序内状态 我的问题 当我尝试以下操作时,Webpack无法找到外部导入(store.tsx、actions.tsx)并为lambda绑定它们: sls package 在我的无服务器文件夹中。获取: ERRO
sls package
在我的无服务器文件夹中。获取:
ERROR in ../reducers/global_reducer.tsx
Module not found: Error: Can't resolve 'actions/action_types' in 'C:\[PATH TO PROJECT]\src\reducers'
@ ../reducers/global_reducer.tsx 1:0-47 20:13-24 22:13-24 24:13-24 26:13-24 28:13-24
@ ../reducers/combined_reducer.tsx
@ ../store.tsx
@ ./handler.ts
我的设置
我基于使用Typescript创建应用程序。在src中,我有我所有的文件和文件夹,包括我试图导入的文件和文件夹。但是,我的无服务器文件夹位于此src文件夹内,如下所示:
serverelss文件夹(SLS)是通过以下方式创建的:
serverless create --template aws-nodejs-typescript
从my handler.ts导入的内容如下所示:
相关webpack.config.js(来自SLS文件夹)。所有评论都是我尝试过的:
const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
module.exports = {
context: __dirname,
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
//entry: path.join(__dirname, "../index.tsx"),
devtool: slsw.lib.webpack.isLocal ? 'cheap-module-eval-source-map' : 'source-map',
resolve: {
extensions: ['.mjs', '.json', '.ts', '.tsx'],
symlinks: false,
cacheWithContext: false,
},
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
target: 'node',
externals: ['aws-sdk'],
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{
test: /\.(tsx?)$/,
loader: 'ts-loader',
// include: [
// path.resolve(__dirname, '../../src'),
// ],
exclude: [
[
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, '.serverless'),
path.resolve(__dirname, '.webpack'),
],
],
options: {
transpileOnly: true,
experimentalWatchApi: true,
},
},
],
},
plugins: [
// new ForkTsCheckerWebpackPlugin({
// eslint: true,
// eslintOptions: {
// cache: true
// }
// })
],
};
serverless.yml:
...
custom:
webpack:
webpackConfig: ./webpack.config.js
includeModules: false
plugins:
- serverless-webpack
#package:
# include:
# - "../../src/**"
...
tsconfig.json(在SLS文件夹中):
package.json(在SLS文件夹中):
如何将应用程序中的逻辑导入并使用到无服务器环境中,并在lambdas for AWS中使用
附言:
请原谅我写了这么长的帖子,尽量详细
{
"compilerOptions": {
"lib": ["es2017"],
"removeComments": true,
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"target": "es2017",
"outDir": "lib",
"skipLibCheck": true,
"jsx": "preserve"
},
"include": ["./**/*.ts"],
"exclude": [
"node_modules/**/*",
".serverless/**/*",
".webpack/**/*",
"_warmup/**/*",
".vscode/**/*"
]
}
{
"name": "sls",
"version": "1.0.0",
"description": "Serverless webpack example using Typescript",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"source-map-support": "^0.5.10"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.17",
"@types/node": "^10.12.18",
"fork-ts-checker-webpack-plugin": "^3.0.1",
"serverless-webpack": "^5.2.0",
"ts-loader": "^5.3.3",
"typescript": "^3.2.4",
"webpack": "^4.29.0",
"aws-sdk": "^2.737.0",
"webpack-node-externals": "^1.7.2"
},
"author": "The serverless webpack authors (https://github.com/elastic-coders/serverless-webpack)",
"license": "MIT"
}