orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
3.68k stars 387 forks source link

[Bug]: esm version has no cookieConsent variable #656

Closed ourmaninindia closed 3 months ago

ourmaninindia commented 4 months ago

Expected Behavior

after importing the esm version I get an error message that cookieConsent is not defined which is true. As a result I was forced to use the umd version.

I much prefer using the esm as I use ES6 vanilla javascript

Current Behavior

cookieConsent missing when using https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@3.0.0/dist/cookieconsent.esm.js

Steps to reproduce

just open the file

Proposed fix or additional info.

export the cookieConsent variable as u=in the umd version

Version

3.0.0

On which browser do you see the issue?

Firefox

orestbida commented 4 months ago

I'm not sure I understand the issue.

The UMD version is attached to the window object and is globally accessible. The ESM version is imported in the current context only, which is the expected behavior of ESM, afaik.

dreinon commented 3 months ago

Same issue here after importing

import CookieConsent from 'vanilla-cookieconsent';

I get this error: image

orestbida commented 3 months ago

@dreinon there is no default export so that won't work.

You should use the import * syntax, as shown in the docs:

import * as CookieConsent from 'vanilla-cookieconsent';
dreinon commented 3 months ago

Right, it might be a good idea to also export it as default, since linters recommend you import as default instead of with "*"

dreinon commented 3 months ago

image

orestbida commented 3 months ago

It mostly boils down to personal preference. I like the explicitness of named exports. They are more flexible and clearer to me.

This clearly conveys that you are willingfully loading everything:

import * as CookieConsent from 'vanilla-cookieconsent';

I would have used a default export if there was only one function exported.

ourmaninindia commented 3 months ago

I agree with you.

On Wed, 13 Mar 2024, 23:20 Orest Bida, @.***> wrote:

It mostly boils down to personal preference. I like the explicitness of named exports. They are more flexible and clearer to me.

This clearly conveys that you are willingfully loading everything:

import * as CookieConsent from 'vanilla-cookieconsent';

I would have used a default export if there was only one function exported.

— Reply to this email directly, view it on GitHub https://github.com/orestbida/cookieconsent/issues/656#issuecomment-1995990920, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARKPJ2HCBMZOXWY5S5SUOTYYDGLZAVCNFSM6AAAAABEAW234OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJVHE4TAOJSGA . You are receiving this because you were mentioned.Message ID: @.***>