Skip to content

Commit 9e87449

Browse files
committed
wip
1 parent 9e5f68d commit 9e87449

6 files changed

+245
-34
lines changed

packages/qwik/src/optimizer/core/src/inlined_fn.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use swc_ecmascript::ast;
88
use swc_ecmascript::codegen::text_writer::JsWriter;
99
use swc_ecmascript::transforms::fixer;
1010
use swc_ecmascript::transforms::hygiene::hygiene_with_config;
11+
use swc_ecmascript::transforms::optimization;
1112
use swc_ecmascript::visit::{Visit, VisitWith};
1213
use swc_ecmascript::{
1314
utils::private_ident,
@@ -199,6 +200,10 @@ pub fn render_expr(expr: &ast::Expr) -> String {
199200
};
200201
expr.visit_mut_with(&mut hygiene_with_config(Default::default()));
201202
expr.visit_mut_with(&mut fixer(None));
203+
expr.visit_mut_with(&mut optimization::simplify::dce::dce(
204+
Default::default(),
205+
Default::default(),
206+
));
202207
emitter
203208
.emit_module(&ast::Module {
204209
span: DUMMY_SP,

packages/qwik/src/optimizer/core/src/snapshots/qwik_core__test__example_drop_side_effects.snap

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export default component$(() => {
3838
============================= test.js ==
3939

4040
import { serverQrl } from "@qwik.dev/router";
41+
import { qrlDEV } from "@qwik.dev/core";
4142
import { _noopQrlDEV } from "@qwik.dev/core";
4243
import { componentQrl } from "@qwik.dev/core";
43-
import { qrlDEV } from "@qwik.dev/core";
4444
const i_LUXeXe0DQrg = ()=>import("./test.tsx_test_component_LUXeXe0DQrg");
4545
import { sideEffect } from './secret';
4646
(function() {
@@ -92,6 +92,34 @@ Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"ma
9292
]
9393
}
9494
*/
95+
============================= test.tsx_api_server_JonPp043gH0.js (ENTRY POINT)==
96+
97+
export const api_server_JonPp043gH0 = ()=>{
98+
supabase.from('ffg').do(dfd);
99+
};
100+
101+
102+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\"sCAoB2B;IAC1B,SAAS,IAAI,CAAC,OAAO,EAAE,CAAC;AACzB\"}")
103+
/*
104+
{
105+
"origin": "test.tsx",
106+
"name": "api_server_JonPp043gH0",
107+
"entry": null,
108+
"displayName": "test.tsx_api_server",
109+
"hash": "JonPp043gH0",
110+
"canonicalFilename": "test.tsx_api_server_JonPp043gH0",
111+
"path": "",
112+
"extension": "js",
113+
"parent": null,
114+
"ctxKind": "function",
115+
"ctxName": "server$",
116+
"captures": false,
117+
"loc": [
118+
433,
119+
473
120+
]
121+
}
122+
*/
95123
============================= test.tsx_test_component_LUXeXe0DQrg.js (ENTRY POINT)==
96124

97125
import { _jsxSorted } from "@qwik.dev/core";

packages/qwik/src/optimizer/core/src/snapshots/qwik_core__test__example_noop_dev_mode.snap

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,39 @@ export const App = /*#__PURE__*/ componentQrl(/*#__PURE__*/ qrlDEV(i_ckEPmXZlub0
4444

4545

4646
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;;AAGA,OAAO,MAAM,oBAAM;;;;;IAoBhB\"}")
47+
============================= test.tsx_App_component_Cmp_p_shouldRemove_uU0MG0jvQD4.js (ENTRY POINT)==
48+
49+
import { useLexicalScope } from "@qwik.dev/core";
50+
export const App_component_Cmp_p_shouldRemove_uU0MG0jvQD4 = ()=>{
51+
const [stuff] = useLexicalScope();
52+
return stuff.count;
53+
};
54+
55+
56+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";4DAgBmB;;WAAM,MAAM,KAAK\"}")
57+
/*
58+
{
59+
"origin": "test.tsx",
60+
"name": "App_component_Cmp_p_shouldRemove_uU0MG0jvQD4",
61+
"entry": null,
62+
"displayName": "test.tsx_App_component_Cmp_p_shouldRemove",
63+
"hash": "uU0MG0jvQD4",
64+
"canonicalFilename": "test.tsx_App_component_Cmp_p_shouldRemove_uU0MG0jvQD4",
65+
"path": "",
66+
"extension": "js",
67+
"parent": "App_component_ckEPmXZlub0",
68+
"ctxKind": "eventHandler",
69+
"ctxName": "shouldRemove$",
70+
"captures": true,
71+
"loc": [
72+
350,
73+
367
74+
],
75+
"captureNames": [
76+
"stuff"
77+
]
78+
}
79+
*/
4780
============================= test.tsx_App_component_ckEPmXZlub0.js (ENTRY POINT)==
4881

4982
import { _jsxSorted } from "@qwik.dev/core";
@@ -115,6 +148,66 @@ Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"ma
115148
]
116149
}
117150
*/
151+
============================= test.tsx_App_component_serverStuff_ebyHaP15ytQ.js (ENTRY POINT)==
152+
153+
import { useLexicalScope } from "@qwik.dev/core";
154+
export const App_component_serverStuff_ebyHaP15ytQ = async ()=>{
155+
const [stuff] = useLexicalScope();
156+
// should be removed but keep scope
157+
console.log(stuff.count);
158+
};
159+
160+
161+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";qDAKc;;IACZ,mCAAmC;IACnC,QAAQ,GAAG,CAAC,MAAM,KAAK\"}")
162+
/*
163+
{
164+
"origin": "test.tsx",
165+
"name": "App_component_serverStuff_ebyHaP15ytQ",
166+
"entry": null,
167+
"displayName": "test.tsx_App_component_serverStuff",
168+
"hash": "ebyHaP15ytQ",
169+
"canonicalFilename": "test.tsx_App_component_serverStuff_ebyHaP15ytQ",
170+
"path": "",
171+
"extension": "js",
172+
"parent": "App_component_ckEPmXZlub0",
173+
"ctxKind": "function",
174+
"ctxName": "serverStuff$",
175+
"captures": true,
176+
"loc": [
177+
154,
178+
235
179+
],
180+
"captureNames": [
181+
"stuff"
182+
]
183+
}
184+
*/
185+
============================= test.tsx_App_component_Cmp_p_onClick_vuXzfUTkpto.js (ENTRY POINT)==
186+
187+
export const App_component_Cmp_p_onClick_vuXzfUTkpto = ()=>console.log('warn');
188+
189+
190+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\"uDAiBc,IAAM,QAAQ,GAAG,CAAC\"}")
191+
/*
192+
{
193+
"origin": "test.tsx",
194+
"name": "App_component_Cmp_p_onClick_vuXzfUTkpto",
195+
"entry": null,
196+
"displayName": "test.tsx_App_component_Cmp_p_onClick",
197+
"hash": "vuXzfUTkpto",
198+
"canonicalFilename": "test.tsx_App_component_Cmp_p_onClick_vuXzfUTkpto",
199+
"path": "",
200+
"extension": "js",
201+
"parent": "App_component_ckEPmXZlub0",
202+
"ctxKind": "eventHandler",
203+
"ctxName": "onClick$",
204+
"captures": false,
205+
"loc": [
206+
383,
207+
408
208+
]
209+
}
210+
*/
118211
== DIAGNOSTICS ==
119212

