Skip to content

Commit 695f372

Browse files
authoredSep 14, 2018
Compile away next/link proptypes in production (#5155)
This saves 7KB as prop-types-exact is not needed in production.
1 parent 5d147a8 commit 695f372

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed
 

‎build/webpack.js

+9
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i
245245
}),
246246
dev && !isServer && new FriendlyErrorsWebpackPlugin(),
247247
new webpack.IgnorePlugin(/(precomputed)/, /node_modules.+(elliptic)/),
248+
// This removes prop-types-exact in production, as it's not used there.
249+
!dev && new webpack.IgnorePlugin({
250+
checkResource: (resource) => {
251+
return /prop-types-exact/.test(resource)
252+
},
253+
checkContext: (context) => {
254+
return context.indexOf(NEXT_PROJECT_ROOT_DIST) !== -1
255+
}
256+
}),
248257
// Even though require.cache is server only we have to clear assets from both compilations
249258
// This is because the client compilation generates the build manifest that's used on the server side
250259
dev && new NextJsRequireCacheHotReloader(),

‎lib/error.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ export default class Error extends React.Component {
99
return { statusCode }
1010
}
1111

12-
static propTypes = {
13-
statusCode: PropTypes.number
14-
}
15-
1612
render () {
1713
const { statusCode } = this.props
1814
const title = statusCode === 404
@@ -35,6 +31,12 @@ export default class Error extends React.Component {
3531
}
3632
}
3733

34+
if (process.env.NODE_ENV === 'development') {
35+
Error.propTypes = {
36+
statusCode: PropTypes.number
37+
}
38+
}
39+
3840
const styles = {
3941
error: {
4042
color: '#000',

‎lib/link.js

+26-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { resolve, format, parse } from 'url'
44
import React, { Component, Children } from 'react'
55
import PropTypes from 'prop-types'
6-
import exact from 'prop-types-exact'
76
import Router, { _rewriteUrlForNextExport } from './router'
87
import { warn, execOnce, getLocationOrigin } from './utils'
98

@@ -35,28 +34,6 @@ function memoizedFormatUrl (formatUrl) {
3534
}
3635

3736
class Link extends Component {
38-
static propTypes = exact({
39-
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
40-
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
41-
prefetch: PropTypes.bool,
42-
replace: PropTypes.bool,
43-
shallow: PropTypes.bool,
44-
passHref: PropTypes.bool,
45-
scroll: PropTypes.bool,
46-
children: PropTypes.oneOfType([
47-
PropTypes.element,
48-
(props, propName) => {
49-
const value = props[propName]
50-
51-
if (typeof value === 'string') {
52-
warnLink(`Warning: You're using a string directly inside <Link>. This usage has been deprecated. Please add an <a> tag as child of <Link>`)
53-
}
54-
55-
return null
56-
}
57-
]).isRequired
58-
})
59-
6037
componentDidMount () {
6138
this.prefetch()
6239
}
@@ -177,4 +154,30 @@ class Link extends Component {
177154
}
178155
}
179156

157+
if (process.env.NODE_ENV === 'development') {
158+
// This module gets removed by webpack.IgnorePlugin
159+
const exact = require('prop-types-exact')
160+
Link.propTypes = exact({
161+
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
162+
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
163+
prefetch: PropTypes.bool,
164+
replace: PropTypes.bool,
165+
shallow: PropTypes.bool,
166+
passHref: PropTypes.bool,
167+
scroll: PropTypes.bool,
168+
children: PropTypes.oneOfType([
169+
PropTypes.element,
170+
(props, propName) => {
171+
const value = props[propName]
172+
173+
if (typeof value === 'string') {
174+
warnLink(`Warning: You're using a string directly inside <Link>. This usage has been deprecated. Please add an <a> tag as child of <Link>`)
175+
}
176+
177+
return null
178+
}
179+
]).isRequired
180+
})
181+
}
182+
180183
export default Link

0 commit comments

Comments
 (0)
Please sign in to comment.