dixonandmoe / rellax

Lightweight, vanilla javascript parallax library
https://dixonandmoe.com/rellax/
MIT License
7.1k stars 874 forks source link

ESModules #197

Open ghost opened 4 years ago

ghost commented 4 years ago

Rellax as an ESModule

coreybyrum7 commented 4 years ago

Running into the same issue. Cannot use Rellax as a common ES6 module.

gingerchew commented 4 years ago

I can't remember where I saw it, but I think you can do something like:

import * as Rellax from '../../rellax.js';

const instance = new Rellax();

I just don't know if this is a bundler feature or a native feature.

SergiuszR commented 3 years ago

Import it as a normal ESM: import Rellax from 'rellax' then mount it: var rellax = new Rellax('.div-class'); rellax.refresh(); Then use a bundler of your choice and it should work like a charm.

gingerchew commented 3 years ago

I later found out from this article, you can just import it using the file path.

Since Rellax is wrapped in a UMD-like wrapper, it is added to the window object. All we have to do is load it as a "side effect".

This is useful if you're just using a <script type="module"></script>, but can would work inside of an ESM file as well.

<script type="module">
import './rellax.min.js'; // after loaded we will be able to use window.Rellax

var rellax = new Rellax();
</script>

No need for a bundler 😊

stratboy commented 1 year ago

Import it as a normal ESM: import Rellax from 'rellax' then mount it: var rellax = new Rellax('.div-class'); rellax.refresh(); Then use a bundler of your choice and it should work like a charm.

This is the right way.