added dynamic import function

This commit is contained in:
bill bittner 2018-03-20 12:23:39 -07:00
parent 57ec269532
commit 4b1f0d46fd
3 changed files with 39 additions and 4 deletions

View file

@ -1,10 +1,11 @@
import dynamicImport from 'utils/dynamicImport.js';
import React from 'react'; import React from 'react';
import { Route, Switch } from 'react-router-dom'; import { Route, Switch } from 'react-router-dom';
import HomePage from 'pages/HomePage'; // or use the provided local homepage
import AboutPage from 'pages/AboutPage'; import AboutPage from 'pages/AboutPage';
import LoginPage from 'pages/LoginPage'; import LoginPage from 'pages/LoginPage';
import ShowPage from 'pages/ShowPage'; import ShowPage from 'pages/ShowPage';
import FourOhFourPage from 'containers/FourOhFourPage'; import FourOhFourPage from 'containers/FourOhFourPage';
const HomePage = dynamicImport('pages/HomePage'); // or use the provided local homepage
const App = () => { const App = () => {
return ( return (

View file

@ -0,0 +1,30 @@
const { componentsConfig } = require('../config/siteConfig.js');
function getDeepestChildValue (parent, childrenKeys) {
let childKey = childrenKeys.shift(); // .shift() retrieves the first element of array and removes it from array
let child = parent[childKey];
if (childrenKeys.length >= 1) {
return getDeepestChildValue(child, childrenKeys);
}
return child;
}
export const dynamicImport = (filePath) => {
// validate inputs
if (!filePath) {
throw new Error('no file path provided to dynamicImport()');
}
if (filePath.typeof !== 'string') {
throw new Error('file path provided to dynamicImport() must be a string');
}
// split out the file folders // filter out any empty or white-space-only strings
const folders = filePath.split('/').filter(folderName => folderName.replace(/\s/g, '').length);
// check for the component corresponding to file path in the site config object
// i.e. componentsConfig[folders[0]][folders[2][...][folders[n]]
const customComponent = getDeepestChildValue(componentsConfig, folders);
if (customComponent) {
return customComponent; // return custom component
} else {
return require(filePath); // return default component
}
};

View file

@ -10,7 +10,11 @@ function SiteConfig () {
this.auth = { this.auth = {
sessionKey: 'default', sessionKey: 'default',
}; };
this.components = {}; this.customComponents = {
components: {},
containers: {},
pages : {},
};
this.details = { this.details = {
description: 'Open-source, decentralized image and video sharing.', description: 'Open-source, decentralized image and video sharing.',
host : 'default', host : 'default',
@ -31,13 +35,13 @@ function SiteConfig () {
if (!config) { if (!config) {
return console.log('No site config received.'); return console.log('No site config received.');
} }
const { analytics, assetDefaults, auth, components, details, publishing } = config; const { analytics, assetDefaults, auth, customComponents, details, publishing } = config;
this.analytics = analytics; this.analytics = analytics;
this.assetDefaults = assetDefaults; this.assetDefaults = assetDefaults;
this.auth = auth; this.auth = auth;
this.components = components;
this.details = details; this.details = details;
this.publishing = publishing; this.publishing = publishing;
this.customComponents = customComponents;
}; };
}; };