Skip to content

Commit

Permalink
feat: 支持路由split
Browse files Browse the repository at this point in the history
  • Loading branch information
senwii committed Nov 25, 2019
1 parent 0f68c93 commit 7e70026
Show file tree
Hide file tree
Showing 18 changed files with 1,030 additions and 745 deletions.
14 changes: 12 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
}
"presets": [
[
"@babel/preset-env", {
"targets": {
"browsers": "last 2 Chrome versions",
},
},
],
"@babel/preset-react",
],
"plugins": ["@babel/plugin-syntax-dynamic-import"],
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist
yarn.lock
.vscode
.DS_Store
yarn-error.log
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@
> react模板项目
![ci action](https://github.com/senwii/react-template-project/workflows/ci/badge.svg)


技术栈:React Hooks + React Router DOM

特性:支持SPA/MPA

SPA页面结构参考`/src/pages/Index/`

新增MPA页面:`/src/pages/`目录下新建一个文件夹即可,参考`/src/pages/Index/`

访问页面:localhost:${port}/${pageFolderName}
1,556 changes: 842 additions & 714 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "NODE_ENV=develop webpack-dev-server",
"build:develop": "NODE_ENV=develop webpack",
"build": "NODE_ENV=product webpack"
},
"author": "senwii",
"homepage": "https://senii.github.io/react-template-project",
"license": "ISC",
"dependencies": {
"dragula": "^3.7.2",
"@loadable/component": "^5.10.3",
"react": "^16.9.0",
"react-dom": "^16.9.0"
"react-dom": "^16.9.0",
"react-router-dom": "^5.1.2"
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"autoprefixer": "^9.6.1",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^3.2.0",
"file-loader": "^4.2.0",
"html-webpack-plugin": "^4.0.0-beta.8",
"less": "^3.10.3",
"less-loader": "^5.0.0",
Expand Down
3 changes: 2 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<head>
<meta charset="utf-8" />
<title><%= title %></title>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1, maximum-scale=1, user-scalable=no"/>
</head>
<body>
<div id="app"></div>
</body>
</html>
</html>
21 changes: 21 additions & 0 deletions src/pages/Error/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from "react"
import ReactDOM from 'react-dom'

import './style.less'

import desc from './assets/404_not_found.png'

function App() {
return (
<div className="root">
<div className="desc">
<img src={ desc } />
</div>
</div>
);
}

ReactDOM.render(
<App />,
document.getElementById('app'),
)
Binary file added src/pages/Error/assets/404_not_found.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/pages/Error/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions src/pages/Error/style.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
html,
body {
height: 100%;
margin: 0;
}

#app {
width: 100%;
height: 100%;
display: flex;

.root {
display: flex;
flex: 1;
flex-wrap: wrap;
flex-direction: column;

.desc {
width: 100%;

& > img {
height: 75px;
display: block;
margin: 45px 0 0 45px;
}
}
}
}
33 changes: 27 additions & 6 deletions src/pages/Index/App.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import React, { useState, useRef } from 'react'
import React from "react"
import ReactDOM from 'react-dom'
import {
BrowserRouter,
Switch,
Route,
Link,
} from "react-router-dom"

import './style.less'

