【问题标题】:How to get rid of webpack warnings when including the winston logger using ContextReplacementPlugin?使用 ContextReplacementPlugin 包含 winston 记录器时如何摆脱 webpack 警告?
【发布时间】:2018-10-02 07:49:03
【问题描述】:

在包含winston 的项目上运行 webpack,会产生许多警告(见下文)。由于依赖项 logform 中的延迟加载机制,Webpack 会自动包含非 JavaScript 文件。 你可以看到这里发生了什么——查看exposeFormat函数:https://github.com/winstonjs/logform/blob/master/index.js

Webpack 无法确定要包含哪些文件,因此它包含所有文件,包括 README.md 文件,并尝试使用 Typescript 处理它们。我知道 webpack 的 ContextReplacementPlugin 应该可以帮助我,但我无法让它工作。

我已经尝试将它添加到我在 webpack.config.js 中的插件列表中:

new webpack.ContextReplacementPlugin(/logform/, /(.*)\.js/)

我认为这应该包括文件夹中的所有 .js 文件。这消除了构建时的警告,但在运行时,我从 webpack 收到一个错误,表明尝试要求这些文件之一失败。

我做错了什么?

Webpack 产生的警告

WARNING in ./node_modules/logform/index.d.ts
Module build failed: Error: Typescript emitted no output for //Users/aneil/code/project/node_modules/logform/index.d.ts.
You should not need to recompile .ts files in node_modules.
Please contact the package author to advise them to use --declaration --outDir.
More https://github.com/Microsoft/TypeScript/issues/12358
    at successLoader (/Users/aneil/code/project/node_modules/ts-loader/dist/index.js:39:15)
    at Object.loader (/Users/aneil/code/project/node_modules/ts-loader/dist/index.js:21:12)
 @ ./node_modules/logform sync ^\.\/.*$
 @ ./node_modules/logform/index.js
 @ ./node_modules/winston/lib/winston.js
 @ ./src/logger.ts
 @ ./src/services/pipeline/handlers.ts

WARNING in ./node_modules/logform/CHANGELOG.md
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| # CHANGELOG
|
| ### 1.4.2
 @ ./node_modules/logform sync ^\.\/.*$
 @ ./node_modules/logform/index.js
 @ ./node_modules/winston/lib/winston.js
 @ ./src/logger.ts
 @ ./src/services/pipeline/handlers.ts

WARNING in ./node_modules/logform/README.md
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| # logform
|
| An mutable object-based log format designed for chaining & objectMode streams.
 @ ./node_modules/logform sync ^\.\/.*$
 @ ./node_modules/logform/index.js
 @ ./node_modules/winston/lib/winston.js
 @ ./src/logger.ts
 @ ./src/services/pipeline/handlers.ts

WARNING in ./node_modules/logform/LICENSE
Module parse failed: Unexpected token (1:4)
You may need an appropriate loader to handle this file type.
| MIT License
|
| Copyright (c) 2017 Charlie Robbins & the Contributors.
 @ ./node_modules/logform sync ^\.\/.*$
 @ ./node_modules/logform/index.js
 @ ./node_modules/winston/lib/winston.js
 @ ./src/logger.ts
 @ ./src/services/pipeline/handlers.ts

添加 ContextReplacementPlugin 后出现运行时错误

Serverless: GET /api (λ: GraphQLPlayground)
Serverless: The first request might take a few extra seconds
Serverless: Error while loading GraphQLPlayground
[ 'Error: Cannot find module "./combine".',
  'at webpackContextResolve (webpack:///./node_modules/logform_sync_(.*)\\.js?:39:11)',
  'at webpackContext (webpack:///./node_modules/logform_sync_(.*)\\.js?:32:11)',
  'at Function.get (webpack:///./node_modules/logform/index.js?:27:101)',
  'at eval (webpack:///./src/logger.ts?:34:20)',

【问题讨论】:

    标签: javascript typescript webpack winston


    【解决方案1】:

    我认为解决此问题的最佳方法是直接在 logform 库中解决此问题,因此我提交了此 PR:https://github.com/winstonjs/logform/pull/24

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 1970-01-01
      • 2016-02-04
      • 2013-01-28
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      相关资源
      最近更新 更多