-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: force esm to reload the new module instead of the cached one #54
base: master
Are you sure you want to change the base?
Conversation
@antonk52 could you take a look, please. |
Hey, thanks for the PR. I am hesitant to merge this in the current state. Since I last checked it is a known limitation of ESM in node that you cannot clear the module cache nodejs issue. Not sure if something changed in this regard. Currently we pass the module
|
@antonk52 I totally forgot that bundlers preprocessors could also add query params to the id to force invalidation for browser builds. Good catch.
|
Apparently
|
There is a built in Url and SearchParams that can utilised for this reason.
I don't want to add discrepancy between async and sync loaders. For this reason adding a cache invalidation PR should target both loaders. As there is currently no recommended way to do this in nodejs can this be a user provided loader instead? We can add this recommendation to the package documentation |
@antonk52 I agree with that being a custom loader. Where would be the best place to put an example? Right after the yamls loader or in the section where you talk about ESM? If I provide a custom loader for dynamic imports, will the "id" of the module be available in the loader's scope? Or do we need to change something to make it available? |
You can add a section about reloading modules and provide an example how to defined a loader that appends import {lilconfig} from 'lilconfig'
function jsLoader(filepath)
return import(`${filepath}?_lilconfig_ts=${Date.now()}`)
}
const options = {
loaders: {
'.js': jsLoader,
'.mjs': jsLoader,
'.cjs': jsLoader,
}
}
lilconfig('myapp', options).search()
lilconfig('myapp', options).load(filepath)
Overriding loaders is already available as an api. In your project you can provide a custom function for any extension and lilconfig will use it to load the content, this function takes filepath and filecontent and returns the module. |
Without this change, config files stored as esm (the ones that use the dynamicImport loader) won't be updated. Esm returns the previous loaded state of the module.
How to visualize the bug
.mjs
.lilconfig
object. Use a singleton approach.dynamicImport
to better visualize it.