来自打字稿定义文件的 Webpack 警告



为此苦苦挣扎,似乎找不到根本原因。我不断从 webpack 构建中收到此错误。出了什么问题,我该如何解决这些警告?为什么它看起来要拿起打字稿定义文件?

仅使用打字稿编译器进行编译不会输出任何警告。我猜这只是网络包或 ts 加载器。

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%)

webpack 配置是这样的

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 是这样的

{
"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"
],
}

看看这个问题: Typescript+webpack:打字稿没有为 index.d.ts 发出任何输出 - 更具体地说,与忽略加载器相关的答案:https://stackoverflow.com/a/50100267/23697

那里的方法是使用忽略加载器 npm 包的 ts-loader 的 .d.ts 文件,这目前似乎有效。

我认为 aws-lambda-router 附带的声明文件有一些非标准的东西会触发这些错误。

最新更新