Js-cookie: Document correct ES6 module usage to avoid "SyntaxError: import not found: default"

Created on 31 Mar 2018  Â·  9Comments  Â·  Source: js-cookie/js-cookie

Probably test in a browser that supports ES6 without using any library and document accordingly.

good first issue

Most helpful comment

@FagnerMartinsBrack

I tried the contents of https://github.com/js-cookie/js-cookie/issues/426 , but it seems that it will not be solved by import * as Cookies from './js-cookie-master/src/js.cookie.js';.
The Cookies variable is Module object.

2018-04-18 16 20 01

If object's name is not Cookies, I can refer towindow.Cookies and it will work.
I think that this project needs another file that is ESModule target. I'll create p-r.

All 9 comments

@FagnerMartinsBrack

I tried the contents of https://github.com/js-cookie/js-cookie/issues/426 , but it seems that it will not be solved by import * as Cookies from './js-cookie-master/src/js.cookie.js';.
The Cookies variable is Module object.

2018-04-18 16 20 01

If object's name is not Cookies, I can refer towindow.Cookies and it will work.
I think that this project needs another file that is ESModule target. I'll create p-r.

It seems documenting ES6 is more trouble than it's worth.

I reverted the commit.

Let's use this issue to test on browsers without extra dependencies and document accordingly, without assuming that the developer is using another third-party tool.

Revert commit: https://github.com/js-cookie/js-cookie/commit/d8b016be0f3c068ee4be8a121313de90502da2e0

TypeError: p.a.setCookie is not a function(…)

image

Clearly not the case. js.cookie.js is a self invoking closure which adds the module "Cookie" to the global namespace.

FagnerMartinsBrack commented on Apr 25 •
It seems documenting ES6 is more trouble than it's worth.

Fine but dont mislead your users by claiming this is an AMD module / commonjs module.

@DoubleCouponDay This thread is about ES6 module, in which the commit was reverted from the README, not AMD or CommonJS.

We support Asynchronous Module Loaders (like require.js) and CommonJS specification, see the tests for Node.js here and require.js here.

The sentence says "JavaScript Cookie can also be loaded as an AMD or CommonJS module". What's misleading there exactly?

My mistake. I attempted to load js-cookie as a module in the past but had confusing issues trying to use es6 module loading without a dependency loader. Now that I'm set up with stealjs without issues, I can do stuff like this it seems:

image

all the best,
dcd

demos

??? testing ...

// ES6
import * as Cookies from "js.cookie.js";

// ES5
var Cookies = require("js.cookie.js");

cookie getter

a more simple cookie getter method!

https://www.w3schools.com/js/js_cookies.asp

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

Closed via #537

Was this page helpful?
0 / 5 - 0 ratings

Related issues

migsyboy picture migsyboy  Â·  7Comments

jiangdefu picture jiangdefu  Â·  3Comments

nanndemoiikara picture nanndemoiikara  Â·  5Comments

Neve12ende12 picture Neve12ende12  Â·  3Comments

mhikhey371 picture mhikhey371  Â·  4Comments