shadow-cljs

niwinz 2025-05-27T13:52:40.318979Z

Hello @thheller. On the storybook (ESM build) i'm getting the following error on reloading the namespace.

Namespace "shadow.esm.esm_import$react" already declared.
Continue on a 🧵

niwinz 2025-05-27T13:53:08.731379Z

The module in question is:

js
import "./cljs_env.js";

import * as esm_import$react from "react";
goog.provide("shadow.esm.esm_import$react");
shadow.esm.esm_import$react = esm_import$react;

niwinz 2025-05-27T13:53:31.450799Z

the exception is raised inside goog.provide.

thheller 2025-05-27T13:53:34.160469Z

is storybook doing the reloading?

niwinz 2025-05-27T13:54:07.417249Z

we are building a esm module which storybook consumes

thheller 2025-05-27T13:55:02.500959Z

so its storybook doing the reloading. not shadow-cljs?

niwinz 2025-05-27T13:55:24.628269Z

i guess yes

niwinz 2025-05-27T13:55:39.659229Z

:storybook
  {:target :esm
   :output-dir "target/storybook/"
   :js-options
   {:js-provider :import
    :entry-keys ["module" "browser" "main"]
    :export-conditions ["module" "import", "browser" "require" "default"]}

niwinz 2025-05-27T13:55:59.803369Z

we use js-provider import so storybook is reponsible to resolve that imports

niwinz 2025-05-27T13:56:51.879609Z

Do you have some hints for make this working? I mean, it works but requires a page refresh after each change

thheller 2025-05-27T13:56:55.788649Z

shadow-cljs calls this when its devtools are loaded/connected https://github.com/thheller/shadow-cljs/blob/d12811cfb7884d86dcecb3a16cf61c9e89bb8b05/src/main/shadow/cljs/devtools/client/env.cljs#L152-L161

thheller 2025-05-27T13:57:10.309309Z

that basically patches the already declared thing

thheller 2025-05-27T13:57:27.204739Z

I guess you'd need to do the same yourself if shadow-cljs isn't reloading

niwinz 2025-05-27T13:58:36.121959Z

thanks for the hint

thheller 2025-05-27T14:02:01.760419Z

but dunno why storybook would reload that file? if it always just reloads everything stuff will likely break

niwinz 2025-05-27T14:02:57.596069Z

i'm not clearly understand what is really happens but adding

:prepend-js ";(globalThis.goog.provide = globalThis.goog.constructNamespace_);(globalThis.goog.require = globalThis.goog.module.get);"

niwinz 2025-05-27T14:03:05.663429Z

fixes the issue

niwinz 2025-05-27T14:04:02.285779Z

Looks like it shadow making reloading

thheller 2025-05-27T14:04:29.117679Z

that wouldn't throw the already declared error

thheller 2025-05-27T14:04:44.967829Z

maybe both doing the reloading? I don't really use storybook, so no clue what it does

thheller 2025-05-27T14:05:03.925569Z

maybe try :devtools {:enabled false} in the build config and check if something is still reloading

niwinz 2025-05-27T14:07:02.048089Z

yeah, looks like both are reloadin

niwinz 2025-05-27T14:07:18.182019Z

disabling devtools there, still reloads on storybook

thheller 2025-05-27T14:07:56.134049Z

better to only use one method, otherwise they just compete and get in each others way and probably just always reloading twice

niwinz 2025-05-27T14:08:16.589809Z

yep, totally agree many thanks for the hints