简体   繁体   中英

How to get rid of webpack warnings when including the winston logger using ContextReplacementPlugin?

Running webpack on a project which includes winston , produces a number of warnings (see below). Webpack is automatically including non-javascript files due to a lazy-loading mechanism in a dependency, logform. You can see what's happening here - see the exposeFormat function: https://github.com/winstonjs/logform/blob/master/index.js

Webpack can't figure out which files to include, so it includes them all, including the README.md files, and attempts to process them with Typescript. I know that webpack's ContextReplacementPlugin should help me, but I can't get it to work.

I've tried adding this to my plugins list in webpack.config.js:

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

I thought this should include all .js files in the folder. This gets rid of the warnings at build time, but at runtime, I get an error from webpack indicating that an attempt to require one of those files fails.

What am I doing wrong?

Warnings produced by 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

Runtime errors after I add the 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)',

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM