Closed ThatXliner closed 10 months ago
I looked at other issues:
"type": "module"
added the the package.json)Interesting, this works in Astro components in a script type="module"
block
---
// index.astro
---
<html>
<!-- ... -->
<script type="module">
import { gsap } from "gsap";
import { TextPlugin } from "gsap/TextPlugin";
gsap.registerPlugin(TextPlugin);
</script>
<!-- ... -->
</html>
But if I put this in a top-level script
tag in a Svelte component loaded via client:load
it fails:
<script>
// Component.svelte
import { TextPlugin } from "gsap/TextPlugin";
</script>
---
// index.astro
import Component from './Component.svelte';
---
<Component client:load />
it appears that this issue isn't specific to Astro
@ThatXliner what was the issue?
@ThatXliner what was the issue?
Importing GSAP plugins (for .registerPlugin
) breaks when I try to import the package in a top level, but it won’t break when you dynamically import it during “runtime.”
For example, in Svelte, this would break:
<script>
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger);
</script>
but this works:
<script>
import { gsap } from "gsap";
import { onMount } from "svelte";
onMount(async () => {
const { ScrollTrigger } = await import("gsap/ScrollTrigger");
gsap.registerPlugin(ScrollTrigger);
})
</script>
if anyone finds this and still has the issue, one way I've found to fix it is by changing the vite config. I believe this tells vite to also bundle gsap for ssr so that gsap can also run on commonjs.
// in astro.config.mjs
... ,
vite: {
ssr: {
noExternal: [
'gsap'
]
}
}
Astro Info
StackBlitz:
Repro on my machine:
If this issue only occurs in one browser, which browser is a problem?
Likely all browser, as this is a build problem
Describe the Bug
I cannot run
gsap.registerPlugin
to notify GSAP of a new plugin. If I uncomment the line runninggsap.registerPlugin
, I getWhat's the expected result?
the
registerPlugin
function call works without any errors.Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-yprlj1?file=src%2Fpages%2Findex.astro
Participation