Skip to content

Commit 8a38876

Browse files
ijjklubieowoce
authored andcommitted
Ensure we match comment minify behavior between terser and swc (#68372)
Currently we are only strip all comments properly when `swcMinify: false` is set although the default is to use `swcMinify` which can cause confusion with these two modes not matching comments handling. This updates the default to match comment stripping between the two and also adds an experimental config to allow toggling this regardless of which minifier is being used. x-ref: [slack thread](https://vercel.slack.com/archives/C0676QZBWKS/p1722444791037279) x-ref: NEXT-3642
1 parent 5757843 commit 8a38876

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

packages/next/src/build/webpack/plugins/terser-webpack-plugin/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ export class TerserPlugin {
158158
compress: true,
159159
mangle: true,
160160
module: 'unknown',
161+
output: {
162+
comments: false,
163+
},
161164
}
162165
)
163166

test/production/terser-class-static-blocks/pages/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* My JSDoc comment that should be minified away
3+
*
4+
* @author Example One <[email protected]>
5+
* @author Example Two <[email protected]>
6+
* @copyright 2024 Vercel Inc
7+
*/
18
class TestClass {
29
static text = 'hello world'
310
}
Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
1-
import { createNextDescribe } from 'e2e-utils'
1+
import glob from 'glob'
2+
import { nextTestSetup } from 'e2e-utils'
3+
import path from 'path'
24

3-
createNextDescribe(
4-
'terser-class-static-blocks',
5-
{
5+
describe('terser-class-static-blocks', () => {
6+
const { next, isNextDeploy } = nextTestSetup({
67
files: __dirname,
7-
nextConfig: {
8-
swcMinify: false,
9-
},
10-
},
11-
({ next }) => {
12-
it('should work using cheerio', async () => {
13-
const $ = await next.render$('/')
14-
expect($('p').text()).toBe('hello world')
8+
nextConfig: {},
9+
})
10+
11+
it('should work using cheerio', async () => {
12+
const $ = await next.render$('/')
13+
expect($('p').text()).toBe('hello world')
14+
})
15+
16+
if (!isNextDeploy) {
17+
it('should have stripped away all comments', async () => {
18+
const chunksDir = path.join(next.testDir, '.next/static')
19+
const chunks = glob.sync('**/*.js', {
20+
cwd: chunksDir,
21+
})
22+
23+
expect(chunks.length).toBeGreaterThan(0)
24+
25+
await Promise.all(
26+
chunks.map(async (chunk) => {
27+
expect(
28+
await next.readFile(path.join('.next/static', chunk))
29+
).not.toContain('/*')
30+
31+
expect(
32+
await next.readFile(path.join('.next/static', chunk))
33+
).not.toContain('My JSDoc comment that')
34+
})
35+
)
1536
})
1637
}
17-
)
38+
})

0 commit comments

Comments
 (0)