120213
[]

packages/qwik/src/optimizer/core/src/snapshots/qwik_core__test__example_strip_client_code.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ import "./keep";
5050
import "../keep2";
5151
import { componentQrl } from "@qwik.dev/core";
5252
import { useClientMountQrl } from "@qwik.dev/core";
53+
import { useLexicalScope } from "@qwik.dev/core";
54+
import { inlinedQrl } from "@qwik.dev/core";
5355
import { _noopQrl } from "@qwik.dev/core";
5456
import { useTaskQrl } from "@qwik.dev/core";
55-
import { inlinedQrl } from "@qwik.dev/core";
5657
import { _wrapProp } from "@qwik.dev/core";
57-
import { useLexicalScope } from "@qwik.dev/core";
5858
import { _jsxSorted } from "@qwik.dev/core";
5959
import { useStore } from '@qwik.dev/core';
6060
export const Parent = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl(()=>{

packages/qwik/src/optimizer/core/src/snapshots/qwik_core__test__example_strip_server_code.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,33 @@ Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"ma
149149
]
150150
}
151151
*/
152+
============================= test.tsx_Parent_component_serverLoader_k1L0DiPQV1I.js (ENTRY POINT)==
153+
154+
import { handler } from "serverless";
155+
export const s_k1L0DiPQV1I = handler;
156+
157+
158+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";6BA8Be\"}")
159+
/*
160+
{
161+
"origin": "test.tsx",
162+
"name": "s_k1L0DiPQV1I",
163+
"entry": null,
164+
"displayName": "test.tsx_Parent_component_serverLoader",
165+
"hash": "k1L0DiPQV1I",
166+
"canonicalFilename": "test.tsx_Parent_component_serverLoader_k1L0DiPQV1I",
167+
"path": "",
168+
"extension": "js",
169+
"parent": "s_0TaiDayHrlo",
170+
"ctxKind": "function",
171+
"ctxName": "serverLoader$",
172+
"captures": false,
173+
"loc": [
174+
676,
175+
683
176+
]
177+
}
178+
*/
152179
============================= test.tsx_Parent_component_0TaiDayHrlo.js (ENTRY POINT)==
153180

