F #3951: loadable components

Signed-off-by: Jorge Lobo <jlobo@opennebula.io>
This commit is contained in:
Tino Vazquez committed 2021-03-10 17:36:13 +01:00
1 parent 3c9265097b
commit 4d9b08d9a4
9 files changed
+1564 -1223

No files matched your search

+2 -1
View File
@@ -11,6 +11,7 @@
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-optional-chaining"
"@babel/plugin-proposal-optional-chaining",
"@loadable/babel-plugin"
]
}
+1512 -1171
View File
File diff suppressed because it is too large. Load diff
+4
View File
@@ -54,6 +54,10 @@
"@babel/preset-env": "7.12.13",
"@babel/preset-react": "7.12.13",
"@hookform/resolvers": "0.1.1",
"@loadable/babel-plugin": "5.13.2",
"@loadable/component": "5.14.1",
"@loadable/server": "5.14.2",
"@loadable/webpack-plugin": "5.14.2",
"@material-ui/core": "4.11.0",
"@material-ui/icons": "4.9.1",
"@material-ui/lab": "4.0.0-alpha.56",
+8 -16
View File
@@ -3,15 +3,7 @@ import {
FormatListBulleted as TemplatesIcons,
Apps as InstancesIcons
} from '@material-ui/icons'
import Login from 'client/containers/Login'
import Dashboard from 'client/containers/Dashboard'
import Settings from 'client/containers/Settings'
import ApplicationsTemplates from 'client/containers/ApplicationsTemplates'
import ApplicationsTemplatesCreateForm from 'client/containers/ApplicationsTemplates/Form/Create'
import ApplicationsInstances from 'client/containers/ApplicationsInstances'
import loadable from '@loadable/component'
export const PATH = {
LOGIN: '/',
@@ -32,7 +24,7 @@ export const ENDPOINTS = [
label: 'Login',
path: PATH.LOGIN,
authenticated: false,
Component: Login
Component: loadable(() => import('client/containers/Login'))
},
{
label: 'Dashboard',
@@ -40,14 +32,14 @@ export const ENDPOINTS = [
authenticated: true,
sidebar: true,
icon: DashboardIcon,
Component: Dashboard
Component: loadable(() => import('client/containers/Dashboard'))
},
{
label: 'Settings',
path: PATH.SETTINGS,
authenticated: true,
header: true,
Component: Settings
Component: loadable(() => import('client/containers/Settings'), { ssr: false })
},
{
label: 'Templates',
@@ -55,19 +47,19 @@ export const ENDPOINTS = [
authenticated: true,
sidebar: true,
icon: TemplatesIcons,
Component: ApplicationsTemplates
Component: loadable(() => import('client/containers/ApplicationsTemplates'), { ssr: false })
},
{
label: 'Create Application template',
path: PATH.APPLICATIONS_TEMPLATES.CREATE,
authenticated: true,
Component: ApplicationsTemplatesCreateForm
Component: loadable(() => import('client/containers/ApplicationsTemplates/Form/Create'), { ssr: false })
},
{
label: 'Edit Application template',
path: PATH.APPLICATIONS_TEMPLATES.EDIT,
authenticated: true,
Component: ApplicationsTemplatesCreateForm
Component: loadable(() => import('client/containers/ApplicationsTemplates/Form/Create'), { ssr: false })
},
{
label: 'Instances',
@@ -75,7 +67,7 @@ export const ENDPOINTS = [
authenticated: true,
sidebar: true,
icon: InstancesIcons,
Component: ApplicationsInstances
Component: loadable(() => import('client/containers/ApplicationsInstances'), { ssr: false })
}
]
+10 -19
View File
@@ -4,16 +4,7 @@ import {
SettingsSystemDaydream as ProvisionsIcon,
Settings as SettingsIcon
} from '@material-ui/icons'
import Login from 'client/containers/Login'
import Dashboard from 'client/containers/Dashboard/Provision'
import Settings from 'client/containers/Settings'
import Providers from 'client/containers/Providers'
import ProvidersCreateForm from 'client/containers/Providers/Form/Create'
import Provisions from 'client/containers/Provisions'
import ProvisionCreateForm from 'client/containers/Provisions/Form/Create'
import loadable from '@loadable/component'
export const PATH = {
LOGIN: '/',
@@ -37,7 +28,7 @@ export const ENDPOINTS = [
label: 'Login',
path: PATH.LOGIN,
authenticated: false,
Component: Login
Component: loadable(() => import('client/containers/Login'))
},
{
label: 'Dashboard',
@@ -45,7 +36,7 @@ export const ENDPOINTS = [
authenticated: true,
sidebar: true,
icon: DashboardIcon,
Component: Dashboard
Component: loadable(() => import('client/containers/Dashboard/Provision'))
},
{
label: 'Providers',
@@ -53,19 +44,19 @@ export const ENDPOINTS = [
authenticated: true,
sidebar: true,
icon: ProvidersIcon,
Component: Providers
Component: loadable(() => import('client/containers/Providers'), { ssr: false })
},
{
label: 'Create Provider',
path: PATH.PROVIDERS.CREATE,
authenticated: true,
Component: ProvidersCreateForm
Component: loadable(() => import('client/containers/Providers/Form/Create'), { ssr: false })
},
{
label: 'Edit Provider template',
path: PATH.PROVIDERS.EDIT,
authenticated: true,
Component: ProvidersCreateForm
Component: loadable(() => import('client/containers/Providers/Form/Create'), { ssr: false })
},
{
label: 'Provisions',
@@ -73,19 +64,19 @@ export const ENDPOINTS = [
authenticated: true,
sidebar: true,
icon: ProvisionsIcon,
Component: Provisions
Component: loadable(() => import('client/containers/Provisions'), { ssr: false })
},
{
label: 'Create Provision',
path: PATH.PROVISIONS.CREATE,
authenticated: true,
Component: ProvisionCreateForm
Component: loadable(() => import('client/containers/Provisions/Form/Create'), { ssr: false })
},
{
label: 'Edit Provision template',
path: PATH.PROVISIONS.EDIT,
authenticated: true,
Component: ProvisionCreateForm
Component: loadable(() => import('client/containers/Provisions/Form/Create'), { ssr: false })
},
{
label: 'Settings',
@@ -93,7 +84,7 @@ export const ENDPOINTS = [
authenticated: true,
sidebar: true,
icon: SettingsIcon,
Component: Settings
Component: loadable(() => import('client/containers/Settings'), { ssr: false })
}
]
@@ -3,13 +3,15 @@ const { Router } = require('express')
const { env } = require('process')
const { renderToString } = require('react-dom/server')
const root = require('window-or-global')
const path = require('path')
const { createStore, compose, applyMiddleware } = require('redux')
const thunk = require('redux-thunk').default
const { ServerStyleSheets } = require('@material-ui/core/styles')
const { ChunkExtractor } = require('@loadable/server')
const rootReducer = require('client/reducers')
const { getConfig } = require('server/utils/yml')
const {
availableLanguages, defaultWebpackMode, defaultApps
availableLanguages, defaultWebpackMode, defaultApps, defaultFileStats
} = require('server/utils/constants/defaults')
const { APP_URL, STATIC_FILES_URL } = require('client/constants')
const { capitalize } = require('client/utils')
@@ -36,8 +38,8 @@ router.get('*', (req, res) => {
let component = ''
let css = ''
let storeRender = ''
let chunks = ''
// production
if (env && (!env.NODE_ENV || (env.NODE_ENV && env.NODE_ENV !== defaultWebpackMode))) {
const App = require('../../../client/dev/_app.js').default
const sheets = new ServerStyleSheets()
@@ -52,17 +54,21 @@ router.get('*', (req, res) => {
}
})
chunks = `<script src='${APP_URL}/client/1.bundle.${app}.js'></script>`
// loadable
const statsFile = path.resolve(__dirname, 'client', app + defaultFileStats)
const extractor = new ChunkExtractor({ statsFile })
// SSR redux store
store = createStore(rootReducer(), composeEnhancer(applyMiddleware(thunk)))
storeRender = `<script id="preloadState">window.__PRELOADED_STATE__ = ${
JSON.stringify(store.getState()).replace(/</g, '\\u003c')
}</script>`
component = renderToString(
sheets.collect(
<App location={req.url} context={context} store={store} />
extractor.collectChunks(
sheets.collect(
<App location={req.url} context={context} store={store} />
)
)
)
@@ -88,11 +94,10 @@ router.get('*', (req, res) => {
${storeRender}
<script>${`langs = ${JSON.stringify(scriptLanguages)}`}</script>
<script src='${APP_URL}/client/bundle.${app}.js'></script>
${chunks}
</body>
</html>
`
res.send(html)
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
})
module.exports = router
@@ -58,6 +58,7 @@ const defaults = {
PUT: 'PUT',
DELETE: 'DELETE'
},
defaultFileStats: '-stats.json',
from: {
resource: 'RESOURCE',
query: 'QUERY',
+11 -7
View File
@@ -1,7 +1,8 @@
const path = require('path')
const webpack = require('webpack')
const CopyPlugin = require('copy-webpack-plugin')
const { defaultApps } = require('./src/server/utils/constants/defaults')
const LoadablePlugin = require('@loadable/webpack-plugin')
const { defaultApps, defaultFileStats, defaultAppName } = require('./src/server/utils/constants/defaults')
const js = {
test: /\.js$/,
@@ -10,11 +11,14 @@ const js = {
}
const bundle = ({ assets = false, name = 'flow' }) => {
const plugins = [new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
})]
const plugins = [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new LoadablePlugin({ filename: name + defaultFileStats })
]
if (assets) {
plugins.push(new CopyPlugin({
patterns: [
@@ -32,7 +36,7 @@ const bundle = ({ assets = false, name = 'flow' }) => {
output: {
path: path.resolve(__dirname, 'dist', 'client'),
filename: `bundle.${name}.js`,
publicPath: '/client'
publicPath: `/${defaultAppName}/client/`
},
stats: {
warnings: false
+3 -1
View File
@@ -1,7 +1,6 @@
const path = require('path')
const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')
const CopyPlugin = require('copy-webpack-plugin')
const { defaultProductionWebpackMode } = require('./src/server/utils/constants/defaults')
const js = {
@@ -30,6 +29,9 @@ module.exports = {
'process.env': {
NODE_ENV: JSON.stringify(defaultProductionWebpackMode)
}
}),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
],
module: {