Skip to content

Commit

Permalink
Move swcMinify out of experimental (vercel#29810)
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens authored Oct 25, 2021
1 parent b31b492 commit bc3ab3c
Show file tree
Hide file tree
Showing 106 changed files with 2,586 additions and 34 deletions.
4 changes: 4 additions & 0 deletions errors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@
{
"title": "swc-disabled",
"path": "/errors/swc-disabled.md"
},
{
"title": "swc-minify-enabled",
"path": "/errors/swc-minify-enabled.md"
}
]
}
Expand Down
7 changes: 7 additions & 0 deletions errors/swc-minify-enabled.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SWC minify enabled

#### Why This Message Occurred

The application has enabled `swcMinify` in `next.config.js`. By opting in minification will happen using the [SWC](https://swc.rs) minifier instead of Terser. This new minifier is 7x faster than Terser with comparable output. We're actively working on optimizing the output size and minification speed further.

If you have feedback about the minification, please provide it on [the feedback thread](https://github.com/vercel/next.js/discussions/30237).
8 changes: 4 additions & 4 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export default async function getBaseWebpackConfig(

const distDir = path.join(dir, config.distDir)

const useSWCLoader = config.experimental.swcLoader && !babelConfigFile
const useSWCLoader = !babelConfigFile
if (!loggedSwcDisabled && !useSWCLoader && babelConfigFile) {
Log.warn(
`Disabled SWC because of custom Babel configuration "${path.relative(
Expand Down Expand Up @@ -942,7 +942,7 @@ export default async function getBaseWebpackConfig(
new TerserPlugin({
cacheDir: path.join(distDir, 'cache', 'next-minifier'),
parallel: config.experimental.cpus,
swcMinify: config.experimental.swcMinify,
swcMinify: config.swcMinify,
terserOptions,
}).apply(compiler)
},
Expand Down Expand Up @@ -1411,8 +1411,8 @@ export default async function getBaseWebpackConfig(
hasRewrites,
reactRoot: config.experimental.reactRoot,
concurrentFeatures: config.experimental.concurrentFeatures,
swcMinify: config.experimental.swcMinify,
swcLoader: config.experimental.swcLoader,
swcMinify: config.swcMinify,
swcLoader: useSWCLoader,
})

const cache: any = {
Expand Down
5 changes: 2 additions & 3 deletions packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ export type NextConfig = { [key: string]: any } & {
}
staticPageGenerationTimeout?: number
crossOrigin?: false | 'anonymous' | 'use-credentials'
swcMinify?: boolean
experimental?: {
swcMinify?: boolean
swcLoader?: boolean
cpus?: number
sharedPool?: boolean
plugins?: boolean
Expand Down Expand Up @@ -202,9 +202,8 @@ export const defaultConfig: NextConfig = {
keepAlive: true,
},
staticPageGenerationTimeout: 60,
swcMinify: false,
experimental: {
swcLoader: true,
swcMinify: false,
cpus: Math.max(
1,
(Number(process.env.CIRCLE_NODE_TOTAL) ||
Expand Down
13 changes: 13 additions & 0 deletions packages/next/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ function assignDefaults(userConfig: { [key: string]: any }) {
)
}

if (result.experimental && 'swcMinify' in (result.experimental as any)) {
Log.warn(
`\`swcMinify\` has been moved out of \`experimental\`. Please update your ${configFileName} file accordingly.`
)
result.swcMinify = (result.experimental as any).swcMinify
}

if (result.swcMinify) {
Log.warn(
'SWC minify beta enabled. nextjs.org/docs/messages/swc-minify-enabled'
)
}

if (result.experimental && 'nftTracing' in (result.experimental as any)) {
// TODO: remove this warning and assignment when we leave experimental phase
Log.warn(
Expand Down
17 changes: 9 additions & 8 deletions test/.stats-app/stats-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ module.exports = {
content: `
module.exports = {
generateBuildId: () => 'BUILD_ID',
swcMinify: ${
// TODO: remove after stable release > 11.1.2
process.env.STATS_IS_RELEASE ? 'false' : 'true'
},
experimental: {
swcLoader: true,
swcMinify: ${
// TODO: remove after stable release > 11.1.2
process.env.STATS_IS_RELEASE ? 'false' : 'true'
},
},
webpack(config) {
config.optimization.minimize = false
Expand All @@ -160,12 +160,13 @@ module.exports = {
path: 'next.config.js',
content: `
module.exports = {
swcMinify: ${
// TODO: remove after stable release > 11.1.2
process.env.STATS_IS_RELEASE ? 'false' : 'true'
},
experimental: {
swcLoader: true,
swcMinify: ${
// TODO: remove after stable release > 11.1.2
process.env.STATS_IS_RELEASE ? 'false' : 'true'
},
},
generateBuildId: () => 'BUILD_ID'
}
Expand Down
3 changes: 3 additions & 0 deletions test/integration/async-modules/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["next/babel"]
}
3 changes: 0 additions & 3 deletions test/integration/async-modules/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ module.exports = {
config.experiments.topLevelAwait = true
return config
},
experimental: {
swcLoader: false,
},
}
3 changes: 3 additions & 0 deletions test/integration/no-anon-default-export/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["next/babel"]
}
5 changes: 0 additions & 5 deletions test/integration/no-anon-default-export/next.config.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import styles from './with-css-1.module.css'

export default () => <p className={styles.content}>With CSS 1</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.content {
color: inherit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import styles from './with-css-2.module.css'

export default () => <p className={styles.content}>With CSS 2</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.content {
color: inherit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import styles from './with-css-3.module.css'

export default () => <p className={styles.content}>With CSS 3</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.content {
color: inherit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.text {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styles from './with-css.module.css'
import styles2 from './with-css-2.module.css'

export default () => (
<div className={styles.content}>
<p className={styles2.text}>With CSS</p>
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.content {
color: inherit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import styles2 from './with-css-2.module.css'

export default () => <p className={styles2.text}>With CSS</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.text {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styles from './with-css.module.css'
import Nested from './Nested'

export default () => (
<div className={styles.content}>
<Nested />
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.content {
color: inherit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <p>Without CSS</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styles from './with-css-2.module.css'
import stylesShared from './with-css-shared.module.css'

export default () => (
<div className={styles.content}>
<p className={stylesShared.test}>With CSS</p>
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.content {
color: inherit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.text {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styles from './with-css.module.css'
import stylesShared from './with-css-shared.module.css'

export default () => (
<div className={styles.content}>
<p className={stylesShared.test}>With CSS</p>
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.content {
color: inherit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import styles from './with-css.module.css'

export default () => (
<p id="with-css" className={styles.content}>
With CSS
</p>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.content {
color: inherit;
display: flex;
}
13 changes: 13 additions & 0 deletions test/integration/production-swcminify/components/hello-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import PropTypes from 'prop-types'

export default class extends React.Component {
static contextTypes = {
data: PropTypes.object,
}

render() {
const { data } = this.context
return <div>{data.title}</div>
}
}
1 change: 1 addition & 0 deletions test/integration/production-swcminify/components/hello1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <p>Hello World 1</p>
1 change: 1 addition & 0 deletions test/integration/production-swcminify/components/hello2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <p>Hello World 2</p>
19 changes: 19 additions & 0 deletions test/integration/production-swcminify/components/logo/dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import styles from './logo.module.css'

export default function Logo() {
return <div className={styles.logo}></div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.logo {
background-image: url(dark.svg);
}
17 changes: 17 additions & 0 deletions test/integration/production-swcminify/components/welcome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'

export default class Welcome extends React.Component {
state = { name: null }

componentDidMount() {
const { name } = this.props
this.setState({ name })
}

render() {
const { name } = this.state
if (!name) return null

return <p>Welcome, {name}</p>
}
}
3 changes: 3 additions & 0 deletions test/integration/production-swcminify/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "cool-version"
}
46 changes: 46 additions & 0 deletions test/integration/production-swcminify/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// intervals/open connections shouldn't block build from exiting
setInterval(() => {}, 250)

module.exports = {
swcMinify: true,
experimental: {
outputFileTracing: true,
},
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
rewrites() {
// add a rewrite so the code isn't dead-code eliminated
return [
{
source: '/some-rewrite',
destination: '/',
},
]
},
redirects() {
return [
{
source: '/redirect/me/to-about/:lang',
destination: '/:lang/about',
permanent: false,
},
{
source: '/nonexistent',
destination: '/about',
permanent: false,
},
{
source: '/shadowed-page',
destination: '/about',
permanent: false,
},
{
source: '/redirect-query-test/:path',
destination: '/about?foo=:path',
permanent: false,
},
]
},
}
1 change: 1 addition & 0 deletions test/integration/production-swcminify/pages/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <div className="about-page">About Page</div>
2 changes: 2 additions & 0 deletions test/integration/production-swcminify/pages/amp-hybrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default () => 'hi'
export const config = { amp: 'hybrid' }
2 changes: 2 additions & 0 deletions test/integration/production-swcminify/pages/amp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default () => 'hi'
export const config = { amp: true }
10 changes: 10 additions & 0 deletions test/integration/production-swcminify/pages/another.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Link from 'next/link'

export default () => (
<div>
<Link href="/">
<a>Index Page</a>
</Link>
<p>Another</p>
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default ({ query }, res) => {
res.status(200).json(query)
}
3 changes: 3 additions & 0 deletions test/integration/production-swcminify/pages/api/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default (req, res) => {
res.end('API hello works')
}
Loading

0 comments on commit bc3ab3c

Please sign in to comment.