Skip to content

Commit 64b4c49

Browse files
author
yunkui.zhou
committed
调整路由结构
1 parent 979346c commit 64b4c49

15 files changed

+104
-75
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/**
22
* Created by zhouyunkui on 2017/6/23.
33
*/
4-
const fnc = function( evt ) {
5-
if ( this.preventDocMove ) {
4+
const fnc = function (evt) {
5+
if (this.preventDocMove) {
66
evt.preventDefault();
77
return false;
88
}
99
}
1010
export const preventDocMove = function () {
11-
//测试
12-
document.addEventListener( 'touchmove', fnc.bind( this ), {
11+
// 测试
12+
document.addEventListener('touchmove', fnc.bind(this), {
1313
passive: false
14-
} );
15-
}
14+
})
15+
};
1616
export const offPreventDefault = function () {
17-
document.removeEventListener( 'touchmove', fnc );
18-
}
17+
document.removeEventListener('touchmove', fnc)
18+
};

client/pages/example/common/js/siderStatusSwitch.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* Created by zhouyunkui on 2017/6/15.
33
*/
44
const switchStatus = function () {
5-
const { match } = this.props;
6-
let targetLink = document.querySelector(`.flag${match.path.replace(/\//g,'-')}`);
5+
const {match} = this.props;
6+
let targetLink = document.querySelector(`.flag${match.path.replace(/\//g, '-')}`);
77
let allLink = [ ...document.querySelectorAll('.sider .top-component')];
8-
allLink.map( ( link, index ) => {
9-
link.classList.remove( 'active' );
8+
allLink.map((link, index) => {
9+
link.classList.remove('active');
1010
})
1111
targetLink.classList.add('active');
1212
}
13-
export default switchStatus;
13+
export default switchStatus

client/pages/integration/redux/action.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ export function center(data = '') {
1616
}
1717
export function jump(path = '') {
1818
return push(path)
19-
}
19+
}

client/pages/integration/redux/reducer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export function home (state = {title: '个人首页内容'}, action) {
1717
default:
1818
return state
1919
}
20-
}
20+
}

client/pages/tools/lazyLoad/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import React from 'react'
55
import Loadable from 'react-loadable'
66
const loadingComponent = Loading => props => {
7-
//props.error:When the loader has errored
8-
//props.timedOut: When the loader has taken longer than the timeout
9-
//props.pastDelay: When the loader has taken longer than the delay
10-
//else: When the loader has just started
7+
// props.error:When the loader has errored
8+
// props.timedOut: When the loader has taken longer than the timeout
9+
// props.pastDelay: When the loader has taken longer than the delay
10+
// else: When the loader has just started
1111
return <Loading {...props}/>
12-
}
12+
};
1313
export default (prefix, Loading) => path => {
1414
return Loadable({
1515
loader: () => import(`../../${prefix}${path}`),
1616
loading: loadingComponent(Loading),
1717
timeout: 10000
1818
})
19-
}
19+
};

