Skip to content

Commit 7c3049c

Browse files
authored
fix(optimizer): don't drop _fnSignal string code in libs (#6732)
1 parent 2434aa2 commit 7c3049c

File tree

5 files changed

+174
-13
lines changed

5 files changed

+174
-13
lines changed

.changeset/funny-turtles-visit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik': patch
3+
---
4+
5+
Library builds now correctly generate \_fnSignal calls again. Any Qwik library that exports components should be built again.

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ pub fn transform_fs(config: TransformFsOptions) -> Result<TransformOutput, Error
154154
strip_ctx_name: config.strip_ctx_name.as_deref(),
155155
strip_event_handlers: config.strip_event_handlers,
156156
// If you don't specify is_server, the safe value is true
157-
is_server: config.is_server.unwrap_or(true),
157+
// For libraries, is_server has to be true because we neet to emit extra code
158+
is_server: config.mode == EmitMode::Lib || config.is_server.unwrap_or(true),
158159
})
159160
})
160161
.reduce(|| Ok(TransformOutput::new()), |x, y| Ok(x?.append(&mut y?)))?;
@@ -215,7 +216,8 @@ pub fn transform_modules(config: TransformModulesOptions) -> Result<TransformOut
215216
strip_ctx_name: config.strip_ctx_name.as_deref(),
216217
strip_event_handlers: config.strip_event_handlers,
217218
// If you don't specify is_server, the safe value is true
218-
is_server: config.is_server.unwrap_or(true),
219+
// For libraries, is_server has to be true because we neet to emit extra code
220+
is_server: config.mode == EmitMode::Lib || config.is_server.unwrap_or(true),
219221
})
220222
});
221223

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

+30-11
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export default component$(() => {
3838
============================= test.js ==
3939

4040
import { serverQrl } from "@builder.io/qwik-city";
41-
import { _noopQrl } from "@builder.io/qwik";
41+
import { _noopQrlDEV } from "@builder.io/qwik";
4242
import { componentQrl } from "@builder.io/qwik";
43-
import { qrl } from "@builder.io/qwik";
43+
import { qrlDEV } from "@builder.io/qwik";
4444
import { sideEffect } from './secret';
4545
(function() {
4646
console.log('run');
@@ -49,11 +49,21 @@ import { sideEffect } from './secret';
4949
console.log('run');
5050
})();
5151
sideEffect();
52-
export const api = serverQrl(/*#__PURE__*/ _noopQrl("api_server_JonPp043gH0"));
53-
export default /*#__PURE__*/ componentQrl(/*#__PURE__*/ qrl(()=>import("./test_component_luxexe0dqrg"), "test_component_LUXeXe0DQrg"));
54-
55-
56-
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;;;AAMA,SAAS,UAAU,QAAQ,WAAW;AAKrC,CAAA,WAAW;IACR,QAAQ,GAAG,CAAC;AACd,CAAA;AACC,CAAA,IAAM;IACL,QAAQ,GAAG,CAAC;AACd,CAAA;AAEF;AAEA,OAAO,MAAM,MAAM,4DAEhB;AAEH,6BAAe,0GAIV\"}")
52+
export const api = serverQrl(/*#__PURE__*/ _noopQrlDEV("api_server_JonPp043gH0", {
53+
file: "/user/qwik/src/test.tsx",
54+
lo: 0,
55+
hi: 0,
56+
displayName: "api_server"
57+
}));
58+
export default /*#__PURE__*/ componentQrl(/*#__PURE__*/ qrlDEV(()=>import("./test_component_luxexe0dqrg"), "test_component_LUXeXe0DQrg", {
59+
file: "/user/qwik/src/test.tsx",
60+
lo: 522,
61+
hi: 605,
62+
displayName: "test_component"
63+
}));
64+
65+
66+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;;;AAMA,SAAS,UAAU,QAAQ,WAAW;AAKrC,CAAA,WAAW;IACR,QAAQ,GAAG,CAAC;AACd,CAAA;AACC,CAAA,IAAM;IACL,QAAQ,GAAG,CAAC;AACd,CAAA;AAEF;AAEA,OAAO,MAAM,MAAM;;;;;IAEhB;AAEH,6BAAe;;;;;IAIV\"}")
5767
============================= test_component_button_onclick_dgk9xlyroka.js (ENTRY POINT)==
5868

