You can use the power of dynamically loaded pieces. For example, imagine this routing layout:
switch(routeName) {
case 'home':
const homePage = require('homePage');
homePage();
break;
case 'contact':
const contactPage = require('contactPage');
contactPage();
break;
case 'dashboard':
const dashboardPage = require('dashboardPage');
dashboardPage();
break;
case 'informationForLoggedUser':
const informationForLoggedUserPage = require('informationForLoggedUserPage');
informationForLoggedUserPage();
break;
};
In the above example, all your routes will be loaded into one file bundle.js. To change this, you can use force require.ensure. Wrap protected routes in require.ensurewith the third parameter , naming this fragment as protected(this name may be different - this is just an example).
switch(routeName) {
case 'home':
const homePage = require('homePage');
homePage();
break;
case 'contact':
const contactPage = require('contactPage');
contactPage();
break;
case 'dashboard':
require.ensure([], () => {
const dashboardPage = require('dashboardPage');
dashboardPage();
}, 'protected');
break;
case 'informationForLoggedUser':
require.ensure([], () => {
const informationForLoggedUserPage = require('informationForLoggedUserPage');
informationForLoggedUserPage();
}, 'protected');
break;
};
In yours webpack.config.js, if you have this configuration:
entry: path.resolve('src', 'main.js'),
output: {
path: path.resolve('build'),
filename: '[name].js',
publicPath: '/'
},
The web package will create these files:
main.js
1.protected.js
Now you must provide your own protection on the backup — do not send the *.protected.jsfile to unauthenticated users .
source
share