Skip to content

Commit e73a8c9

Browse files
committed
Fix local storage warning
1 parent 57343ce commit e73a8c9

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎packages/histoire/src/node/dom/env.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ export function createDomEnv() {
1717
},
1818
)
1919

20+
const restoreStorageGlobals = useStorageGlobals(dom.window)
2021
const { keys, originals } = populateGlobal(globalThis, dom.window, { bindFunctions: true })
2122

2223
function destroy() {
2324
keys.forEach(key => delete globalThis[key])
2425
originals.forEach((v, k) => {
2526
globalThis[k] = v
2627
})
28+
restoreStorageGlobals()
2729
}
2830

2931
window.ResizeObserver = window.ResizeObserver || class ResizeObserver {
@@ -58,3 +60,33 @@ export function createDomEnv() {
5860
destroy,
5961
}
6062
}
63+
64+
function useStorageGlobals(window: Window) {
65+
const originalLocalStorage = Object.getOwnPropertyDescriptor(globalThis, 'localStorage')
66+
const originalSessionStorage = Object.getOwnPropertyDescriptor(globalThis, 'sessionStorage')
67+
68+
Object.defineProperty(globalThis, 'localStorage', {
69+
value: window.localStorage,
70+
configurable: true,
71+
writable: true,
72+
})
73+
Object.defineProperty(globalThis, 'sessionStorage', {
74+
value: window.sessionStorage,
75+
configurable: true,
76+
writable: true,
77+
})
78+
79+
return () => {
80+
restoreGlobal('localStorage', originalLocalStorage)
81+
restoreGlobal('sessionStorage', originalSessionStorage)
82+
}
83+
}
84+
85+
function restoreGlobal(key: 'localStorage' | 'sessionStorage', descriptor?: PropertyDescriptor) {
86+
if (descriptor) {
87+
Object.defineProperty(globalThis, key, descriptor)
88+
}
89+
else {
90+
delete (globalThis as any)[key]
91+
}
92+
}

‎packages/histoire/src/node/vite.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ const collectInlineDeps = [
3030
/vite\w*\/dist\/client\/(client|env).mjs/,
3131
]
3232

33+
const collectVueAliases = {
34+
vue: 'vue/dist/vue.runtime.esm-bundler.js',
35+
'@vue/runtime-dom': '@vue/runtime-dom/dist/runtime-dom.esm-bundler.js',
36+
'@vue/runtime-core': '@vue/runtime-core/dist/runtime-core.esm-bundler.js',
37+
'@vue/reactivity': '@vue/reactivity/dist/reactivity.esm-bundler.js',
38+
'@vue/shared': '@vue/shared/dist/shared.esm-bundler.js',
39+
}
40+
3341
export async function mergeHistoireViteConfig(viteConfig: InlineConfig, ctx: Context) {
3442
if (ctx.config.vite) {
3543
const command = ctx.mode === 'dev' ? 'serve' : 'build'
@@ -123,14 +131,14 @@ export async function getViteConfigWithPlugins(isServer: boolean, ctx: Context):
123131
resolve: {
124132
dedupe: [
125133
'vue',
134+
'@vue/runtime-dom',
135+
'@vue/runtime-core',
136+
'@vue/reactivity',
137+
'@vue/shared',
126138
],
127139
alias: {
128140
'histoire-style': join(APP_PATH, process.env.HISTOIRE_DEV ? 'app/style/main.pcss' : 'style.css'),
129-
...(isServer
130-
? {
131-
vue: 'vue/dist/vue.runtime.esm-bundler.js',
132-
}
133-
: {}),
141+
...(isServer ? collectVueAliases : {}),
134142
},
135143
...(isServer
136144
? {

0 commit comments

Comments
 (0)