154181
import { _jsxSorted } from "@qwik.dev/core";
@@ -226,6 +253,44 @@ Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"ma
226253
]
227254
}
228255
*/
256+
============================= test.tsx_Parent_component_serverStuff_r1qAHX7Opp0.js (ENTRY POINT)==
257+
258+
import { clientQrl } from "@qwik.dev/core";
259+
import { qrl } from "@qwik.dev/core";
260+
const i_2ca3HLDC7yc = ()=>import("./test.tsx_Parent_component_serverStuff_a_2ca3HLDC7yc");
261+
const i_v9qawr2Inkk = ()=>import("./test.tsx_Parent_component_serverStuff_b_client_v9qawr2Inkk");
262+
export const s_r1qAHX7Opp0 = async ()=>{
263+
// should be removed too
264+
const a = /*#__PURE__*/ qrl(i_2ca3HLDC7yc, "s_2ca3HLDC7yc");
265+
const b = clientQrl(/*#__PURE__*/ qrl(i_v9qawr2Inkk, "s_v9qawr2Inkk"));
266+
return [
267+
a,
268+
b
269+
];
270+
};
271+
272+
273+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;;;6BAmBc;IACZ,wBAAwB;IACxB,MAAM;IAGN,MAAM,IAAI;IAGV,OAAO;QAAC;QAAE;KAAE;AACb\"}")
274+
/*
275+
{
276+
"origin": "test.tsx",
277+
"name": "s_r1qAHX7Opp0",
278+
"entry": null,
279+
"displayName": "test.tsx_Parent_component_serverStuff",
280+
"hash": "r1qAHX7Opp0",
281+
"canonicalFilename": "test.tsx_Parent_component_serverStuff_r1qAHX7Opp0",
282+
"path": "",
283+
"extension": "js",
284+
"parent": "s_0TaiDayHrlo",
285+
"ctxKind": "function",
286+
"ctxName": "serverStuff$",
287+
"captures": false,
288+
"loc": [
289+
483,
290+
658
291+
]
292+
}
293+
*/
229294
============================= test.tsx_Parent_component_useTask_1_P8oRQhHsurk.js (ENTRY POINT)==
230295

