Skip to content

Commit

Permalink
fix: Ensure revert() does revert correctly
Browse files Browse the repository at this point in the history
Fixes #90
Restores compatibility with esm library
  • Loading branch information
danez committed Jan 24, 2022
1 parent 7083385 commit 4e1b015
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ export function addHook(hook, opts = {}) {
// if the current loader for the extension is our loader then unregister it and set the oldLoader again
// if not we can not do anything as we cannot remove a loader from within the loader-chain
if (Module._extensions[ext] === loaders[ext]) {
Module._extensions[ext] = oldLoaders[ext];
if (!oldLoaders[ext]) {
delete Module._extensions[ext];
} else {
Module._extensions[ext] = oldLoaders[ext];
}
}
});
};
Expand Down
2 changes: 2 additions & 0 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ test('reverts to previous loader', (t) => {
});

test('reverts to nothing if no previous loader', (t) => {
const oldKeys = Object.keys(require.extensions);
t.is(require.extensions['.foo2js'], undefined);
const revert = t.context.addHook((code) => code.replace('@@a', '<a>'), {
exts: ['.foo2js'],
Expand All @@ -110,4 +111,5 @@ test('reverts to nothing if no previous loader', (t) => {
revert();

t.is(require.extensions['.foo2js'], undefined);
t.deepEqual(Object.keys(require.extensions), oldKeys);
});

0 comments on commit 4e1b015

Please sign in to comment.