magic-akari / seamless-scroll-polyfill

Scroll Behavior polyfill
https://www.npmjs.com/package/seamless-scroll-polyfill
MIT License
159 stars 11 forks source link

How to import?? #123

Closed jonathanmelville closed 3 years ago

jonathanmelville commented 3 years ago

Ever since the recent update, it is unclear how to import and use this lib.

This doesn't work:

import { polyfill } from "seamless-scroll-polyfill";

polyfill();

You will get something like this Cannot resolve symbol 'polyfill'

jonathanmelville commented 3 years ago

If you are using Nuxt, you have to transpile the package:

in nuxt.config.js:

 // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    transpile: ['seamless-scroll-polyfill'],
  },

IntelliJ still can't resolve it though, but it does seem to work:

Image 2021-09-08 at 11 50 26 AM png
magic-akari commented 3 years ago

This library has two entries, the main entry with umd format and the module entry with esm.

Most packagers use the main entry, but generally there is an option that can be tweaked. For example, webpack uses resolve.mainFields to change the order of which entry to use.

I'm not familiar with Nuxt, its default mainField may not be main. But there should be a similar option that can change the mainField. If you cannot use esm format, try umd format.

IntelliJ's issue seems to be another issue about types which I have no clue.

9mm commented 3 years ago

Yes I don't understand how to import either.

import { windowScroll } from 'seamless-scroll-polyfill';
jonathanmelville commented 3 years ago

Thanks @magic-akari for the reply and for maintaining the lib! Very helpful to have this for Safari.

@9mm This does actually work exactly as described in the docs, but it's possible you might have to transpile this dependency (with say Babel) to get the import to work properly.

9mm commented 3 years ago

Ok, for vue-cli and Vue 3 this goes in vue.config.js:

transpileDependencies: ['seamless-scroll-polyfill'],