231296
export const s_P8oRQhHsurk = ()=>{

packages/qwik/src/optimizer/core/src/transform.rs

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -685,30 +685,9 @@ impl<'a> QwikTransform<'a> {
685685
hash,
686686
};
687687
let should_emit = self.should_emit_segment(&segment_data);
688-
if should_emit {
689-
for id in &segment_data.local_idents {
690-
if !self.options.global_collect.exports.contains_key(id) {
691-
if self.options.global_collect.root.contains_key(id) {
692-
self.ensure_export(id);
693-
}
694-
if invalid_decl.iter().any(|entry| entry.0 == *id) {
695-
HANDLER.with(|handler| {
696-
handler
697-
.struct_err_with_code(
698-
&format!(
699-
"Reference to identifier '{}' can not be used inside a Qrl($) scope because it's a function",
700-
id.0
701-
),
702-
errors::get_diagnostic_id(errors::Error::FunctionReference),
703-
)
704-
.emit();
705-
});
706-
}
707-
}
708-
}
709-
}
710-
if !should_emit {
711-
(self.create_noop_qrl(&symbol_name, segment_data), is_const)
688+
689+
let result = if is_empty_function(&folded) {
690+
self.create_noop_qrl(&symbol_name, segment_data.clone())
712691
} else if self.is_inline() {
713692
let folded = if !segment_data.scoped_idents.is_empty() {
714693
let new_local = self.ensure_core_import(&USE_LEXICAL_SCOPE);
@@ -728,15 +707,41 @@ impl<'a> QwikTransform<'a> {
728707
} else {
729708
folded
730709
};
731-
(
732-
self.create_inline_qrl(segment_data, folded, symbol_name, span),
733-
is_const,
734-
)
710+
self.create_inline_qrl(segment_data.clone(), folded, symbol_name.clone(), span)
735711
} else {
736-
(
737-
self.create_segment(segment_data, folded, symbol_name, span, segment_hash),
738-
is_const,
712+
self.create_segment(
713+
segment_data.clone(),
714+
folded,
715+
symbol_name.clone(),
716+
span,
717+
segment_hash,
739718
)
719+
};
720+
721+
if should_emit {
722+
for id in &segment_data.local_idents {
723+
if !self.options.global_collect.exports.contains_key(id) {
724+
if self.options.global_collect.root.contains_key(id) {
725+
self.ensure_export(id);
726+
}
727+
if invalid_decl.iter().any(|entry| entry.0 == *id) {
728+
HANDLER.with(|handler| {
729+
handler
730+
.struct_err_with_code(
731+
&format!(
732+
"Reference to identifier '{}' can not be used inside a Qrl($) scope because it's a function",
733+
id.0
734+
),
735+
errors::get_diagnostic_id(errors::Error::FunctionReference),
736+
)
737+
.emit();
738+
});
739+
}
740+
}
741+
}
742+
(result, is_const)
743+
} else {
744+
(self.create_noop_qrl(&symbol_name, segment_data), is_const)
740745
}
741746
}
742747

@@ -2737,6 +2742,21 @@ fn is_text_only(node: &str) -> bool {
27372742
)
27382743
}
27392744

2745+
fn is_empty_function(expr: &ast::Expr) -> bool {
2746+
match expr {
2747+
ast::Expr::Arrow(arrow) => match &*arrow.body {
2748+
ast::BlockStmtOrExpr::BlockStmt(block) => block.stmts.is_empty(),
2749+
ast::BlockStmtOrExpr::Expr(_) => false,
2750+
},
2751+
ast::Expr::Fn(fn_expr) => fn_expr
2752+
.function
2753+
.body
2754+
.as_ref()
2755+
.map_or(true, |body| body.stmts.is_empty()),
2756+
_ => false,
2757+
}
2758+
}
2759+
27402760
fn process_node_props(pat: &ast::Pat) -> Vec<IdPlusType> {
27412761
let mut identifiers = vec![];
27422762
let mut processed_scope_data: Vec<IdPlusType> = vec![];

0 commit comments

Comments
 (0)