Saturday, 2 March 2019

[joke] 0F's export; ES6 Import/Require Compatibility saving frustration

After a long day of coding; I kinda just gave up and decided to use this export pattern on my constants. I call it '0 eff exports' inspired by the Instagram account.​import path from 'path'; const cachePath = path.resolve(process.env.PWD, '_cache'); const S3_PATHS = { BANNERS: '/banners', USER_AVATARS: '/user-avatars' }; const LOCAL_CACHES = { S3_IMAGES: `${cachePath}/S3-images`, BANNERS: `${cachePath}/S3-images${S3_PATHS.BANNERS}`, USER_AVATARS: `${cachePath}/S3-images${S3_PATHS.USER_AVATARS}` }; // 0F's export module.exports = LOCAL_CACHES; module.exports.default = LOCAL_CACHES; ​Benefit is that you can collect these in another central constants export cleverly found in /src/constants/index.js. Which then requires you to patch those later import-exports​import LOCAL_CACHES from './localCaches'; import S3_PATHS from './s3Paths'; import USER_LEVELS from './userLevels'; /* eslint-disable no-return-assign */ const cleanExport = constant => Object.keys(constant).reduce((acc, key) => (key !== 'default' ? (() => acc)(acc[key] = constant[key]) : acc), {}); /* eslint-enable no-return-assign */ const constants = { S3_PATHS: cleanExport(S3_PATHS), LOCAL_CACHES: cleanExport(LOCAL_CACHES) ...USER_LEVELS }; export default constants; Then this way you can pull off named exports in some places and default exports in others.Got major code smell but its cut down on my frustrations with constants and import/exports.[EDIT]Fix formatting

Submitted March 03, 2019 at 01:56AM by bigorangemachine

No comments:

Post a Comment