typescript - 来自 typescript 定义文件的 Webpack 警告

标签 typescript webpack-4

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

仅使用 typescript 编译器编译不会输出任何警告。我猜它只是 webpack 或 ts-loader。

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: typescript emmited no output for index.d.ts - 更具体地说是与忽略加载程序相关的答案:https://stackoverflow.com/a/50100267/23697

这里的方法是使用 ignore-loader npm 包忽略 ts-loader 的 .d.ts 文件,这似乎暂时有效。

我认为 aws-lambda-router 附带的声明文件存在一些不标准的地方会触发这些错误。

关于typescript - 来自 typescript 定义文件的 Webpack 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62604966/

相关文章:

Angular HttpClient post 请求类型

Typescript 模板文字类型循环约束

javascript - 如何复制文件夹中的文件进行构建?

webpack 开发服务器不构建和重新加载 index.html

webpack-4 - 多个带有 browserslist 和 babel preset-env 的 webpack 包

javascript - Typescript 在嵌套 for 循环完成后执行代码

javascript - 为 MockComponent 制作 JSX 语法并使用 typescript 输入

javascript - 导入/导出模块 - Typescript

javascript - 如何使用webpack代理devserver路径重写?

webpack 4 禁用 uglifyjs-webpack-plugin