forked from DevExpress/testcafe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28b1ac9
commit c8db6ab
Showing
3 changed files
with
69 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,24 @@ | ||
import resolveFrom from 'resolve-from'; | ||
import momentDurationFormatSetup from 'moment-duration-format-commonjs'; | ||
|
||
|
||
const MOMENT_MODULE_NAME = 'moment'; | ||
const DURATION_FORMAT_MODULE_NAME = 'moment-duration-format'; | ||
const MOMENT_MODULE_NAME = 'moment'; | ||
|
||
function restoreInitialCacheState (module, path) { | ||
if (module) | ||
require.cache[path] = module; | ||
else | ||
delete require.cache[path]; | ||
} | ||
|
||
function getSideMomentModulePath (sidePath) { | ||
try { | ||
return resolveFrom(sidePath, MOMENT_MODULE_NAME); | ||
} | ||
catch (err) { | ||
return ''; | ||
} | ||
} | ||
|
||
function getModulesPaths () { | ||
const durationFormatModulePath = require.resolve(DURATION_FORMAT_MODULE_NAME); | ||
|
||
return { | ||
durationFormatModulePath, | ||
|
||
mainMomentModulePath: require.resolve(MOMENT_MODULE_NAME), | ||
sideMomentModulePath: getSideMomentModulePath(durationFormatModulePath) | ||
}; | ||
} | ||
|
||
function getCachedAndCleanModules (modulePath) { | ||
const cachedModule = require.cache[modulePath]; | ||
|
||
delete require.cache[modulePath]; | ||
function loadMomentModule () { | ||
const momentModulePath = require.resolve(MOMENT_MODULE_NAME); | ||
const savedMomentModule = require.cache[momentModulePath]; | ||
|
||
require(modulePath); | ||
delete require.cache[savedMomentModule]; | ||
|
||
return { cachedModule, cleanModule: require.cache[modulePath] }; | ||
} | ||
|
||
function getMomentModules ({ mainMomentModulePath, sideMomentModulePath }) { | ||
const { cachedModule, cleanModule } = getCachedAndCleanModules(mainMomentModulePath); | ||
|
||
return { | ||
sideModule: require.cache[sideMomentModulePath], | ||
mainModule: cleanModule, | ||
cachedModule | ||
}; | ||
} | ||
|
||
function getMomentModuleWithDurationFormatPatch () { | ||
const modulesPaths = getModulesPaths(); | ||
const momentModules = getMomentModules(modulesPaths); | ||
const moment = require(momentModulePath); | ||
|
||
const { sideMomentModulePath, mainMomentModulePath, durationFormatModulePath } = modulesPaths; | ||
momentDurationFormatSetup(moment); | ||
|
||
if (sideMomentModulePath && sideMomentModulePath !== mainMomentModulePath) { | ||
require.cache[sideMomentModulePath] = momentModules.mainModule; | ||
|
||
require(durationFormatModulePath); | ||
|
||
restoreInitialCacheState(momentModules.sideModule, sideMomentModulePath); | ||
} | ||
else { | ||
const durationFormatSetup = require(durationFormatModulePath); | ||
|
||
if (!sideMomentModulePath) | ||
durationFormatSetup(momentModules.mainModule.exports); | ||
} | ||
|
||
restoreInitialCacheState(momentModules.cachedModule, mainMomentModulePath); | ||
if (savedMomentModule) | ||
require.cache[momentModulePath] = savedMomentModule; | ||
else | ||
delete require.cache[momentModulePath]; | ||
|
||
return momentModules.mainModule.exports; | ||
return moment; | ||
} | ||
|
||
export default getMomentModuleWithDurationFormatPatch(); | ||
export default loadMomentModule(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters