Tìm kiếm trang web

Cảnh báo Webpack từ tệp định nghĩa typescript


Đang gặp khó khăn với vấn đề này và dường như không thể tìm ra nguyên nhân gốc rễ. Tôi liên tục nhận được lỗi này từ các bản build webpack. Có gì sai và làm thế nào để tôi khắc phục những cảnh báo này? Tại sao nó lại tìm nạp tệp định nghĩa TypeScript?

Biên dịch chỉ với trình biên dịch TypeScript thì không xuất ra bất kỳ cảnh báo nào. Tôi đoán chỉ có webpack hoặc ts-loader thôi.

WARNING in ./node_modules/aws-lambda-router/lib/EventProcessor.d.ts 1:7
Module parse failed: Unexpected token (1:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export interface EventProcessor<TConfig = any, TEvent = any, TContext = any, TOutput = any> {
|     process: ProcessMethod<TConfig, TEvent, TContext, TOutput>;
| }
 @ ./node_modules/aws-lambda-router/lib sync ^\.\/.*$ ./EventProcessor.d.ts
 @ ./node_modules/aws-lambda-router/index.js
 @ ./src/lambdas/es_manager.ts

WARNING in ./node_modules/aws-lambda-router/lib/s3.d.ts 3:7
Module parse failed: Unexpected token (3:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import { Context, S3Event as awsS3Event, S3EventRecord } from 'aws-lambda';
| import { ProcessMethod } from './EventProcessor';
> export declare type S3Event = awsS3Event;
| export interface S3Route {
|     bucketName?: string | RegExp;
 @ ./node_modules/aws-lambda-router/lib sync ^\.\/.*$ ./s3.d.ts
 @ ./node_modules/aws-lambda-router/index.js
 @ ./src/lambdas/es_manager.ts

WARNING in ./node_modules/aws-lambda-router/lib/sns.d.ts 3:7
Module parse failed: Unexpected token (3:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import { Context, SNSEvent, SNSMessage, SNSEventRecord } from 'aws-lambda';
| import { ProcessMethod } from './EventProcessor';
> export declare type SnsEvent = SNSEvent;
| export interface SnsRoute {
|     subject: RegExp;
 @ ./node_modules/aws-lambda-router/lib sync ^\.\/.*$ ./sns.d.ts
 @ ./node_modules/aws-lambda-router/index.js
 @ ./src/lambdas/es_manager.ts

WARNING in ./node_modules/aws-lambda-router/lib/sqs.d.ts 3:7
Module parse failed: Unexpected token (3:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import { Context, SQSEvent, SQSRecord } from 'aws-lambda';
| import { ProcessMethod } from './EventProcessor';
> export declare type SqsEvent = SQSEvent;
| export interface SqsRoute {
|     source: string | RegExp;
 @ ./node_modules/aws-lambda-router/lib sync ^\.\/.*$ ./sqs.d.ts
 @ ./node_modules/aws-lambda-router/index.js
 @ ./src/lambdas/es_manager.ts

WARNING in ./node_modules/aws-lambda-router/lib/proxyIntegration.d.ts 3:8
Module parse failed: Unexpected token (3:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import { APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
| import { ProcessMethod } from './EventProcessor';
> declare type ProxyIntegrationParams = {
|     paths?: {
|         [paramId: string]: string;
 @ ./node_modules/aws-lambda-router/lib sync ^\.\/.*$ ./proxyIntegration.d.ts
 @ ./node_modules/aws-lambda-router/index.js
 @ ./src/lambdas/es_manager.ts
  adding: main.js (deflated 75%)

Cấu hình webpack như thế này

const path = require('path');
const fs = require('fs');

module.exports = {
    entry: path.join(__dirname, "src/lambdas", "es_manager.ts"),
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          use: {
            loader: 'ts-loader',
          },  
          exclude: /node_modules/
        }
      ]
    },
    resolve: {
      extensions: ['.tsx', '.ts', '.js' ]
    },
    output: {
      libraryTarget: 'commonjs',
      filename: '[name].js',
      path: path.resolve(__dirname, 'dist')
    },
    target: 'node',
    mode: 'production'
};

tsconfig.json trông như thế này

{
  "compilerOptions": {
    "target": "es2017",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "noEmitOnError": true,
    "allowJs": true,                       /* Allow javascript files to be compiled. */
    "sourceMap": false,                     /* Generates corresponding '.map' file. */
    "outDir": "./dist",                        /* Redirect output structure to the directory. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    "rootDirs": ["./src"],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    "typeRoots": ["node_modules/@types"],                       /* List of folders to include type definitions from. */
    "types": ["node"],                           /* Type declaration files to be included in compilation. */
    "esModuleInterop": true,                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
  },
  "exclude": [
    "node_modules",
    "**/*.test.ts"
  ],
}

Câu trả lời #1

Hãy xem câu hỏi này: Typescript+webpack: typescript không tạo ra output cho index.d.ts - cụ thể hơn câu trả lời liên quan đến ignore-loader: https://stackoverflow.com/a/50100267/23697

Cách tiếp cận ở đó là bỏ qua các tệp .d.ts cho ts-loader bằng gói npm ignore-loader và điều đó dường như hoạt động trong thời điểm hiện tại.

Tôi nghĩ có điều gì đó không chuẩn về các tệp khai báo đi kèm với aws-lambda-router khiến những lỗi này xuất hiện.