mirror of
https://github.com/tuono-labs/tuono
synced 2026-07-29 22:02:46 -07:00
Prevent dynamic fn replacement when not imported by tuono (#57)
* fix: prevent dynamic fn replacement when not imported by tuono * feat: update version to v0.10.3 * Revert "fix(deps): update dependency zustand to v5 (#55)" This reverts commitd7a086dfb1. * Revert "fix(deps): update dependency zustand to v4.5.5 (#37)" This reverts commitb36dc0b897. * Revert "chore(deps): update dependency @testing-library/react to v16 (#38)" This reverts commit1a1b62698c. * Revert "chore(deps): update dependency @tanstack/config to ^0.13.0 (#32)" This reverts commit6c7cc62371. * revert pnpm update * Revert "chore(deps): update dependency vitest to v2 (#45)" This reverts commitc5b5f6aefe. * Revert "chore(deps): update dependency node to v20.18.0 (#33)" This reverts commit588326cc57.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { Plugin } from 'vite'
|
||||
import * as babel from '@babel/core'
|
||||
import type { PluginItem } from '@babel/core'
|
||||
import { isTuonoDynamicFnImported } from './utils'
|
||||
|
||||
import {
|
||||
TUONO_MAIN_PACKAGE,
|
||||
@@ -12,7 +13,6 @@ import * as t from '@babel/types'
|
||||
|
||||
import type {
|
||||
Identifier,
|
||||
ImportDeclaration,
|
||||
CallExpression,
|
||||
ArrowFunctionExpression,
|
||||
StringLiteral,
|
||||
@@ -26,13 +26,8 @@ const RemoveTuonoLazyImport: PluginItem = {
|
||||
name: 'remove-tuono-lazy-import-plugin',
|
||||
visitor: {
|
||||
ImportSpecifier: (path) => {
|
||||
if ((path.node.imported as Identifier).name === TUONO_DYNAMIC_FN_ID) {
|
||||
if (
|
||||
(path.parentPath.node as ImportDeclaration).source.value ===
|
||||
TUONO_MAIN_PACKAGE
|
||||
) {
|
||||
path.remove()
|
||||
}
|
||||
if (isTuonoDynamicFnImported(path)) {
|
||||
path.remove()
|
||||
}
|
||||
},
|
||||
},
|
||||
@@ -46,18 +41,39 @@ const ReplaceTuonoLazyImport: PluginItem = {
|
||||
name: 'remove-tuono-lazy-import-plugin',
|
||||
visitor: {
|
||||
ImportSpecifier: (path) => {
|
||||
if ((path.node.imported as Identifier).name === TUONO_DYNAMIC_FN_ID) {
|
||||
if (
|
||||
(path.parentPath.node as ImportDeclaration).source.value ===
|
||||
TUONO_MAIN_PACKAGE
|
||||
) {
|
||||
;(path.node.imported as Identifier).name = TUONO_LAZY_FN_ID
|
||||
}
|
||||
if (isTuonoDynamicFnImported(path)) {
|
||||
;(path.node.imported as Identifier).name = TUONO_LAZY_FN_ID
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const turnLazyIntoStatic = {
|
||||
VariableDeclaration: (path: babel.NodePath<t.VariableDeclaration>): void => {
|
||||
path.node.declarations.forEach((el) => {
|
||||
const init = el.init as CallExpression
|
||||
if ((init.callee as Identifier).name === TUONO_DYNAMIC_FN_ID) {
|
||||
const importName = (el.id as Identifier).name
|
||||
const importPath = (
|
||||
(
|
||||
(init.arguments[0] as ArrowFunctionExpression)
|
||||
.body as CallExpression
|
||||
).arguments[0] as StringLiteral
|
||||
).value
|
||||
|
||||
if (importName && importPath) {
|
||||
const importDeclaration = t.importDeclaration(
|
||||
[t.importDefaultSpecifier(t.identifier(importName))],
|
||||
t.stringLiteral(importPath),
|
||||
)
|
||||
|
||||
path.replaceWith(importDeclaration)
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* [SERVER build]
|
||||
* This plugin statically imports the lazy loaded components
|
||||
@@ -65,27 +81,13 @@ const ReplaceTuonoLazyImport: PluginItem = {
|
||||
const TurnLazyIntoStaticImport: PluginItem = {
|
||||
name: 'turn-lazy-into-static-import-plugin',
|
||||
visitor: {
|
||||
VariableDeclaration: (path) => {
|
||||
path.node.declarations.forEach((el) => {
|
||||
const init = el.init as CallExpression
|
||||
if ((init.callee as Identifier).name === TUONO_DYNAMIC_FN_ID) {
|
||||
const importName = (el.id as Identifier).name
|
||||
const importPath = (
|
||||
(
|
||||
(init.arguments[0] as ArrowFunctionExpression)
|
||||
.body as CallExpression
|
||||
).arguments[0] as StringLiteral
|
||||
).value
|
||||
|
||||
if (importName && importPath) {
|
||||
const importDeclaration = t.importDeclaration(
|
||||
[t.importDefaultSpecifier(t.identifier(importName))],
|
||||
t.stringLiteral(importPath),
|
||||
)
|
||||
|
||||
path.replaceWith(importDeclaration)
|
||||
Program: (path) => {
|
||||
path.traverse({
|
||||
ImportSpecifier: (subPath) => {
|
||||
if (isTuonoDynamicFnImported(subPath)) {
|
||||
path.traverse(turnLazyIntoStatic)
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user