Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#66): signals as refs #70

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions playground/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSignal, onCleanup, onMount } from 'solid-js';

export function App() {
const [count, setCount] = createSignal(0);
const [, setEl] = createSignal<HTMLElement>();

function increment() {
setCount(c => c + 1);
Expand All @@ -19,7 +20,7 @@ export function App() {

return (
<>
<h1>Count: {count()}</h1>
<h1 ref={setEl}>Count: {count()}</h1>
<button type="button" onClick={increment}>
Increment
</button>
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions src/babel/core/transform-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ function extractJSXExpressionFromRef(
let replacement: t.Expression;
if (unwrappedIdentifier) {
const arg = expr.scope.generateUidIdentifier('arg');
const binding = expr.scope.getBinding(unwrappedIdentifier.name);
const cannotAssignKind = ['const', 'module'];
const isConst = binding && cannotAssignKind.includes(binding.kind);

replacement = t.arrowFunctionExpression(
[arg],
t.blockStatement([
Expand All @@ -91,11 +95,17 @@ function extractJSXExpressionFromRef(
t.callExpression(unwrappedIdentifier, [arg]),
),
]),
t.blockStatement([
t.expressionStatement(
t.assignmentExpression('=', unwrappedIdentifier, arg),
),
]),
// fix the new usage of `ref` attribute,
// if use `Signals as refs`, the `else` branch will throw an error with `Cannot assign to "setter" because it is a constant` message
// issue: https://github.com/solidjs/solid-refresh/issues/66
// docs: https://docs.solidjs.com/concepts/refs#signals-as-refs
isConst
? null
: t.blockStatement([
t.expressionStatement(
t.assignmentExpression('=', unwrappedIdentifier, arg),
),
]),
),
]),
);
Expand Down
113 changes: 113 additions & 0 deletions tests/client-hydratable/__snapshots__/esm.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,119 @@ if (import.meta.hot) {
}"
`;

exports[`esm (client, hydratable) > fix build > refs > should work with a function 1`] = `
"import { template as _$template } from "solid-js/web";
import { createComponent as _$createComponent } from "solid-js/web";
import { getNextElement as _$getNextElement } from "solid-js/web";
import { use as _$use } from "solid-js/web";
var _tmpl$ = /*#__PURE__*/_$template(\`<div>Comp\`);
import { $$component as _$$component } from "solid-refresh";
import { $$refresh as _$$refresh } from "solid-refresh";
import { $$registry as _$$registry } from "solid-refresh";
const _REGISTRY = _$$registry();
const Comp_1 = _$$component(_REGISTRY, "Comp_1", _props => /*@refresh jsx-skip*/(() => {
var _el$ = _$getNextElement(_tmpl$);
var _ref$ = _props.v0;
typeof _ref$ === "function" ? _$use(_ref$, _el$) : _props.v0 = _el$;
return _el$;
})(), {
location: "example.jsx:4:19",
signature: "6f76290"
});
const Comp = _$$component(_REGISTRY, "Comp", () => {
let el;
return /*@refresh jsx-skip*/_$createComponent(Comp_1, {
v0: _el => el = _el
});
}, {
location: "example.jsx:2:23",
signature: "726057ca"
});
if (import.meta.hot) {
_$$refresh("esm", import.meta.hot, _REGISTRY);
}"
`;

exports[`esm (client, hydratable) > fix build > refs > should work with a mutable variable 1`] = `
"import { template as _$template } from "solid-js/web";
import { createComponent as _$createComponent } from "solid-js/web";
import { getNextElement as _$getNextElement } from "solid-js/web";
import { use as _$use } from "solid-js/web";
var _tmpl$ = /*#__PURE__*/_$template(\`<div>Comp\`);
import { $$component as _$$component } from "solid-refresh";
import { $$refresh as _$$refresh } from "solid-refresh";
import { $$registry as _$$registry } from "solid-refresh";
const _REGISTRY = _$$registry();
const Comp_1 = _$$component(_REGISTRY, "Comp_1", _props => /*@refresh jsx-skip*/(() => {
var _el$ = _$getNextElement(_tmpl$);
var _ref$ = _props.v0;
typeof _ref$ === "function" ? _$use(_ref$, _el$) : _props.v0 = _el$;
return _el$;
})(), {
location: "example.jsx:4:19",
signature: "6f76290"
});
const Comp = _$$component(_REGISTRY, "Comp", () => {
let el;
return /*@refresh jsx-skip*/_$createComponent(Comp_1, {
v0: _arg => {
if (typeof el === "function") {
el(_arg);
} else {
el = _arg;
}
}
});
}, {
location: "example.jsx:2:23",
signature: "d3db0f89"
});
if (import.meta.hot) {
_$$refresh("esm", import.meta.hot, _REGISTRY);
}"
`;

exports[`esm (client, hydratable) > fix build > signals as refs > should work 1`] = `
"import { template as _$template } from "solid-js/web";
import { createComponent as _$createComponent } from "solid-js/web";
import { getNextElement as _$getNextElement } from "solid-js/web";
import { use as _$use } from "solid-js/web";
var _tmpl$ = /*#__PURE__*/_$template(\`<div>Comp\`);
import { $$component as _$$component } from "solid-refresh";
import { $$refresh as _$$refresh } from "solid-refresh";
import { $$registry as _$$registry } from "solid-refresh";
import { createSignal } from 'solid-js';
const _REGISTRY = _$$registry();
const Comp_1 = _$$component(_REGISTRY, "Comp_1", _props => /*@refresh jsx-skip*/(() => {
var _el$ = _$getNextElement(_tmpl$);
var _ref$ = _props.v0;
typeof _ref$ === "function" ? _$use(_ref$, _el$) : _props.v0 = _el$;
return _el$;
})(), {
location: "example.jsx:6:19",
signature: "6f76290"
});
const Comp = _$$component(_REGISTRY, "Comp", () => {
const [el, setEl] = createSignal();
return /*@refresh jsx-skip*/_$createComponent(Comp_1, {
v0: _arg => {
if (typeof setEl === "function") {
setEl(_arg);
}
}
});
}, {
location: "example.jsx:4:23",
signature: "57db44c6",
dependencies: () => ({
createSignal
})
});
if (import.meta.hot) {
_$$refresh("esm", import.meta.hot, _REGISTRY);
}"
`;

exports[`esm (client, hydratable) > fix render > @refresh reload > should work 1`] = `
"import { createComponent as _$createComponent } from "solid-js/web";
import { $$decline as _$$decline } from "solid-refresh";
Expand Down
113 changes: 113 additions & 0 deletions tests/client-hydratable/__snapshots__/rspack-esm.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,119 @@ if (import.meta.webpackHot) {
}"
`;

exports[`rspack-esm (client, hydratable) > fix build > refs > should work with a function 1`] = `
"import { template as _$template } from "solid-js/web";
import { createComponent as _$createComponent } from "solid-js/web";
import { getNextElement as _$getNextElement } from "solid-js/web";
import { use as _$use } from "solid-js/web";
var _tmpl$ = /*#__PURE__*/_$template(\`<div>Comp\`);
import { $$component as _$$component } from "solid-refresh";
import { $$refresh as _$$refresh } from "solid-refresh";
import { $$registry as _$$registry } from "solid-refresh";
const _REGISTRY = _$$registry();
const Comp_1 = _$$component(_REGISTRY, "Comp_1", _props => /*@refresh jsx-skip*/(() => {
var _el$ = _$getNextElement(_tmpl$);
var _ref$ = _props.v0;
typeof _ref$ === "function" ? _$use(_ref$, _el$) : _props.v0 = _el$;
return _el$;
})(), {
location: "example.jsx:4:19",
signature: "6f76290"
});
const Comp = _$$component(_REGISTRY, "Comp", () => {
let el;
return /*@refresh jsx-skip*/_$createComponent(Comp_1, {
v0: _el => el = _el
});
}, {
location: "example.jsx:2:23",
signature: "726057ca"
});
if (import.meta.webpackHot) {
_$$refresh("rspack-esm", import.meta.webpackHot, _REGISTRY);
}"
`;

exports[`rspack-esm (client, hydratable) > fix build > refs > should work with a mutable variable 1`] = `
"import { template as _$template } from "solid-js/web";
import { createComponent as _$createComponent } from "solid-js/web";
import { getNextElement as _$getNextElement } from "solid-js/web";
import { use as _$use } from "solid-js/web";
var _tmpl$ = /*#__PURE__*/_$template(\`<div>Comp\`);
import { $$component as _$$component } from "solid-refresh";
import { $$refresh as _$$refresh } from "solid-refresh";
import { $$registry as _$$registry } from "solid-refresh";
const _REGISTRY = _$$registry();
const Comp_1 = _$$component(_REGISTRY, "Comp_1", _props => /*@refresh jsx-skip*/(() => {
var _el$ = _$getNextElement(_tmpl$);
var _ref$ = _props.v0;
typeof _ref$ === "function" ? _$use(_ref$, _el$) : _props.v0 = _el$;
return _el$;
})(), {
location: "example.jsx:4:19",
signature: "6f76290"
});
const Comp = _$$component(_REGISTRY, "Comp", () => {
let el;
return /*@refresh jsx-skip*/_$createComponent(Comp_1, {
v0: _arg => {
if (typeof el === "function") {
el(_arg);
} else {
el = _arg;
}
}
});
}, {
location: "example.jsx:2:23",
signature: "d3db0f89"
});
if (import.meta.webpackHot) {
_$$refresh("rspack-esm", import.meta.webpackHot, _REGISTRY);
}"
`;

exports[`rspack-esm (client, hydratable) > fix build > signals as refs > should work 1`] = `
"import { template as _$template } from "solid-js/web";
import { createComponent as _$createComponent } from "solid-js/web";
import { getNextElement as _$getNextElement } from "solid-js/web";
import { use as _$use } from "solid-js/web";
var _tmpl$ = /*#__PURE__*/_$template(\`<div>Comp\`);
import { $$component as _$$component } from "solid-refresh";
import { $$refresh as _$$refresh } from "solid-refresh";
import { $$registry as _$$registry } from "solid-refresh";
import { createSignal } from 'solid-js';
const _REGISTRY = _$$registry();
const Comp_1 = _$$component(_REGISTRY, "Comp_1", _props => /*@refresh jsx-skip*/(() => {
var _el$ = _$getNextElement(_tmpl$);
var _ref$ = _props.v0;
typeof _ref$ === "function" ? _$use(_ref$, _el$) : _props.v0 = _el$;
return _el$;
})(), {
location: "example.jsx:6:19",
signature: "6f76290"
});
const Comp = _$$component(_REGISTRY, "Comp", () => {
const [el, setEl] = createSignal();
return /*@refresh jsx-skip*/_$createComponent(Comp_1, {
v0: _arg => {
if (typeof setEl === "function") {
setEl(_arg);
}
}
});
}, {
location: "example.jsx:4:23",
signature: "57db44c6",
dependencies: () => ({
createSignal
})
});
if (import.meta.webpackHot) {
_$$refresh("rspack-esm", import.meta.webpackHot, _REGISTRY);
}"
`;

exports[`rspack-esm (client, hydratable) > fix render > @refresh reload > should work 1`] = `
"import { createComponent as _$createComponent } from "solid-js/web";
import { $$decline as _$$decline } from "solid-refresh";
Expand Down
Loading
Loading