5969
import { api } from "./test";
@@ -84,15 +94,24 @@ Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"ma
8494
============================= test_component_luxexe0dqrg.js (ENTRY POINT)==
8595

8696
import { _jsxQ } from "@builder.io/qwik";
87-
import { qrl } from "@builder.io/qwik";
97+
import { qrlDEV } from "@builder.io/qwik";
8898
export const test_component_LUXeXe0DQrg = ()=>{
8999
return /*#__PURE__*/ _jsxQ("button", null, {
90-
onClick$: /*#__PURE__*/ qrl(()=>import("./test_component_button_onclick_dgk9xlyroka"), "test_component_button_onClick_DGk9xLyRokA")
91-
}, null, 3, "u6_0");
100+
onClick$: /*#__PURE__*/ qrlDEV(()=>import("./test_component_button_onclick_dgk9xlyroka"), "test_component_button_onClick_DGk9xLyRokA", {
101+
file: "/user/qwik/src/test.tsx",
102+
lo: 567,
103+
hi: 584,
104+
displayName: "test_component_button_onClick"
105+
})
106+
}, null, 3, "u6_0", {
107+
fileName: "test.tsx",
108+
lineNumber: 27,
109+
columnNumber: 7
110+
});
92111
};
93112

94113

95-
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;0CAwB0B,IAAM;IAC5B,qBACE,MAAC;QAAO,QAAQ;;AAEpB\"}")
114+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;0CAwB0B,IAAM;IAC5B,qBACE,MAAC;QAAO,QAAQ;;;;;;;;;;;AAEpB\"}")
96115
/*
97116
{
98117
"origin": "test.tsx",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
source: packages/qwik/src/optimizer/core/src/test.rs
3+
assertion_line: 3540
4+
expression: output
5+
---
6+
==INPUT==
7+
8+
9+
import { component$ } from '@builder.io/qwik';
10+
export const Counter = component$(() => {
11+
const count = useSignal(0);
12+
13+
return (
14+
<div>
15+
<p>Count: {count.value}</p>
16+
<p>
17+
<button onClick$={() => count.value++}>Increment</button>
18+
</p>
19+
</div>
20+
);
21+
});
22+
23+
============================= counter_component_ztmrhll09gg.ts (ENTRY POINT)==
24+
25+
import { _fnSignal } from "@builder.io/qwik";
26+
import { _jsxQ } from "@builder.io/qwik";
27+
import { qrl } from "@builder.io/qwik";
28+
export const Counter_component_zTmRHlL09Gg = ()=>{
29+
const count = useSignal(0);
30+
return /*#__PURE__*/ _jsxQ("div", null, null, [
31+
/*#__PURE__*/ _jsxQ("p", null, null, [
32+
"Count: ",
33+
_fnSignal((p0)=>p0.value, [
34+
count
35+
], "p0.value")
36+
], 3, null),
37+
/*#__PURE__*/ _jsxQ("p", null, null, /*#__PURE__*/ _jsxQ("button", null, {
38+
onClick$: /*#__PURE__*/ qrl(()=>import("./counter_component_div_p_button_onclick_kqq00u8qaim"), "Counter_component_div_p_button_onClick_Kqq00U8qaIM", [
39+
count
40+
])
41+
}, "Increment", 3, null), 3, null)
42+
], 3, "u6_0");
43+
};
44+
45+
46+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;;6CAEkC,IAAM;IACtC,MAAM,QAAQ,UAAU;IAExB,qBACE,MAAC;sBACC,MAAC;YAAE;4BAAQ,GAAM,KAAK;;;;sBACtB,MAAC,+BACC,MAAC;YAAO,QAAQ;;;WAAuB;;AAI/C\"}")
47+
/*
48+
{
49+
"origin": "test.tsx",
50+
"name": "Counter_component_zTmRHlL09Gg",
51+
"entry": null,
52+
"displayName": "Counter_component",
53+
"hash": "zTmRHlL09Gg",
54+
"canonicalFilename": "counter_component_ztmrhll09gg",
55+
"path": "",
56+
"extension": "ts",
57+
"parent": null,
58+
"ctxKind": "function",
59+
"ctxName": "component$",
60+
"captures": false,
61+
"loc": [
62+
87,
63+
285
64+
]
65+
}
66+
*/
67+
============================= counter_component_div_p_button_onclick_kqq00u8qaim.ts (ENTRY POINT)==
68+
69+
import { useLexicalScope } from "@builder.io/qwik";
70+
export const Counter_component_div_p_button_onClick_Kqq00U8qaIM = ()=>{
71+
const [count] = useLexicalScope();
72+
return count.value++;
73+
};
74+
75+
76+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";kEAS0B;;WAAM,MAAM,KAAK\"}")
77+
/*
78+
{
79+
"origin": "test.tsx",
80+
"name": "Counter_component_div_p_button_onClick_Kqq00U8qaIM",
81+
"entry": null,
82+
"displayName": "Counter_component_div_p_button_onClick",
83+
"hash": "Kqq00U8qaIM",
84+
"canonicalFilename": "counter_component_div_p_button_onclick_kqq00u8qaim",
85+
"path": "",
86+
"extension": "ts",
87+
"parent": "Counter_component_zTmRHlL09Gg",
88+
"ctxKind": "eventHandler",
89+
"ctxName": "onClick$",
90+
"captures": true,
91+
"loc": [
92+
217,
93+
236
94+
]
95+
}
96+
*/
97+
============================= test.ts ==
98+
99+
import { componentQrl } from "@builder.io/qwik";
100+
import { qrl } from "@builder.io/qwik";
101+
export const Counter = /*#__PURE__*/ componentQrl(/*#__PURE__*/ qrl(()=>import("./counter_component_ztmrhll09gg"), "Counter_component_zTmRHlL09Gg"));
102+
103+
104+
Some("{\"version\":3,\"sources\":[\"/user/qwik/src/test.tsx\"],\"names\":[],\"mappings\":\";;AAEA,OAAO,MAAM,wBAAU,gHAWpB\"}")
105+
== DIAGNOSTICS ==
106+
107+
[]

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

