Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile away next/link proptypes in production #5155

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i
}),
dev && !isServer && new FriendlyErrorsWebpackPlugin(),
new webpack.IgnorePlugin(/(precomputed)/, /node_modules.+(elliptic)/),
// This removes prop-types-exact in production, as it's not used there.
!dev && new webpack.IgnorePlugin({
checkResource: (resource) => {
return /prop-types-exact/.test(resource)
},
checkContext: (context) => {
return context.indexOf(NEXT_PROJECT_ROOT_DIST) !== -1
}
}),
// Even though require.cache is server only we have to clear assets from both compilations
// This is because the client compilation generates the build manifest that's used on the server side
dev && new NextJsRequireCacheHotReloader(),
Expand Down
10 changes: 6 additions & 4 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export default class Error extends React.Component {
return { statusCode }
}

static propTypes = {
statusCode: PropTypes.number
}

render () {
const { statusCode } = this.props
const title = statusCode === 404
Expand All @@ -35,6 +31,12 @@ export default class Error extends React.Component {
}
}

if (process.env.NODE_ENV === 'development') {
Error.propTypes = {
statusCode: PropTypes.number
}
}

const styles = {
error: {
color: '#000',
Expand Down
49 changes: 26 additions & 23 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { resolve, format, parse } from 'url'
import React, { Component, Children } from 'react'
import PropTypes from 'prop-types'
import exact from 'prop-types-exact'
import Router, { _rewriteUrlForNextExport } from './router'
import { warn, execOnce, getLocationOrigin } from './utils'

Expand Down Expand Up @@ -35,28 +34,6 @@ function memoizedFormatUrl (formatUrl) {
}

class Link extends Component {
static propTypes = exact({
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
prefetch: PropTypes.bool,
replace: PropTypes.bool,
shallow: PropTypes.bool,
passHref: PropTypes.bool,
scroll: PropTypes.bool,
children: PropTypes.oneOfType([
PropTypes.element,
(props, propName) => {
const value = props[propName]

if (typeof value === 'string') {
warnLink(`Warning: You're using a string directly inside <Link>. This usage has been deprecated. Please add an <a> tag as child of <Link>`)
}

return null
}
]).isRequired
})

componentDidMount () {
this.prefetch()
}
Expand Down Expand Up @@ -177,4 +154,30 @@ class Link extends Component {
}
}

if (process.env.NODE_ENV === 'development') {
// This module gets removed by webpack.IgnorePlugin
const exact = require('prop-types-exact')
Link.propTypes = exact({
href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
prefetch: PropTypes.bool,
replace: PropTypes.bool,
shallow: PropTypes.bool,
passHref: PropTypes.bool,
scroll: PropTypes.bool,
children: PropTypes.oneOfType([
PropTypes.element,
(props, propName) => {
const value = props[propName]

if (typeof value === 'string') {
warnLink(`Warning: You're using a string directly inside <Link>. This usage has been deprecated. Please add an <a> tag as child of <Link>`)
}

return null
}
]).isRequired
})
}

export default Link