server/config/development.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require('babel-register')({
1111
]
1212
})
1313
const injectDevelopmentTools = (app) => {
14-
const config = require('../../webpack.config.local')
14+
const config = require('../../webpack.config.dev.babel')
1515
const webpack = require('webpack')
1616
const compiler = webpack(config)
1717
const webpackDevMiddleware = require('webpack-dev-middleware')

server/controller/components.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
import React from 'react'
4+
import {renderToString} from 'react-dom/server'
5+
import mapAssets from '../utils/mapAssets'
6+
import Component from '../../client/pages/example/App'
7+
import {StaticRouter} from 'react-router'
8+
import express from 'express'
9+
const router = express.Router()
10+
router.get('*', (req, res, next) => {
11+
//可以根据路径,针对某一个页面进行服务端渲染
12+
const location = `${req.baseUrl}${req.path}`
13+
console.log(req.path)
14+
const content = renderToString(<StaticRouter
15+
location={location}
16+
context={{}}
17+
basename={req.baseUrl}>
18+
<Component/>
19+
</StaticRouter>)
20+
res.render('components', {
21+
app: content,
22+
links: mapAssets('components/index.css'),
23+
scripts: mapAssets('components/index.js')
24+
})
25+
})
26+
27+
export default {
28+
router
29+
}

server/controller/integration.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
import express from 'express'
4+
import React from 'react'
5+
import {renderToString} from 'react-dom/server'
6+
import mapAssets from '../utils/mapAssets'
7+
import {StaticRouter} from 'react-router'
8+
import {Provider} from 'react-redux'
9+
import store from '../../client/pages/tools/store/forServer'
10+
import {Content} from '../../client/pages/integration/Root/index'
11+
import reducers from '../../client/pages/integration/store/'
12+
import Integration from '../../client/pages/integration/App'
13+
14+
const router = express.Router()
15+
router.get('*', (req, res, next) => {
16+
const location = `${req.baseUrl}${req.path}`
17+
const content = renderToString(<Provider store={store(reducers)}>
18+
<StaticRouter
19+
location={location}
20+
context={{}}
21+
basename={req.baseUrl}
22+
>
23+
<Content component={Integration}/>
24+
</StaticRouter>
25+
</Provider>)
26+
res.render('integration', {
27+
app: content,
28+
links: mapAssets('integration/index.css'),
29+
scripts: mapAssets('integration/index.js')
30+
})
31+
})
32+
33+
export default {
34+
router
35+
}

server/middleware/extractMapping.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* global.staticAssetsMapping
44
*/
55
import path from 'path'
6-
const {NODE_ENV} = process.env
6+
const config = require('config')
77
const mappingRealativePath = '../../public/mapping/mapping.json'
88
const mappingRealPath = path.resolve(__dirname, mappingRealativePath)
99
let mapping = {}
1010
const extractMapping = (req, res, next) => {
11-
if (NODE_ENV === 'local') {
11+
if (config.env === 'development') {
1212
mapping = res.locals.webpackStats.compilation.assets['mapping.json'].source()
1313
} else {
1414
// 每次请求清除模块缓存

server/routes/pages.js

+6-44
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,19 @@
11
/**
22
* Created by ink on 2018/5/3.
33
*/
4-
5-
import React from 'react'
6-
import {renderToString} from 'react-dom/server'
7-
import Component from '../../client/pages/example/App'
84
import extractMapping from '../middleware/extractMapping'
9-
import mapAssets from '../utils/mapAssets'
10-
import {StaticRouter} from 'react-router'
11-
12-
import {Provider} from 'react-redux'
13-
import store from '../../client/pages/tools/store/forServer'
14-
import {Content} from '../../client/pages/integration/Root/index'
15-
import Integration from '../../client/pages/integration/App'
16-
import reducers from '../../client/pages/integration/store/'
175
import express from 'express'
6+
7+
import integrationController from '../controller/integration'
8+
import componentsController from '../controller/components'
189
const router = express.Router()
1910

2011
router.get('/', (req, res) => {
2112
res.status(301).set('Location', '/').end()
2213
})
2314

24-
router.get('/integration(/:page)?', extractMapping, (req, res, next) => {
25-
const location = `${req.baseUrl}${req.path}`
26-
const content = renderToString(<Provider store={store(reducers)}>
27-
<StaticRouter
28-
location={location}
29-
context={{}}
30-
basename="/p/integration"
31-
>
32-
<Content component={Integration}/>
33-
</StaticRouter>
34-
</Provider>)
35-
res.render('integration', {
36-
app: content,
37-
links: mapAssets('integration/index.css'),
38-
scripts: mapAssets('integration/index.js')
39-
})
40-
})
15+
router.use('/integration', extractMapping, integrationController.router)
16+
17+
router.use('/components', extractMapping, componentsController.router)
4118

42-
router.get('/components(/:page)?', extractMapping, (req, res, next) => {
43-
//可以根据路径,针对某一个页面进行服务端渲染
44-
const location = `${req.baseUrl}${req.path}`
45-
const content = renderToString(<StaticRouter
46-
location={location}
47-
context={{}}
48-
basename="/p/components">
49-
<Component/>
50-
</StaticRouter>)
51-
res.render('components', {
52-
app: content,
53-
links: mapAssets('components/index.css'),
54-
scripts: mapAssets('components/index.js')
55-
})
56-
})
5719
export default router

server/views/components.hbs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
<html>
1+
<html lang="zh-cn">
22
<head>
3+
<meta charset="utf-8"/>
4+
<meta name="description" content="react app"/>
5+
<meta http-equiv="X-UA-Compatible" conent="IE=Edge,chrome=1"/>
36
<meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1" />
47
<title>{{title}}</title>
58
{{{links}}}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const NODE_ENV = process.env.NODE_ENV
22
let config
33
if (NODE_ENV === 'local') {
4-
config = require('./webpack.config.local')
4+
config = require('./webpack.config.dev.babel')
55
} else {
6-
config = require('./webpack.config.pro')
6+
config = require('./webpack.config.prod.babel')
77
}
88
module.exports = config

webpack.config.common.js renamed to webpack.config.common.babel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const entry = {
1010
}
1111
const rules = [{
1212
enforce: 'pre',
13-
test: '/\.jsx?$/',
13+
test: /\.jsx?$/,
1414
exclude: /node_modules/,
1515
use: ['eslint-loader']
1616
}, {

webpack.config.local.js renamed to webpack.config.dev.babel.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const path = require('path')
55
const webpack = require('webpack')
66
const contentPath = path.resolve(__dirname, 'public')
7-
const commonConfig = require('./webpack.config.common')
7+
const commonConfig = require('./webpack.config.common.babel')
88
const publicPath = '/'
99
const copyEntry = Object.assign({}, commonConfig.entry)
1010
const entry = {}
@@ -13,7 +13,7 @@ Object.keys(copyEntry).map(item => {
1313
temp.unshift('webpack-hot-middleware/client?path=/__webpack_hmr&timeout=2000&reload=true')
1414
entry[item] = temp
1515
})
16-
console.log('读取webpack devConfig')
16+
console.log('读取webpack.config.dev.babel.js')
1717
const config = {
1818
mode: 'development',
1919
devtool: 'eval-source-map',

webpack.config.pro.js renamed to webpack.config.prod.babel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const contentPath = path.resolve(__dirname, 'dist/public/mapping')
77
//extract-text-webpack-plugin 还是beta版有bug,不同入口的css内容都一样
88
const ExtractCssChunks = require('extract-text-webpack-plugin')
99
const publicPath = '/mapping/'
10-
const commonConfig = require('./webpack.config.common')
10+
const commonConfig = require('./webpack.config.common.babel')
1111
console.log('读取webpack production')
1212
const config = {
1313
mode: 'production',

0 commit comments

Comments
 (0)