1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Compile away next/link proptypes in production (#5155)

This saves 7KB as prop-types-exact is not needed in production.
This commit is contained in:
Tim Neutkens 2018-09-14 11:34:08 +02:00 committed by GitHub
parent 5d147a82c4
commit 695f372da9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 27 deletions

View file

@ -245,6 +245,15 @@ export default async function getBaseWebpackConfig (dir: string, {dev = false, i
}), }),
dev && !isServer && new FriendlyErrorsWebpackPlugin(), dev && !isServer && new FriendlyErrorsWebpackPlugin(),
new webpack.IgnorePlugin(/(precomputed)/, /node_modules.+(elliptic)/), 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 // 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 // This is because the client compilation generates the build manifest that's used on the server side
dev && new NextJsRequireCacheHotReloader(), dev && new NextJsRequireCacheHotReloader(),

View file

@ -9,10 +9,6 @@ export default class Error extends React.Component {
return { statusCode } return { statusCode }
} }
static propTypes = {
statusCode: PropTypes.number
}
render () { render () {
const { statusCode } = this.props const { statusCode } = this.props
const title = statusCode === 404 const title = statusCode === 404
@ -35,6 +31,12 @@ export default class Error extends React.Component {
} }
} }
if (process.env.NODE_ENV === 'development') {
Error.propTypes = {
statusCode: PropTypes.number
}
}
const styles = { const styles = {
error: { error: {
color: '#000', color: '#000',

View file

@ -3,7 +3,6 @@
import { resolve, format, parse } from 'url' import { resolve, format, parse } from 'url'
import React, { Component, Children } from 'react' import React, { Component, Children } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import exact from 'prop-types-exact'
import Router, { _rewriteUrlForNextExport } from './router' import Router, { _rewriteUrlForNextExport } from './router'
import { warn, execOnce, getLocationOrigin } from './utils' import { warn, execOnce, getLocationOrigin } from './utils'
@ -35,28 +34,6 @@ function memoizedFormatUrl (formatUrl) {
} }
class Link extends Component { 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 () { componentDidMount () {
this.prefetch() this.prefetch()
} }
@ -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 export default Link