我正在为AWS Lambda / APIGateway构建一个简单的Hello World函数,并希望合并ESLint和Typescript功能。通常,当我使用Typescript和ESlint时,我需要eslint-plugin-import
软件包,并需要在我的.eslintrc
文件中指定扩展名,如下所示:
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
}
但是,使用此monorepo,即使设置了这些错误,我仍然遇到Unable to resolve path to module 'aws-lambda'
错误。这是我的回购设置:
apis/
users/
dist/ <-- My complied JS code
node_modules/
src/
functions/ <-- My Typescript Lambda functions
test/ <-- My Jest tests
.eslintrc <-- My ESLint config
package.json
template.yaml <-- My SAM template
tsconfig.json <-- My Typescript config
webpack.config.js <-- My WebPack config
这是我的.eslintrc
配置:
{
"root": true,
"env": {
"es2020": true,
"mongo": true,
"node": true,
"jest": true
},
"parser": "@typescript-eslint/parser",
"extends": [
"airbnb",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended" // This will display prettier errors as ESLint errors. Make sure this is always the last configuration.
],
"ignorePatterns": ["dist/", "coverage/", "test/"],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true
}
},
"rules": {
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
],
"max-len": [
"error",
{
"code": 100,
"ignoreComments": true,
"ignoreStrings": true,
"ignoreTemplateLiterals": true
}
],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"vars": "all",
"args": "none"
}
]
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
}
}
这是我的tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "esnext",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["**/*.test.*"]
}
最后是我的webpack配置:
/* eslint-disable */
const path = require('path');
const { readFileSync } = require('fs');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const { yamlParse } = require('yaml-cfn');
/* eslint-enable */
const conf = {
prodMode: process.env.NODE_ENV === 'production',
templatePath: './template.yaml',
};
const cfn = yamlParse(readFileSync(conf.templatePath));
const entries = Object.values(cfn.Resources)
// Find nodejs functions
.filter((resource) => resource.Type === 'AWS::Serverless::Function')
.filter(
(resource) =>
(resource.Properties.Runtime && resource.Properties.Runtime.startsWith('nodejs')) ||
(!resource.Properties.Runtime && cfn.Globals.Function.Runtime)
)
.map((resource) => ({
// Isolate handler src filename
handlerFile: resource.Properties.Handler.split('.')[0],
// Build handler dst path
CodeUriDir: resource.Properties.CodeUri.split('/').splice(1).join('/'),
}))
.reduce(
(resources, resource) =>
Object.assign(
resources,
// Generate {outputPath: inputPath} object
{ [`${resource.CodeUriDir}/${resource.handlerFile}`]: `./src/${resource.CodeUriDir}.ts` }
),
{}
);
module.exports = {
entry: entries,
target: 'node',
mode: conf.prodMode ? 'production' : 'development',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
libraryTarget: 'commonjs2',
},
devtool: 'source-map',
plugins: conf.prodMode
? [
new UglifyJsPlugin({
parallel: true,
extractComments: true,
sourceMap: true,
}),
]
: [],
};
答案 0 :(得分:0)
尝试使用eslint-import-resolver-typescript
而不是node
解析器。
它旨在使eslint-plugin-import了解打字稿
https://www.npmjs.com/package/eslint-import-resolver-typescript