From fb3903a5bce826a8c61f4bd49c8b36b4289b9788 Mon Sep 17 00:00:00 2001 From: "Alexis H. Munsayac" Date: Fri, 16 Feb 2024 14:37:06 +0800 Subject: [PATCH] Fix missed bindings --- src/babel/core/create-registry.ts | 20 ++++++++++------- src/babel/core/get-import-identifier.ts | 29 +++++++++++-------------- src/babel/core/transform-jsx.ts | 8 +++---- src/babel/index.ts | 28 ++++++++++++++---------- 4 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/babel/core/create-registry.ts b/src/babel/core/create-registry.ts index cc272f1..924f582 100644 --- a/src/babel/core/create-registry.ts +++ b/src/babel/core/create-registry.ts @@ -20,15 +20,19 @@ export function createRegistry( const root = getRootStatementPath(path); const identifier = path.scope.generateUidIdentifier(REGISTRY); - const [tmp] = root.insertBefore( - t.variableDeclaration('const', [ - t.variableDeclarator( - identifier, - t.callExpression(getImportIdentifier(state, path, IMPORT_REGISTRY), []), - ), - ]), + root.scope.registerDeclaration( + root.insertBefore( + t.variableDeclaration('const', [ + t.variableDeclarator( + identifier, + t.callExpression( + getImportIdentifier(state, path, IMPORT_REGISTRY), + [], + ), + ), + ]), + )[0], ); - root.scope.registerDeclaration(tmp); const pathToHot = getHotIdentifier(state); const statements: t.Statement[] = [ t.expressionStatement( diff --git a/src/babel/core/get-import-identifier.ts b/src/babel/core/get-import-identifier.ts index 926ccd6..0c82081 100644 --- a/src/babel/core/get-import-identifier.ts +++ b/src/babel/core/get-import-identifier.ts @@ -14,23 +14,20 @@ export function getImportIdentifier( return current; } const programParent = path.scope.getProgramParent(); - const uid = programParent.generateUidIdentifier( - registration.kind === 'named' ? registration.name : 'default', + const uid = programParent.generateUidIdentifier(name); + programParent.registerDeclaration( + (programParent.path as babel.NodePath).unshiftContainer( + 'body', + t.importDeclaration( + [ + registration.kind === 'named' + ? t.importSpecifier(uid, t.identifier(registration.name)) + : t.importDefaultSpecifier(uid), + ], + t.stringLiteral(registration.source), + ), + )[0], ); - const newPath = ( - programParent.path as babel.NodePath - ).unshiftContainer( - 'body', - t.importDeclaration( - [ - registration.kind === 'named' - ? t.importSpecifier(uid, t.identifier(registration.name)) - : t.importDefaultSpecifier(uid), - ], - t.stringLiteral(registration.source), - ), - )[0]; - programParent.registerDeclaration(newPath); state.imports.set(target, uid); return uid; } diff --git a/src/babel/core/transform-jsx.ts b/src/babel/core/transform-jsx.ts index 7a1679c..70dabec 100644 --- a/src/babel/core/transform-jsx.ts +++ b/src/babel/core/transform-jsx.ts @@ -300,12 +300,12 @@ export function transformJSX( templateComp.loc = path.node.loc; } - const [tmp] = rootPath.insertBefore( - t.variableDeclaration('const', [t.variableDeclarator(id, templateComp)]), + rootPath.scope.registerDeclaration( + rootPath.insertBefore( + t.variableDeclaration('const', [t.variableDeclarator(id, templateComp)]), + )[0], ); - rootPath.scope.registerDeclaration(tmp); - path.replaceWith( skippableJSX( t.jsxElement( diff --git a/src/babel/index.ts b/src/babel/index.ts index 4363e1d..c7595a8 100644 --- a/src/babel/index.ts +++ b/src/babel/index.ts @@ -268,21 +268,22 @@ function transformFunctionDeclaration( // have zero or one parameter decl.params.length < 2 ) { - const [tmp] = path.replaceWith( - t.variableDeclaration('const', [ - t.variableDeclarator( - decl.id, - wrapComponent( - state, - path, + path.scope.registerDeclaration( + path.replaceWith( + t.variableDeclaration('const', [ + t.variableDeclarator( decl.id, - t.functionExpression(decl.id, decl.params, decl.body), - decl, + wrapComponent( + state, + path, + decl.id, + t.functionExpression(decl.id, decl.params, decl.body), + decl, + ), ), - ), - ]), + ]), + )[0], ); - path.scope.registerDeclaration(tmp); path.skip(); } } @@ -375,6 +376,9 @@ export default function solidRefreshPlugin(): babel.PluginObj { transformFunctionDeclaration(state, path); }, }); + // TODO anything simpler than this? + // This is to fix an issue with webpack + programPath.scope.crawl(); }, }, };