sindresorhus / clear-module

Clear a module from the cache
MIT License
182 stars 25 forks source link

clear-module does not clear modules deep in the stack trace #21

Open MCArth opened 5 months ago

MCArth commented 5 months ago

Due to v8 stack traces only going so many deep by default, modules deep enough in the dependency tree such that clear has been called >~8 times do not get cleared. This is because resolve/parentModule relies on the v8 stack trace.

It seems to me we don't need to resolve moduleId when calling it recursively from within clear, since children are absolute paths.

I've copied this module into our project and fixed as so:

const clear = (moduleId: string) => {
    clearInternal(moduleId, true);
}

const clearInternal = (moduleId: string, doResolve: boolean) => {
    if (typeof moduleId !== 'string') {
        throw new TypeError(`Expected a \`string\`, got \`${typeof moduleId}\``);
    }

    const filePath = doResolve ? resolve(moduleId) : moduleId;

       ...

        for (const id of children) {
            clearInternal(id, false);
        }