Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

GSAP ScrollTrigger cannot be imported as module in build #2889

Closed
StevenJPx2 opened this issue Jan 24, 2022 · 8 comments
Closed

GSAP ScrollTrigger cannot be imported as module in build #2889

StevenJPx2 opened this issue Jan 24, 2022 · 8 comments

Comments

@StevenJPx2
Copy link

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

@franzus5
Copy link

@StevenJPx2

Is it works for you?

import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';

gsap.registerPlugin(ScrollTrigger)

@StevenJPx2
Copy link
Author

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)

@danielroe
Copy link
Member

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.

@StevenJPx2
Copy link
Author

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

@chrispreisler
Copy link

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",
      },
    ],
  },

@StevenJPx2
Copy link
Author

@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.

@chrispreisler
Copy link

@StevenJPx2 you are correct, sorry I overread your need for TS support.

@Zielgestalt
Copy link

Zielgestalt commented Feb 7, 2022

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>

@nuxt nuxt locked and limited conversation to collaborators Apr 14, 2022
@danielroe danielroe converted this issue into a discussion Apr 14, 2022
@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants