Skip to content

Commit

Permalink
Use moment-duration-format-commonjs
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyBelym committed Sep 6, 2018
1 parent faef1c2 commit 781624a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 71 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"map-reverse": "^1.0.1",
"mkdirp": "^0.5.1",
"moment": "^2.10.3",
"moment-duration-format": "^2.2.2",
"moment-duration-format-commonjs": "^1.0.0",
"mustache": "^2.1.2",
"nanoid": "^1.0.1",
"node-version": "^1.0.0",
Expand Down
84 changes: 14 additions & 70 deletions src/utils/moment-loader.js
Original file line number Diff line number Diff line change
@@ -1,80 +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];

const cleanModuleExports = require(modulePath);
const cleanModule = require.cache[modulePath] || { exports: cleanModuleExports };
delete require.cache[savedMomentModule];

return { cachedModule, cleanModule };
}

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();

0 comments on commit 781624a

Please sign in to comment.