Skip to content

Commit

Permalink
only use minify.rs to replace cond
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 16, 2024
1 parent 70b87c3 commit 10de13c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/napi/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn patch_opts(opts: &mut JsMinifyOptions) {
inline: Some(TerserInlineOption::Num(2)),
global_defs: [(
"process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE".into(),
"production".into(),
"false".into(),
)]
.iter()
.cloned()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ fn effect_has_side_effect_deps(call: &CallExpr) -> bool {

fn wrap_expr_with_env_prod_condition(call: CallExpr) -> Expr {
// Wrap the call expression with the condition
// turn it into `process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE` && <call>.
// And `process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE`` will be treated as `false` in
// turn it into `process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && <call>`.
// And `process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE` will be treated as `false` in
// minification. In this way the expression and dependencies are still available in
// compilation during bundling, but will be removed in the final DEC.
Expr::Bin(BinExpr {
Expand All @@ -75,7 +75,6 @@ fn wrap_expr_with_env_prod_condition(call: CallExpr) -> Expr {
span: DUMMY_SP,
..Default::default()
}))),

prop: MemberProp::Ident(IdentName {
sym: "env".into(),
span: DUMMY_SP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ export class MinifyPlugin {
},
}
: {}),
compress: {
global_defs: {
'process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE': false,
},
},
compress: true,
mangle: true,
module: 'unknown',
output: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { nextTestSetup } from 'e2e-utils'
;(process.env.TURBOPACK ? describe.skip : describe)(
'optimize-server-react',
() => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should work with useEffect', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('hello world')
})

it('should optimize useEffect call on server side', async () => {
const file = await next.readFile('.next/server/pages/index.js')
expect(file).not.toContain('useEffect')
})
}
)
13 changes: 13 additions & 0 deletions test/production/optimize-server-react/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useEffect } from 'react'

export default function Page() {
useEffect(() => {
console.log('use-effect-call-log')
}, [])
return <p>hello world</p>
}

// Mark as dynamic
export function getServerSideProps() {
return { props: {} }
}

0 comments on commit 10de13c

Please sign in to comment.