const App = () => {
return (
<div className="root">
Hello World
import routes, { basename } from './router'

function App() {
return (
<div className="root">
<BrowserRouter basename={ basename }>
<Link to="/hello">Hello</Link>
<span style={{ padding: '0 10px', lineHeight: '150px' }}>|</span>
<Link to="/about">About</Link>
<Switch>
{
routes.map((route, index) => (
<Route path={ route.path } key={ index } render={ props => (
<route.component {...props} routes={route.routes} />
)} />
))
}
</Switch>
</BrowserRouter>
</div>
)
);
}

ReactDOM.render(
Expand Down
21 changes: 21 additions & 0 deletions src/pages/Index/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import loadable from "@loadable/component"

const routes = [
{
path: '/hello',
component: loadable(() => import(/* webpackChunkName: "hello" */ './views/Hello')),
},
{
path: '/about',
component: loadable(() => import(/* webpackChunkName: "about" */ './views/About')),
}
]

// set current pageName as base
const basename = '/Index'

export {
basename,
}

export default routes
2 changes: 1 addition & 1 deletion src/pages/Index/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ body {
display: flex;

.root {
display: flex;
flex: 1;
text-align: center;
}
}
7 changes: 7 additions & 0 deletions src/pages/Index/views/About.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

export default function About() {
return (
<div>This is the <b>About</b> sub page view.</div>
)
}
7 changes: 7 additions & 0 deletions src/pages/Index/views/Hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

export default function Hello() {
return (
<div>Hello World.</div>
)
}
52 changes: 37 additions & 15 deletions webpack/webpack.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const HtmlWebpackPlugin = require('html-webpack-plugin')

const appDirName = process.cwd()

process.env.OUTPUT_DIR = 'dist'

// 入口+HTML模板配置
function getEntries() {
const entry = {}
Expand All @@ -20,7 +22,7 @@ function getEntries() {
templateParameters: {
title: dirName,
},
filename: `${dirName.toLowerCase()}/index.html`,
filename: `${dirName}/index.html`,
template: `${appDirName}/public/index.html`,
})
)
Expand All @@ -36,19 +38,25 @@ const { entry, htmlWebpackPluginList } = getEntries()

module.exports = {
entry,
// output: {
// path: __dirname + '/dist',
// filename: '[name]/app.[hash].js',
// publicPath: 'https://senwii.github.io/react-template-project',
// },
devServer: {
contentBase: `${appDirName}/dist`,
contentBase: `${appDirName}/${process.env.OUTPUT_DIR}`,
// host: '172.23.62.60',
compress: true,
historyApiFallback: {
rewrites: [
{ from: /^\/$/, to: '/index\/index.html' },
{
from: /\/([\s\S]+)\//,
to({ parsedUrl }) {
const pageName = parsedUrl.href.split('/')[1] || ''
if (Object.keys(entry).find(name => name === pageName) !== undefined) {
return `/${pageName}`
} else {
return '/'
}
}
},
],
},
},
},
resolve: {
alias: {
Expand All @@ -60,9 +68,9 @@ module.exports = {
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules\/(react|react-dom)/,
filename: 'vendor.[hash].js',
chunks: 'all',
test: /node_modules\/(react|react-dom|react-router-dom)\//,
name: 'vendor',
chunks: 'all',
},
},
},
Expand All @@ -82,16 +90,30 @@ module.exports = {
'postcss-loader',
'less-loader',
],
},
{
test: /\.(png|svg|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
name: '[name].[contenthash].[ext]',
outputPath: '/assets/',
},
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader',
],
},
],
},
plugins: [
...htmlWebpackPluginList,
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: '[name]/main.[hash].css',
chunkFilename: '[name]/[id].[hash].css',
filename: '[name]/main.[contenthash].css',
chunkFilename: '[name]/[id].[contenthash].css',
ignoreOrder: true,
}),
}),
]
}
6 changes: 4 additions & 2 deletions webpack/webpack.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const appDirName = process.cwd()
module.exports = {
mode: 'development',
output: {
path: `${appDirName}/dist`,
filename: '[name]/app.[hash].js',
path: `${appDirName}/${process.env.OUTPUT_DIR}`,
filename: '[name]/app.[contenthash].js',
chunkFilename: 'chunks/[name].[contenthash].js',
publicPath: '/',
},
}
5 changes: 3 additions & 2 deletions webpack/webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const appDirName = process.cwd()
module.exports = {
mode: 'production',
output: {
path: `${appDirName}/dist`,
filename: '[name]/app.[hash].js',
path: `${appDirName}/${process.env.OUTPUT_DIR}`,
filename: '[name]/app.[contenthash].js',
chunkFilename: 'chunks/[name].[contenthash].js',
publicPath: 'https://senwii.github.io/react-template-project',
},
}

0 comments on commit 7e70026

Please sign in to comment.