0

我正在为 AWS Lambda/APIGateway 构建一个简单的 Hello World 函数,并希望合并 ESLint 和 Typescript 功能。通常,当我使用 Typescript 和 ESlint 时,我需要eslint-plugin-import包并且需要在我的.eslintrc文件中指定扩展名,如下所示:

  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  }

Unable to resolve path to module 'aws-lambda'但是,有了这个 monorepo,即使在设置之后,我仍然会收到错误消息。这是我的回购设置:

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,
        }),
      ]
    : [],
};
4

1 回答 1

0

尝试使用eslint-import-resolver-typescript而不是node解析器。它旨在让 eslint-plugin-import 理解 typescript

https://www.npmjs.com/package/eslint-import-resolver-typescript

于 2020-07-30T23:48:33.257 回答