Skip to content

Commit

Permalink
Lazy Import: Avoid mutating require cache for final import
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jun 3, 2020
1 parent a2cd417 commit 6481d03
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/lazy-import/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,18 @@ async function lazyImport( arg, options = {} ) {
throw error;
}

// Delete cache from prior `require` attempt.
delete require.cache[ require.resolve( localModule ) ];
// Note: For those familiar with the internals of module importing, the
// second attempt here at requiring the same package as earlier may stand
// out as being susceptible to conflicting with module caching.
//
// https://nodejs.org/api/modules.html#modules_require_cache
//
// Testing appears to indicate that module results are not cached if it
// fails to resolve, which is what we can assume would have happened in the
// first attempt to require the module. Thus, it's not required to reset any
// values from `require.cache`.
//
// See: https://github.com/WordPress/gutenberg/pull/22684#discussion_r434583858

return require( localModule );
}
Expand Down

0 comments on commit 6481d03

Please sign in to comment.