Skip to content

Commit 2847227

Browse files
authored
add object support for +staticReplace (#2983)
1 parent 99d3239 commit 2847227

6 files changed

Lines changed: 56 additions & 0 deletions

File tree

packages/vike/src/node/vite/plugins/pluginStaticReplace/applyStaticReplace.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ const staticReplaceVue: StaticReplace[] = [
7777
},
7878
remove: { arg: 2, prop: 'default' },
7979
},
80+
{
81+
env: 'server',
82+
filter: 'vike-vue/useHydrated',
83+
type: 'call',
84+
match: {
85+
function: 'import:vike-vue/useHydrated:useHydrated',
86+
},
87+
replace: { with: { value: false } },
88+
},
8089
]
8190

8291
const staticReplaceSolid: StaticReplace[] = [

packages/vike/src/node/vite/plugins/pluginStaticReplace/applyStaticReplace.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ function valueToAst(value: unknown): t.Expression {
182182
if (typeof value === 'string') return t.stringLiteral(value)
183183
if (typeof value === 'number') return t.numericLiteral(value)
184184
if (typeof value === 'boolean') return t.booleanLiteral(value)
185+
if (Array.isArray(value)) {
186+
return t.arrayExpression(value.map(valueToAst))
187+
}
188+
if (typeof value === 'object') {
189+
const properties = Object.entries(value).map(([key, val]) => {
190+
return t.objectProperty(t.identifier(key), valueToAst(val))
191+
})
192+
return t.objectExpression(properties)
193+
}
185194
return t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('parse')), [
186195
t.stringLiteral(JSON.stringify(value)),
187196
])
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function SomeComponent() {
2+
const isHydrated = false;
3+
if (isHydrated) {
4+
return 'Hydrated!';
5+
} else {
6+
return 'SSR';
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useHydrated } from 'vike-react/useHydrated'
2+
3+
export function SomeComponent() {
4+
const isHydrated = useHydrated()
5+
if (isHydrated) {
6+
return 'Hydrated!'
7+
} else {
8+
return 'SSR'
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function SomeComponent() {
2+
const isHydrated = {
3+
value: false
4+
};
5+
if (isHydrated.value) {
6+
return 'Hydrated!';
7+
} else {
8+
return 'SSR';
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useHydrated } from 'vike-vue/useHydrated'
2+
3+
export function SomeComponent() {
4+
const isHydrated = useHydrated()
5+
if (isHydrated.value) {
6+
return 'Hydrated!'
7+
} else {
8+
return 'SSR'
9+
}
10+
}

0 commit comments

Comments
 (0)