From 70b87c3b03154bf0d42e213bc98c5b44ff228292 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 16 Sep 2024 12:54:34 +0200 Subject: [PATCH 1/3] Fix typo of macro --- crates/napi/src/minify.rs | 2 +- .../src/transforms/optimize_server_react.rs | 6 +++--- .../tests/fixture/optimize_server_react/1/output.js | 6 +++--- .../tests/fixture/optimize_server_react/2/output.js | 4 ++-- .../tests/fixture/optimize_server_react/4/output.js | 2 +- .../tests/fixture/optimize_server_react/6/output.js | 2 +- .../tests/fixture/optimize_server_react/7/output.js | 4 ++-- .../webpack/plugins/minify-webpack-plugin/src/index.ts | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/napi/src/minify.rs b/crates/napi/src/minify.rs index 4856c7d6f942f..f4845c1d84b43 100644 --- a/crates/napi/src/minify.rs +++ b/crates/napi/src/minify.rs @@ -111,7 +111,7 @@ fn patch_opts(opts: &mut JsMinifyOptions) { opts.compress = BoolOrDataConfig::from_obj(TerserCompressorOptions { inline: Some(TerserInlineOption::Num(2)), global_defs: [( - "process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE".into(), + "process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE".into(), "production".into(), )] .iter() diff --git a/crates/next-custom-transforms/src/transforms/optimize_server_react.rs b/crates/next-custom-transforms/src/transforms/optimize_server_react.rs index 64590d3631974..80ed0b68f8b1a 100644 --- a/crates/next-custom-transforms/src/transforms/optimize_server_react.rs +++ b/crates/next-custom-transforms/src/transforms/optimize_server_react.rs @@ -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_MARCO_FALSE` && . - // And `process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE`` will be treated as `false` in + // turn it into `process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE` && . + // 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 { @@ -83,7 +83,7 @@ fn wrap_expr_with_env_prod_condition(call: CallExpr) -> Expr { span: DUMMY_SP, }))), prop: (MemberProp::Ident(IdentName { - sym: "__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE".into(), + sym: "__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE".into(), span: DUMMY_SP, })), span: DUMMY_SP, diff --git a/crates/next-custom-transforms/tests/fixture/optimize_server_react/1/output.js b/crates/next-custom-transforms/tests/fixture/optimize_server_react/1/output.js index 9e46707814434..11df359cceb27 100644 --- a/crates/next-custom-transforms/tests/fixture/optimize_server_react/1/output.js +++ b/crates/next-custom-transforms/tests/fixture/optimize_server_react/1/output.js @@ -1,10 +1,10 @@ import { useEffect, useLayoutEffect, useMemo } from 'react'; import React from 'react'; export default function App() { - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && useEffect(()=>{ + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && useEffect(()=>{ console.log('Hello World'); }, []); - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && useLayoutEffect(()=>{ + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && useLayoutEffect(()=>{ function foo() {} return ()=>{}; }, [ @@ -24,7 +24,7 @@ export default function App() { const a = useMemo(()=>{ return 1; }, []); - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && React.useEffect(()=>{ + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && React.useEffect(()=>{ console.log('Hello World'); }); return
diff --git a/crates/next-custom-transforms/tests/fixture/optimize_server_react/2/output.js b/crates/next-custom-transforms/tests/fixture/optimize_server_react/2/output.js index a45bed4f4b8e5..3593e7f866d69 100644 --- a/crates/next-custom-transforms/tests/fixture/optimize_server_react/2/output.js +++ b/crates/next-custom-transforms/tests/fixture/optimize_server_react/2/output.js @@ -16,7 +16,7 @@ export default function FilterItemDropdown({ list }) { ()=>null ]; const ref = useRef(null); - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && useEffect(()=>{ + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && useEffect(()=>{ const handleClickOutside = (event)=>{ if (ref.current && !ref.current.contains(event.target)) { setOpenSelect(false); @@ -25,7 +25,7 @@ export default function FilterItemDropdown({ list }) { window.addEventListener('click', handleClickOutside); return ()=>window.removeEventListener('click', handleClickOutside); }, []); - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && useEffect(()=>{ + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && useEffect(()=>{ list.forEach((listItem)=>{ if ('path' in listItem && pathname === listItem.path || 'slug' in listItem && searchParams.get('sort') === listItem.slug) { setActive(listItem.title); diff --git a/crates/next-custom-transforms/tests/fixture/optimize_server_react/4/output.js b/crates/next-custom-transforms/tests/fixture/optimize_server_react/4/output.js index 835aae43cfbab..a46a22bda4cbd 100644 --- a/crates/next-custom-transforms/tests/fixture/optimize_server_react/4/output.js +++ b/crates/next-custom-transforms/tests/fixture/optimize_server_react/4/output.js @@ -5,7 +5,7 @@ export default function App() { useEffect(()=>{ console.log('Hello World'); }, []); - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && useLayoutEffect(()=>{ + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && useLayoutEffect(()=>{ function foo() {} return ()=>{}; }, [ diff --git a/crates/next-custom-transforms/tests/fixture/optimize_server_react/6/output.js b/crates/next-custom-transforms/tests/fixture/optimize_server_react/6/output.js index 0d05a9dd34bd0..24149bef2a20f 100644 --- a/crates/next-custom-transforms/tests/fixture/optimize_server_react/6/output.js +++ b/crates/next-custom-transforms/tests/fixture/optimize_server_react/6/output.js @@ -5,7 +5,7 @@ const Component = ({ children, fallback })=>{ false, ()=>null ]; - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && useEffect(()=>setMounted(true), []); + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && useEffect(()=>setMounted(true), []); if (!mounted) { return fallback ?? /* @__PURE__ */ jsx(Fragment, {}); } diff --git a/crates/next-custom-transforms/tests/fixture/optimize_server_react/7/output.js b/crates/next-custom-transforms/tests/fixture/optimize_server_react/7/output.js index 372b80ad7fbdd..161635a819760 100644 --- a/crates/next-custom-transforms/tests/fixture/optimize_server_react/7/output.js +++ b/crates/next-custom-transforms/tests/fixture/optimize_server_react/7/output.js @@ -1,10 +1,10 @@ import { useEffect, useLayoutEffect, useMemo } from 'react'; import * as React from 'react'; export default function App() { - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && useEffect(()=>{ + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && useEffect(()=>{ console.log('Hello World'); }, []); - process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE && useLayoutEffect(()=>{ + process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && useLayoutEffect(()=>{ function foo() {} return ()=>{}; }, [ diff --git a/packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts b/packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts index 8c731879a323b..b69a43d213b12 100644 --- a/packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts +++ b/packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts @@ -111,7 +111,7 @@ export class MinifyPlugin { : {}), compress: { global_defs: { - 'process.env.__NEXT_PRIVATE_MINIMIZE_MARCO_FALSE': false, + 'process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE': false, }, }, mangle: true, From 10de13c7680eccaa4f3f192a8456b29ba07b6008 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 16 Sep 2024 15:30:59 +0200 Subject: [PATCH 2/3] only use minify.rs to replace cond --- crates/napi/src/minify.rs | 2 +- .../src/transforms/optimize_server_react.rs | 5 ++--- .../minify-webpack-plugin/src/index.ts | 6 +----- .../optimize-server-react.test.ts | 19 +++++++++++++++++++ .../optimize-server-react/pages/index.tsx | 13 +++++++++++++ 5 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 test/production/optimize-server-react/optimize-server-react.test.ts create mode 100644 test/production/optimize-server-react/pages/index.tsx diff --git a/crates/napi/src/minify.rs b/crates/napi/src/minify.rs index f4845c1d84b43..246fe65b32065 100644 --- a/crates/napi/src/minify.rs +++ b/crates/napi/src/minify.rs @@ -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() diff --git a/crates/next-custom-transforms/src/transforms/optimize_server_react.rs b/crates/next-custom-transforms/src/transforms/optimize_server_react.rs index 80ed0b68f8b1a..7209e6d5ea9f9 100644 --- a/crates/next-custom-transforms/src/transforms/optimize_server_react.rs +++ b/crates/next-custom-transforms/src/transforms/optimize_server_react.rs @@ -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` && . - // And `process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE`` will be treated as `false` in + // turn it into `process.env.__NEXT_PRIVATE_MINIMIZE_MACRO_FALSE && `. + // 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 { @@ -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, diff --git a/packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts b/packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts index b69a43d213b12..0973145ba271b 100644 --- a/packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts +++ b/packages/next/src/build/webpack/plugins/minify-webpack-plugin/src/index.ts @@ -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: { diff --git a/test/production/optimize-server-react/optimize-server-react.test.ts b/test/production/optimize-server-react/optimize-server-react.test.ts new file mode 100644 index 0000000000000..f59792e0889a6 --- /dev/null +++ b/test/production/optimize-server-react/optimize-server-react.test.ts @@ -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') + }) + } +) diff --git a/test/production/optimize-server-react/pages/index.tsx b/test/production/optimize-server-react/pages/index.tsx new file mode 100644 index 0000000000000..8eccd43edf4ed --- /dev/null +++ b/test/production/optimize-server-react/pages/index.tsx @@ -0,0 +1,13 @@ +import { useEffect } from 'react' + +export default function Page() { + useEffect(() => { + console.log('use-effect-call-log') + }, []) + return

hello world

+} + +// Mark as dynamic +export function getServerSideProps() { + return { props: {} } +} From a3ff7a9eaef224fa24641c8a7ff86261f47c11d7 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 16 Sep 2024 15:48:04 +0200 Subject: [PATCH 3/3] boolean --- crates/napi/src/minify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/napi/src/minify.rs b/crates/napi/src/minify.rs index 246fe65b32065..9fd04e41576d1 100644 --- a/crates/napi/src/minify.rs +++ b/crates/napi/src/minify.rs @@ -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(), - "false".into(), + false.into(), )] .iter() .cloned()