Skip to content

Commit e31adc2

Browse files
committed
add px2rem-loader
1 parent 3f9d77b commit e31adc2

File tree

11 files changed

+178
-26
lines changed

11 files changed

+178
-26
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = {
5151
'react/jsx-first-prop-new-line': 'off',
5252
'react/jsx-indent': [ 'error', 2 ],
5353
'react/jsx-indent-props': [2, 2],
54+
'react/prop-types': 0,
5455

5556
// @typescript-eslint
5657
'@typescript-eslint/indent': [2, 2],

build/px2rem-loader.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const loaderUtils = require('loader-utils')
2+
3+
const pxReg = /\b(\d+(\.\d+)?)PX\b/g
4+
5+
module.exports = function(content) {
6+
7+
const options = loaderUtils.getOptions(this)
8+
9+
const config = Object.assign({}, {
10+
remRoot: 40,
11+
fixed: 4
12+
}, options)
13+
14+
if(pxReg.test(content)) {
15+
return content.replace(pxReg, ($0, $1) => {
16+
let val = $1 / config.remRoot;
17+
val = parseFloat(val.toFixed(config.fixed));
18+
return val === 0 ? val : val + 'rem';
19+
})
20+
}
21+
22+
return content
23+
}

build/webpack.base.conf.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ module.exports = {
2626
filename: 'static/js/[name].[hash:8].js'
2727
},
2828

29+
resolveLoader: {
30+
modules: [
31+
resolve('build'),
32+
'node_modules'
33+
]
34+
},
35+
2936
module: {
3037
rules: [
3138
// {
@@ -35,6 +42,14 @@ module.exports = {
3542
// transpileOnly: true
3643
// }
3744
// },
45+
{
46+
test: /\.(j|t)sx?$/,
47+
loader: 'px2rem-loader',
48+
options: {
49+
remRoot: 40,
50+
fixed: 6
51+
}
52+
},
3853
{
3954
test: /\.(j|t)sx?$/,
4055
include: [

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@
8181
"src/**/*.{js,jsx,ts,tsx}": [
8282
"npm run lint:fix",
8383
"git add"
84-
],
85-
"src/**/*.{css,less,json,html,md,markdown}": [
86-
"prettier --write",
87-
"git add"
8884
]
8985
}
9086
}

src/index.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
function getSize() {
1313
var w = _html.clientWidth;
1414
if(w <= 320) {
15-
_html.style.fontSize = '17px';
16-
} else if(w >= 750) {
17-
_html.style.fontSize = '40px';
15+
_html.style.fontSize = '17.06666666px';
1816
} else {
1917
_html.style.fontSize = w/750*40 + 'px';
2018
}

src/pages/app.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,21 @@ const { lazy, Suspense } = React
88
const Home = lazy(() => import( /* webpackChunkName:"home" */'@pages/home/home'))
99
const Login = lazy(() => import( /* webpackChunkName:"login" */'@pages/login/login'))
1010

11-
class App extends React.Component {
11+
const App: React.FC = () => {
1212

13-
render(){
14-
15-
return (
16-
<Provider {...stores}>
17-
<Router>
18-
<Suspense fallback={<Loading size="large"/>}>
19-
<Switch>
20-
<Route path="/home" component={(props: any) => <Home {...props}/>}/>
21-
<Route path="/login" exact component={(props: any) => <Login {...props}/>}/>
22-
<Redirect to="/home"/>
23-
</Switch>
24-
</Suspense>
25-
</Router>
26-
</Provider>
27-
)
28-
}
13+
return (
14+
<Provider {...stores}>
15+
<Router>
16+
<Suspense fallback={<Loading size="large"/>}>
17+
<Switch>
18+
<Route path="/home" component={(props: any) => <Home {...props}/>}/>
19+
<Route path="/login" exact component={(props: any) => <Login {...props}/>}/>
20+
<Redirect to="/home"/>
21+
</Switch>
22+
</Suspense>
23+
</Router>
24+
</Provider>
25+
)
2926
}
3027

3128
export default App

src/pages/dashboard/dashboard.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export default class Dashboard extends React.Component {
55
return <div>
66
<p>Welcome to Dashboard!</p>
77
<div>GraphQL Application. </div>
8+
<p style={{width: '400PX', height: 40, fontSize: '24px'}}>测试rem</p>
9+
<div>66px</div>
810
</div>
911
}
1012
}

src/pages/demo/demoHooks.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as React from 'react'
2+
import { Button } from 'antd'
3+
4+
5+
function useState<T>(initialValue: T): [T, (newState: T) => void] {
6+
let state = initialValue
7+
8+
function setState(newState: T) {
9+
state = newState
10+
// render()
11+
}
12+
return [state, setState]
13+
}
14+
15+
16+
export const HookButton: React.FC = props => {
17+
18+
let [count, setCount] = React.useState(0)
19+
20+
React.useEffect(() => {
21+
console.log(count, '_v')
22+
}, [count > 5])
23+
24+
return <Button onClick={() => setCount(++count)}>{count}</Button>
25+
}
26+
27+
const AppContext = React.createContext(null)
28+
29+
const { Provider, Consumer } = AppContext
30+
31+
const DemoContextGrandson: React.FC = props => {
32+
33+
const msg = React.useContext(AppContext)
34+
35+
return <div>孙子节点:{msg}</div>
36+
}
37+
38+
const DemoContextSon: React.FC = props => {
39+
40+
return <Consumer>
41+
{/* <DemoContextGrandson></DemoContextGrandson> */}
42+
</Consumer>
43+
}
44+
45+
46+
47+
const DemoContext: React.FC = props => {
48+
49+
return <Provider value="this is test context">
50+
<DemoContextSon></DemoContextSon>
51+
</Provider>
52+
}
53+

src/pages/demo/demoRedux.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react'
22
import { Row, Col, Button, Badge, Icon, Form, Input, Select } from 'antd'
33
import { ReduxProvider, connect } from '../../plugins/react-redux'
44
import { store, countActions } from './reducer'
5+
import { HookButton } from './demoHooks'
56

67
console.log(store.getState(), '---store')
78

@@ -61,6 +62,7 @@ class DemoRedux extends React.Component<any> {
6162
<Button onClick={ADD}>
6263
<Icon type="plus" />
6364
</Button>
65+
<HookButton></HookButton>
6466
</Col>
6567
</Row>
6668
</section>

src/plugins/react-redux/connect.tsx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react'
22
import PropTypes from 'prop-types'
33
import { ICreateStore } from '../redux'
4+
import {} from './provider'
45

56
export function connect<T>(
67
mapStateToProps?: Function,
@@ -53,4 +54,58 @@ export function connect<T>(
5354
}
5455
}
5556
}
56-
}
57+
}
58+
59+
60+
export function connect1<T>(
61+
mapStateToProps?: Function,
62+
mapDispatchToProps?: Function
63+
) {
64+
65+
return function connectWithComponent(WrappedComponent: React.ComponentType) {
66+
67+
return class CustomeComponent extends Component {
68+
69+
static contextTypes = {
70+
store: PropTypes.shape({
71+
subscribe: PropTypes.func.isRequired,
72+
dispatch: PropTypes.func.isRequired,
73+
getState: PropTypes.func.isRequired
74+
}).isRequired
75+
}
76+
77+
constructor(props: any, context: {store: ICreateStore}) {
78+
super(props)
79+
this.store = context.store
80+
this.state = mapStateToProps(this.store.getState())
81+
if(typeof mapDispatchToProps === 'function') {
82+
this.mappedDispatch = mapDispatchToProps(this.store.dispatch)
83+
}
84+
console.log('connect....constructor', this.state, this.mappedDispatch)
85+
}
86+
87+
state = {}
88+
89+
store = {} as ICreateStore
90+
mappedDispatch: any = {}
91+
unsub = () => {}
92+
93+
componentDidMount() {
94+
console.log('connect----componentDidMount', mapStateToProps(this.store.getState()))
95+
this.unsub = this.store.subscribe(() => {
96+
const mappedState = mapStateToProps(this.store.getState())
97+
console.log(mappedState, '----mappedState')
98+
this.setState(mappedState)
99+
})
100+
}
101+
102+
componentWillUnmount() {
103+
this.unsub()
104+
}
105+
106+
render() {
107+
return <WrappedComponent {...this.props} {...this.state} {...this.mappedDispatch}/>
108+
}
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)