1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00

feat: using code splitting on routers

* enable syntax-dynamic-import
This commit is contained in:
Juan Picado @jotadeveloper 2018-06-03 18:34:41 +02:00
parent e0bf1cd5f0
commit 0af6f5a865
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
5 changed files with 51 additions and 8 deletions

@ -22,7 +22,8 @@
"react-hot-loader/babel", "react-hot-loader/babel",
"transform-runtime", "transform-runtime",
"transform-object-rest-spread", "transform-object-rest-spread",
"transform-decorators-legacy" "transform-decorators-legacy",
"syntax-dynamic-import"
] ]
}, },
"test": { "test": {

@ -43,8 +43,7 @@ module.exports = function(config, auth, storage) {
const defaultTitle = 'Verdaccio'; const defaultTitle = 'Verdaccio';
let webPage = template let webPage = template
.replace(/ToReplaceByVerdaccio/g, base) .replace(/ToReplaceByVerdaccio/g, base)
.replace(/ToReplaceByTitle/g, _.get(config, 'web.title') ? config.web.title : defaultTitle) .replace(/ToReplaceByTitle/g, _.get(config, 'web.title') ? config.web.title : defaultTitle);
.replace(/(main.*\.js|style.*\.css)/g, `${base}/-/static/$1`);
res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Type', 'text/html');

@ -1,11 +1,13 @@
import React from 'react'; import React from 'react';
import {HashRouter as Router, Route, Switch} from 'react-router-dom'; import {HashRouter as Router, Route, Switch} from 'react-router-dom';
import {asyncComponent} from './utils/asyncComponent';
import Header from './components/Header'; import Header from './components/Header';
import Home from './modules/home';
import Detail from './modules/detail';
import Footer from './components/Footer'; import Footer from './components/Footer';
const DetailPackage = asyncComponent(() => import('./modules/detail'));
const HomePage = asyncComponent(() => import('./modules/home'));
const RouterApp = () => { const RouterApp = () => {
return ( return (
<Router> <Router>
@ -13,9 +15,9 @@ const RouterApp = () => {
<Header/> <Header/>
<div className="container"> <div className="container">
<Switch> <Switch>
<Route exact path="/(search/:keyword)?" component={ Home } /> <Route exact path="/(search/:keyword)?" component={ HomePage } />
<Route exact path="/detail/@:scope/:package" component={Detail} /> <Route exact path="/detail/@:scope/:package" component={DetailPackage} />
<Route exact path="/detail/:package" component={Detail} /> <Route exact path="/detail/:package" component={DetailPackage} />
</Switch> </Switch>
</div> </div>
<Footer/> <Footer/>

@ -0,0 +1,24 @@
import React from 'react';
export function asyncComponent(getComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = {Component: AsyncComponent.Component};
componentWillMount() {
if (!this.state.Component) {
getComponent().then(({default: Component}) => {
AsyncComponent.Component = Component;
this.setState({Component});
});
}
}
render() {
const {Component} = this.state;
if (Component) {
return <Component {...this.props} />;
}
return null;
}
};
}

@ -7,6 +7,7 @@ module.exports = {
output: { output: {
path: `${env.APP_ROOT}/static/`, path: `${env.APP_ROOT}/static/`,
filename: '[name].[hash].js', filename: '[name].[hash].js',
publicPath: '/-/static',
}, },
resolve: { resolve: {
@ -22,6 +23,22 @@ module.exports = {
}), }),
], ],
optimization: {
runtimeChunk: {
name: 'manifest',
},
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
priority: -20,
chunks: 'all',
},
},
},
},
module: { module: {
rules: [ rules: [
/* Pre loader */ /* Pre loader */