sainsburys-tech / next-logger

JSON logging patcher for Next.js
MIT License
144 stars 14 forks source link

Replace cosmiconfig with lilconfig to Work with Webpack #34

Closed CHC383 closed 2 months ago

CHC383 commented 2 months ago

Partially fix https://github.com/sainsburys-tech/next-logger/issues/29

Description

Webpack will try to bundle modules during compile time and replace the original require, so using require directly in the Webpack context to load configs could result in MODULE_NOT_FOUND error. next-logger uses the sync API of cosmiconfig to load its config, which internally uses import-fresh to handle the config loading, and import-fresh underneath uses the plain require. This has been causing the issues mentioned in https://github.com/sainsburys-tech/next-logger/issues/13#issuecomment-2113625702 and https://github.com/sainsburys-tech/next-logger/issues/29

While trying to fix the issue (PR: https://github.com/sindresorhus/import-fresh/pull/27), it seems that import-fresh is not actively maintained. lilconfig is a zero-dependency alternative to cosmiconfig with the same API and more lightweight, and just released a new version 3.1.2 with the same fix.

I am proposing to use lilconfig instead in this PR to fix the config loading issue and hopefully avoid some issues caused by legacy components in the future.

Testing

CHC383 commented 2 months ago

Renamed the branch and rebased on top of latest main, so replacing https://github.com/sainsburys-tech/next-logger/pull/31 with this one

atkinchris commented 2 months ago

Great write up! Thank you for contributing this fix @CHC383.