withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
47.26k stars 2.51k forks source link

After build, package is not defined #8728

Closed uwaovo closed 1 year ago

uwaovo commented 1 year ago

Astro Info

Astro                    v3.2.1
Node                     v18.16.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/react
                         unocss

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

astro dev runs normally, but when I use build and then preview, I encounter an error: Uncaught (in promise) ReferenceError: Cesium is not defined. image In _astro js: image

What's the expected result?

show as in astro dev.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-9z3dkb

Participation

bluwy commented 1 year ago

The issue is that vite-plugin-cesium uses transformIndexHtml to link the build output of Cesium global runtime. Astro doesn't run the transformIndexHtml hook as it doesn't work for us, and it only applies to Vite-handled HTML pages.

As a workaround, you can add that script yourself by placing this in the <head> of Layout.astro:

{import.meta.env.PROD && <script src={`${import.meta.env.BASE_URL}cesium/Cesium.js`}></script>}

As this isn't something Astro supports by default, I'll close this as expected behaviour.