GSAP ScrollTrigger cannot be imported as module in build
StevenJPx2 opened this issue · 8 comments
Environment
Nuxt CLI v3.0.0-27383920.81ee59c
Nuxt project info:
- Operating System:
Darwin
- Node Version:
v17.3.1
- Nuxt Version:
3.0.0-27383920.81ee59c
- Package Manager:
yarn@1.22.15
- Bundler:
Vite
- User Config:
build
- Runtime Modules:
-
- Build Modules:
-
Reproduction
npm i gsap
<script setup lang="ts">
import {gsap} from 'gsap'
// even though importing gsap like "import gsap from 'gsap'"
// is valid, it throws the error gsap.registerPlugin is not a function
// when running the production build
import ScrollTrigger from 'gsap/dist/ScrollTrigger'
// over here, the error is "Did you mean to import gsap/dist/ScrollTrigger.js?"
// Once it's done, it works, but there no longer is any typescript support
gsap.registerPlugin(ScrollTrigger)
</setup>
Describe the bug
Build errors
Additional context
No response
Logs
No response
Is it works for you?
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';
gsap.registerPlugin(ScrollTrigger)
No, same error all around.
import { ScrollTrigger } from 'gsap/ScrollTrigger';
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger';
import ScrollTrigger from 'gsap/ScrollTrigger';
import ScrollTrigger from 'gsap/dist/ScrollTrigger';
// all do not work
// only
import { ScrollTrigger } from 'gsap/ScrollTrigger.js';
import { ScrollTrigger } from 'gsap/dist/ScrollTrigger.js';
import ScrollTrigger from 'gsap/ScrollTrigger.js';
import ScrollTrigger from 'gsap/dist/ScrollTrigger.js';
// runs in production (build is fine, it's a runtime error)
We may be able to improve this experience in dev (resolving to the id with .js
).
But the core of this is a packaging issue with gsap
: see https://v3.nuxtjs.org/concepts/esm. You could likely resolve by adding it to your build.transpile
array.
I tried it, this is the error I got:
self is not defined
at file://./.nuxt/dist/server/server.mjs:7504:22
at $id_e82c9c8c (file://./.nuxt/dist/server/server.mjs:7505:
at __instantiateModule__ (file://./.nuxt/dist/server/server.
at __ssrLoadModule__ (file://./.nuxt/dist/server/server.mjs:
at ssrImport (file://./.nuxt/dist/server/server.mjs:10125:13
at $id_49eb7661 (file://./.nuxt/dist/server/server.mjs:1784:
at async __instantiateModule__ (file://./.nuxt/dist/server/s
m.default is not a function
at file://./.nuxt/dist/server/server.mjs:10:126
at processTicksAndRejections (node:internal/process/task_que
at async renderToString (file://./node_modules/vue-bundle-re
at async renderMiddleware (file://./.nuxt/nitro/index.mjs:20
at async handle (file://./node_modules/h3/dist/index.mjs:601
Right now my solution for that is to use the script version of it like this in the nuxt.config.js/ts
Also gsap is recommending this kind of, because the probability is high, that your user has already downloaded the script from another site. Downside is of course, if you only need in some specific components, but then you could also load it with useMeta there.
meta: {
script: [
{ src: "https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/gsap.min.js" },
{
src: "https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/ScrollTrigger.min.js",
},
],
},
@chrispreisler I would say that this solution just goes back to the first issue that I have with ScrollTrigger. I need TS support, and that doesn't work if I import the js file. ScrollTrigger works perfectly otherwise.
@StevenJPx2 you are correct, sorry I overread your need for TS support.
This worked for me (combination of answers before):
In nuxt.config.ts:
build: {
transpile: ['gsap']
}
in app.vue:
<script setup lang="ts">
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';
gsap.registerPlugin(ScrollTrigger)
function addScrollClass() {
ScrollTrigger.create({
// markers: true,
start: '200px top',
trigger: 'body',
toggleClass: { targets: '.navbar-main', className: 'is-scrolled' },
})
}
onMounted(() => {
addScrollClass()
})
</script>