Skip to content

Commit 1defacb

Browse files
authored
Fix compilation (#52)
* Update biome.json * Add `babel-preset-solid` for testing * Fix `solid-refresh` to transform ahead of the preset * Update index.ts * Fix dependencies for `granular` mode * Fix testing
1 parent 35bf8e4 commit 1defacb

15 files changed

Lines changed: 24264 additions & 1747 deletions

biome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"noArguments": "error",
117117
"noCommaOperator": "off",
118118
"noDefaultExport": "off",
119-
"noImplicitBoolean": "error",
119+
"noImplicitBoolean": "off",
120120
"noInferrableTypes": "error",
121121
"noNamespace": "error",
122122
"noNegationElse": "error",

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@
2929
},
3030
"typesVersions": {
3131
"*": {
32-
"babel": ["./dist/src/babel/index.d.ts"]
32+
"babel": [
33+
"./dist/src/babel/index.d.ts"
34+
]
3335
}
3436
},
35-
"files": ["dist"],
37+
"files": [
38+
"dist"
39+
],
3640
"publishConfig": {
3741
"access": "public"
3842
},
39-
"contributors": ["Alexis Munsayac"],
43+
"contributors": [
44+
"Alexis Munsayac"
45+
],
4046
"sideEffects": false,
4147
"scripts": {
4248
"build": "rollup -c",
@@ -54,6 +60,7 @@
5460
"@types/babel__helper-module-imports": "^7.18.3",
5561
"@types/babel__traverse": "^7.20.4",
5662
"@types/node": "^20.10.5",
63+
"babel-preset-solid": "^1.8.9",
5764
"rollup": "^4.9.1",
5865
"solid-js": "^1.8.7",
5966
"tslib": "^2.6.2",

pnpm-lock.yaml

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/babel/index.ts

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ function wrapComponent(
541541
properties.push(
542542
t.objectProperty(
543543
t.identifier('dependencies'),
544-
t.objectExpression(dependencyKeys),
544+
t.arrowFunctionExpression([], t.objectExpression(dependencyKeys)),
545545
),
546546
);
547547
}
@@ -587,8 +587,7 @@ function setupProgram(state: State, path: babel.NodePath<t.Program>): void {
587587
let shouldSkip = false;
588588
const comments = state.file.ast.comments;
589589
if (comments) {
590-
for (let i = 0; i < comments.length; i++) {
591-
const comment = comments[i].value;
590+
for (const { value: comment } of comments) {
592591
if (/^\s*@refresh granular\s*$/.test(comment)) {
593592
state.granular = true;
594593
break;
@@ -712,59 +711,62 @@ export default function solidRefreshPlugin(): babel.PluginObj<State> {
712711
this.imports = [...IMPORT_IDENTITIES, ...(this.opts.imports || [])];
713712
},
714713
visitor: {
715-
Program(path, state) {
716-
setupProgram(state, path);
717-
},
718-
ExportNamedDeclaration(path, state) {
719-
transformExportNamedDeclaration(state, path);
720-
},
721-
VariableDeclarator(path, state) {
722-
transformVariableDeclarator(state, path);
723-
},
724-
FunctionDeclaration(path, state) {
725-
if (state.processed) {
726-
return;
727-
}
728-
if (
729-
path.parentPath.isProgram() ||
730-
path.parentPath.isExportDefaultDeclaration()
731-
) {
732-
const decl = path.node;
733-
// Check if declaration is FunctionDeclaration
734-
if (
735-
// Check if the declaration has an identifier, and then check
736-
decl.id &&
737-
// if the name is component-ish
738-
isComponentishName(decl.id.name) &&
739-
!(decl.generator || decl.async) &&
740-
// Might be component-like, but the only valid components
741-
// have zero or one parameter
742-
decl.params.length < 2
743-
) {
744-
const replacement = wrapComponent(
745-
state,
746-
path,
747-
decl.id,
748-
t.functionExpression(decl.id, decl.params, decl.body),
749-
decl,
750-
);
751-
const newDecl = t.variableDeclaration('var', [
752-
t.variableDeclarator(decl.id, replacement),
753-
]);
754-
if (path.parentPath.isExportDefaultDeclaration()) {
755-
const parent = path.parentPath
756-
.parentPath as babel.NodePath<t.Program>;
757-
const first = parent.get('body')[0];
758-
first.insertBefore(newDecl);
759-
path.replaceWith(decl.id);
760-
} else {
761-
const parent = path.parentPath as babel.NodePath<t.Program>;
762-
const first = parent.get('body')[0];
763-
first.insertBefore(newDecl);
764-
path.remove();
714+
Program(programPath, state) {
715+
setupProgram(state, programPath);
716+
717+
programPath.traverse({
718+
ExportNamedDeclaration(path) {
719+
transformExportNamedDeclaration(state, path);
720+
},
721+
VariableDeclarator(path) {
722+
transformVariableDeclarator(state, path);
723+
},
724+
FunctionDeclaration(path) {
725+
if (state.processed) {
726+
return;
765727
}
766-
}
767-
}
728+
if (
729+
path.parentPath.isProgram() ||
730+
path.parentPath.isExportDefaultDeclaration()
731+
) {
732+
const decl = path.node;
733+
// Check if declaration is FunctionDeclaration
734+
if (
735+
// Check if the declaration has an identifier, and then check
736+
decl.id &&
737+
// if the name is component-ish
738+
isComponentishName(decl.id.name) &&
739+
!(decl.generator || decl.async) &&
740+
// Might be component-like, but the only valid components
741+
// have zero or one parameter
742+
decl.params.length < 2
743+
) {
744+
const replacement = wrapComponent(
745+
state,
746+
path,
747+
decl.id,
748+
t.functionExpression(decl.id, decl.params, decl.body),
749+
decl,
750+
);
751+
const newDecl = t.variableDeclaration('var', [
752+
t.variableDeclarator(decl.id, replacement),
753+
]);
754+
if (path.parentPath.isExportDefaultDeclaration()) {
755+
const parent = path.parentPath
756+
.parentPath as babel.NodePath<t.Program>;
757+
const first = parent.get('body')[0];
758+
first.insertBefore(newDecl);
759+
path.replaceWith(decl.id);
760+
} else {
761+
const parent = path.parentPath as babel.NodePath<t.Program>;
762+
const first = parent.get('body')[0];
763+
first.insertBefore(newDecl);
764+
path.remove();
765+
}
766+
}
767+
}
768+
},
769+
});
768770
},
769771
},
770772
};

src/runtime/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface ComponentOptions {
1010
// generated from the component's JS string
1111
signature?: string;
1212
// An array of foreign bindings (values that aren't locally declared in the component)
13-
dependencies?: Record<string, any>;
13+
dependencies?: () => Record<string, any>;
1414
}
1515

1616
// The registration data for the components
@@ -88,12 +88,14 @@ function patchComponent<P>(
8888
// Check if incoming module has signature
8989
if (newData.signature) {
9090
// Compare signatures
91+
const oldDeps = oldData.dependencies?.();
92+
const newDeps = newData.dependencies?.();
9193
if (
9294
newData.signature !== oldData.signature ||
93-
isListUpdated(newData.dependencies, oldData.dependencies)
95+
isListUpdated(newDeps, oldDeps)
9496
) {
9597
// Replace signatures and dependencies
96-
oldData.dependencies = newData.dependencies;
98+
oldData.dependencies = newDeps ? () => newDeps : undefined;
9799
oldData.signature = newData.signature;
98100
// Remount
99101
oldData.update(() => newData.component);

0 commit comments

Comments
 (0)