+28
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ export default component$(() => {
744744
transpile_ts: true,
745745
transpile_jsx: true,
746746
is_server: Some(false),
747+
mode: EmitMode::Dev,
747748
..TestInput::default()
748749
});
749750
}
@@ -3535,6 +3536,33 @@ export const App = component$(() => {
35353536
});
35363537
}
35373538

3539+
#[test]
3540+
fn lib_mode_fn_signal() {
3541+
test_input!(TestInput {
3542+
code: r#"
3543+
import { component$ } from '@builder.io/qwik';
3544+
export const Counter = component$(() => {
3545+
const count = useSignal(0);
3546+
3547+
return (
3548+
<div>
3549+
<p>Count: {count.value}</p>
3550+
<p>
3551+
<button onClick$={() => count.value++}>Increment</button>
3552+
</p>
3553+
</div>
3554+
);
3555+
});
3556+
"#
3557+
.to_string(),
3558+
transpile_jsx: true,
3559+
mode: EmitMode::Lib,
3560+
// make sure it overrides it
3561+
is_server: Some(false),
3562+
..TestInput::default()
3563+
});
3564+
}
3565+
35383566
// TODO(misko): Make this test work by implementing strict serialization.
35393567
// #[test]
35403568
// fn example_of_synchronous_qrl_that_cant_be_serialized() {

0 commit comments

Comments
 (0)