EdwardZZZ / articles

工作点滴记录
2 stars 0 forks source link

require cache #64

Open EdwardZZZ opened 5 years ago

EdwardZZZ commented 5 years ago

function requireNoCache(modulePath, env = process.env.NODE_ENV) {
    env === 'development' && deleteRequireCache(modulePath);

    return require(modulePath);
}

function deleteRequireCache(modulePath) {
    const module = require.cache[modulePath];

    if (!module) return;

    if (module.parent) {
        module.parent.children.splice(module.parent.children.indexOf(module), 1);
    }

    delete require.cache[modulePath];
}