-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
86 changed files
with
1,967 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "react-router-5-js", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start:browser": "cross-env HISTORY_MODE=browser webpack serve --mode development", | ||
"start:hash": "cross-env HISTORY_MODE=hash webpack serve --mode development" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"history": "^4.9.0", | ||
"qs": "^6.11.2", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"react-router": "5.3.4", | ||
"react-router-dom": "5.3.4" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.2.12", | ||
"@types/react-dom": "^18.2.5", | ||
"@types/react-router": "5.1.20", | ||
"@types/react-router-dom": "5.3.3", | ||
"html-webpack-plugin": "^5.5.3", | ||
"ts-loader": "^9.4.3", | ||
"typescript": "^4.9.5", | ||
"webpack": "^5.87.0", | ||
"webpack-cli": "^5.1.4", | ||
"webpack-dev-server": "^4.15.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>这是react-router-5-js</title> | ||
</head> | ||
<body> | ||
<h1>这是react-router-5-js</h1> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import React from 'react' | ||
|
||
import { | ||
Route, | ||
Switch, | ||
} from 'react-router-dom' | ||
import history, { Router } from '~history' | ||
const context = require.context('@/pages', true, /index\.page\./) | ||
|
||
const routePaths = context.keys().map((path) => { | ||
const Component = context(path) | ||
// 对于pages/index.page.js,认为是首页 | ||
path = path.replace(/\.(.*?)\/index\.page\.jsx?$/, '$1') || '/' | ||
return { | ||
path, | ||
Component, | ||
} | ||
}) | ||
|
||
const commonLiStyle = { marginRight: 10, color: 'blue', cursor: 'pointer' } | ||
function Layout() { | ||
return ( | ||
<> | ||
<button onClick={() => { location.href = '/' }}>回到首页</button> | ||
<ul> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.OPEN_ORDER({ | ||
/** 这里因为order下面又index.params.ts定义了传参类型,所以有参数类型提示,点击对应参数会跳到对应文件 */ | ||
order_id: '123', | ||
enter_order_type: 'OPEN', | ||
customer_names: ['name1', 'name2'], | ||
})} | ||
> | ||
OPEN_ORDER | ||
</li> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.TO_ORDER({ | ||
/** 这里因为order下面又index.params.ts定义了传参类型,所以有参数类型提示,点击对应参数会跳到对应文件 */ | ||
order_id: '123', | ||
enter_order_type: 'OPEN', | ||
customer_names: ['name1', 'name2'], | ||
})} | ||
> | ||
TO_ORDER | ||
</li> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.REPLACE_ORDER({ | ||
order_id: '123', | ||
enter_order_type: 'REPLACE', | ||
customer_names: ['name1', 'name2'], | ||
})} | ||
> | ||
REPLACE_ORDER | ||
</li> | ||
</ul> | ||
|
||
<ul> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.OPEN_ORDER_DETAIL()} | ||
> | ||
OPEN_ORDER_DETAIL | ||
</li> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.TO_ORDER_DETAIL()} | ||
> | ||
TO_ORDER_DETAIL | ||
</li> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.REPLACE_ORDER_DETAIL()} | ||
> | ||
REPLACE_ORDER_DETAIL | ||
</li> | ||
</ul> | ||
|
||
<ul> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.OPEN_MY()} | ||
> | ||
OPEN_MY | ||
</li> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.TO_MY()} | ||
> | ||
TO_MY | ||
</li> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => history.OPEN_MY()} | ||
> | ||
REPLACE_MY | ||
</li> | ||
</ul> | ||
<hr /> | ||
|
||
{ | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
routePaths.filter(({ path }) => !['/order', '/order/detail', '/my'].includes(path)).map(({ path }) => { | ||
const name = path.slice(1).toUpperCase().replace(/\//g, '_') || '$INDEX' | ||
return ( | ||
<ul key={name}> | ||
<li | ||
style={commonLiStyle} | ||
onClick={() => { history[`OPEN_${name}`]() }} | ||
> | ||
OPEN_{name} | ||
</li> | ||
<li | ||
style={{ marginRight: 10, color: 'blue', cursor: 'pointer' }} | ||
onClick={() => { history[`TO_${name}`]() }} | ||
> | ||
TO_{name} | ||
</li> | ||
<li | ||
style={{ marginRight: 10, color: 'blue', cursor: 'pointer' }} | ||
onClick={() => { history[`OPEN_${name}`]() }} | ||
> | ||
REPLACE_{name} | ||
</li> | ||
</ul> | ||
) | ||
}) | ||
} | ||
<hr /> | ||
|
||
</> | ||
) | ||
} | ||
|
||
function App() { | ||
return ( | ||
<Router> | ||
<Layout/> | ||
<Switch> | ||
{routePaths.map(({ path, Component }) => { | ||
return <Route path={path} exact component={Component.default} key={path} /> | ||
})} | ||
</Switch> | ||
</Router> | ||
) | ||
} | ||
|
||
export default App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { createBrowserHistory } from 'history' | ||
|
||
export default createBrowserHistory() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ declare const require: { | |
<T>(id: string): T; | ||
}; | ||
}; | ||
|
||
declare const HISTORY_MODE: 'hash' | 'browser' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { createHashHistory } from 'history' | ||
export default createHashHistory() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react' | ||
|
||
import ReactDOM from 'react-dom/client' | ||
import App from './app' | ||
|
||
ReactDOM.createRoot(document.getElementById('root')).render(<App />) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react' | ||
|
||
export default () => { | ||
return (<div>这是首页</div>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react' | ||
|
||
export default () => { | ||
return ( | ||
<div>这是我的页</div> | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
playgrounds/react-router-5-js/src/pages/my/setting/detail/index.page.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react' | ||
|
||
export default () => { | ||
return ( | ||
<div>这是我的设置详情页</div> | ||
) | ||
} |
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
playgrounds/react-router-5-js/src/pages/my/setting/info/index.page.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react' | ||
|
||
export default () => { | ||
return ( | ||
<div>这是我的设置Info页</div> | ||
) | ||
} |
File renamed without changes.
11 changes: 11 additions & 0 deletions
11
playgrounds/react-router-5-js/src/pages/order/detail/index.page.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react' | ||
|
||
import { useLocation } from 'react-router' | ||
import qs from 'qs' | ||
export default () => { | ||
const { search } = useLocation() | ||
console.log(qs.parse(search)) | ||
return ( | ||
<div>这是订单详情页</div> | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
playgrounds/react-router-5-js/src/pages/order/index.page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react' | ||
|
||
import { useSearchParams } from '~history' | ||
|
||
export default () => { | ||
const params = useSearchParams() | ||
console.log(params, params.customer_names) | ||
return ( | ||
<div>这是order</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
playgrounds/react-router-5-js/src/pages/order/xxx/index.page.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react' | ||
|
||
import { useParams } from 'react-router' | ||
export default () => { | ||
const params = useParams() | ||
console.log(params) | ||
|
||
return ( | ||
<div>这是order 的xxx</div> | ||
) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const path = require('path') | ||
const HtmlWebpackPlugin = require('html-webpack-plugin') | ||
const { DefinePlugin } = require('webpack') | ||
const GenerateHistoryMethodWebpackPlugin = require('../../dist/genetate-history-method-webpack-plugin.cjs') | ||
const isHash = process.env.HISTORY_MODE === 'hash' | ||
module.exports = { | ||
entry: './src/index.jsx', | ||
output: { | ||
path: path.resolve(__dirname, 'dist'), | ||
filename: 'bundle.js', | ||
publicPath: '/', // 重要,否则会报错 | ||
}, | ||
resolve: { | ||
extensions: ['.ts', '.tsx', '.js', '.jsx'], | ||
alias: { | ||
'@': path.resolve(__dirname, 'src'), | ||
}, | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.(t|j)sx?$/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
presets: ['@babel/preset-react'], | ||
}, | ||
}, | ||
}, | ||
{ | ||
test: /\.tsx?$/, | ||
use: [ | ||
{ | ||
loader: 'ts-loader', | ||
options: { | ||
transpileOnly: true, | ||
}, | ||
}], | ||
}, | ||
], | ||
}, | ||
plugins: [ | ||
new DefinePlugin({ | ||
HISTORY_MODE: JSON.stringify(process.env.HISTORY_MODE), | ||
}), | ||
new HtmlWebpackPlugin({ | ||
template: 'public/index.html', | ||
}), | ||
new GenerateHistoryMethodWebpackPlugin({ | ||
pagesRootPath: path.resolve(__dirname, 'src/pages'), | ||
originHistoryModuleName: isHash ? '@/hash_history' : '@/browser_history', | ||
mode: process.env.HISTORY_MODE, | ||
reactRouterVersion: 5, | ||
}), | ||
// ...其他插件 | ||
], | ||
devServer: { | ||
historyApiFallback: true, | ||
port: 3000, | ||
}, | ||
ignoreWarnings: [ | ||
/was not found in '~history/, | ||
], | ||
} |
Oops, something went wrong.