Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6 Module Support? #313

Closed
kaseyhinton opened this issue Aug 24, 2018 · 24 comments · Fixed by #1298
Closed

ES6 Module Support? #313

kaseyhinton opened this issue Aug 24, 2018 · 24 comments · Fixed by #1298
Labels

Comments

@kaseyhinton
Copy link

kaseyhinton commented Aug 24, 2018

I am trying to use this library in a polymer 3 typescript project. Would it be possible to add support for es6 modules?

Code

import dayjs from 'dayjs';

Result

my-app.ts:73 One or more components failed to load SyntaxError: The requested module '../node_modules/dayjs/dayjs.min.js' does not provide an export named 'default'

Workaround

<script src="node_modules/dayjs/dayjs.min.js" async></script>
@kaseyhinton kaseyhinton changed the title ES Module Support? ES6 Module Support? Aug 24, 2018
@ghost
Copy link

ghost commented Aug 25, 2018

try this import * as dayjs from 'dayjs';

@kaseyhinton
Copy link
Author

When i import like that i receive an error

One or more components failed to load TypeError: Cannot set property 'dayjs' of undefined

Is there something i have not configured properly with typescript perhaps?

@ghost
Copy link

ghost commented Oct 9, 2018

Have you ever tried the --allowSyntheticDefaultImports option for tsc? I think with that option you may use the ES6 syntax.

@alxdnlnko
Copy link

Script tag workaround helps in browser, but I can't test (jest) my ES6 modules which use dayjs.
I vote for ES6 modules support! Isn't it a "must-have" feature nowdays, when browsers get native modue imports support? Especially for such nice and usefull tool like dayjs :)

@iamkun
Copy link
Owner

iamkun commented Feb 2, 2019

🎉 This is included in version 1.8.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iamkun iamkun closed this as completed Feb 2, 2019
@juhangsin
Copy link

One issue with --allowSyntheticDefaultImports is that, as library developers we can't rely on this to be set. We really need way to use this library without this flag.

@jogibear9988
Copy link

Why is this issue closed? I still get this error when using

import dayjs from '/node_modules/dayjs/dayjs.min.js';

@jogibear9988
Copy link

like this I could use it in ES6 directly (without minification)

import dayjs from '../../node_modules/dayjs/esm/index.js'
import LocalizedFormat from '../../node_modules/dayjs/esm/plugin/localizedFormat/index.js'

@hakimio
Copy link

hakimio commented Nov 11, 2020

@iamkun browser config in your package.json should be pointing to esm bundle like in firebase package.json to fix this issue. Please take a look at it when you have some time.

@iamkun
Copy link
Owner

iamkun commented Nov 11, 2020

Should we use esm in this way?

import dayjs from 'dayjs/esm/index.js'

@hakimio
Copy link

hakimio commented Nov 11, 2020

@iamkun at least when using Angular, that would cause "deep import" warnings. If you fixed package.json we could just use ES6 module without doing these kind of deep imports.

@hakimio
Copy link

hakimio commented Nov 11, 2020

Detailed discussion about this on firebase repository: firebase/firebase-js-sdk#3315 (comment)

@hakimio
Copy link

hakimio commented Nov 11, 2020

If we follow NodeJS documentation module config should point to ES6 module and main can point to commonJS module for backwards compatibility. I think it might be best if dayjs defined its modules the same way firebase does:

  "main": "dayjs.min.js",
  "module": "esm/index.js",
  "browser": "esm/index.js"

@iamkun
Copy link
Owner

iamkun commented Nov 11, 2020

@hakimio Thanks. You can find some related issue here #598

@hakimio
Copy link

hakimio commented Nov 11, 2020

@iamkun if you can't change module because of #492 , maybe you could at least add browser config?

@iamkun
Copy link
Owner

iamkun commented Nov 11, 2020

Cool, I'm not sure if browser has the same side effect or not, some more test is needed before adding.

@hakimio
Copy link

hakimio commented Nov 11, 2020

@iamkun browser config shouldn't have any effect on nodejs. It just might be important to test if it doesn't break TypeScript projects.

@hiredgunhouse
Copy link

I try that:

import * as dayjs from 'dayjs'
var someDate = dayjs(new Date()).add(28, 'day')

and I get dayjs is not a function
How come there is no sample of ES6 import in the docs?

@hakimio
Copy link

hakimio commented Nov 17, 2020

@hiredgunhouse
For some reason the way you import it changed: import dayjs from 'dayjs';:
https://stackblitz.com/edit/js-uccszw

iamkun pushed a commit that referenced this issue Jan 3, 2021
# [1.10.0](v1.9.8...v1.10.0) (2021-01-03)

### Bug Fixes

* add ordinal to localeData plugin ([#1266](#1266)) ([fd229fa](fd229fa))
* add preParsePostFormat plugin & update Arabic [ar] locale ([#1255](#1255)) ([f2e4790](f2e4790))
* add type support for plural forms of units ([#1289](#1289)) ([de49bb1](de49bb1))
* escape last period to match only milliseconds ([#1239](#1239)) ([#1295](#1295)) ([64037e6](64037e6))

### Features

* add ES6 Module Support, package.json module point to "esm/index.js" ([#1298](#1298)) ([f63375d](f63375d)), closes [#598](#598) [#313](#313)
@iamkun
Copy link
Owner

iamkun commented Jan 3, 2021

🎉 This issue has been resolved in version 1.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@lukepuplett
Copy link

I'm running version 1.10.5 and I have the original problem still.

Uncaught SyntaxError: The requested module '../lib/dayjs/dayjs.min.js' does not provide an export named 'default'

Transpiled TypeScript to deployed JavaScript looks like this in browser:

import dayjs from "../lib/dayjs/dayjs.min.js";

@nativ69
Copy link

nativ69 commented Apr 18, 2022

Same problem here.
however, I found a workaround, just import ESM directly.
for example:
import dayjs from 'https://unpkg.com/[email protected]/esm/index.js'; this is for JavaScript, for TypeScript (haven't tested by myself) import index.d.js

the downside is for this workaround are:
A. it's without minification
B. browser will make 4 requests to load the basic needed (index.js, en.js, utils.js and constants.js)

It will be better if dayjs include minified esm bundle version for browsers.

andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this issue May 10, 2022
# [1.10.0](iamkun/dayjs@v1.9.8...v1.10.0) (2021-01-03)

### Bug Fixes

* add ordinal to localeData plugin ([#1266](iamkun/dayjs#1266)) ([fd229fa](iamkun/dayjs@fd229fa))
* add preParsePostFormat plugin & update Arabic [ar] locale ([#1255](iamkun/dayjs#1255)) ([f2e4790](iamkun/dayjs@f2e4790))
* add type support for plural forms of units ([#1289](iamkun/dayjs#1289)) ([de49bb1](iamkun/dayjs@de49bb1))
* escape last period to match only milliseconds ([#1239](iamkun/dayjs#1239)) ([#1295](iamkun/dayjs#1295)) ([64037e6](iamkun/dayjs@64037e6))

### Features

* add ES6 Module Support, package.json module point to "esm/index.js" ([#1298](iamkun/dayjs#1298)) ([f63375d](iamkun/dayjs@f63375d)), closes [#598](iamkun/dayjs#598) [#313](iamkun/dayjs#313)
andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this issue May 10, 2022
# [1.10.0](iamkun/dayjs@v1.9.8...v1.10.0) (2021-01-03)

### Bug Fixes

* add ordinal to localeData plugin ([#1266](iamkun/dayjs#1266)) ([fd229fa](iamkun/dayjs@fd229fa))
* add preParsePostFormat plugin & update Arabic [ar] locale ([#1255](iamkun/dayjs#1255)) ([f2e4790](iamkun/dayjs@f2e4790))
* add type support for plural forms of units ([#1289](iamkun/dayjs#1289)) ([de49bb1](iamkun/dayjs@de49bb1))
* escape last period to match only milliseconds ([#1239](iamkun/dayjs#1239)) ([#1295](iamkun/dayjs#1295)) ([64037e6](iamkun/dayjs@64037e6))

### Features

* add ES6 Module Support, package.json module point to "esm/index.js" ([#1298](iamkun/dayjs#1298)) ([f63375d](iamkun/dayjs@f63375d)), closes [#598](iamkun/dayjs#598) [#313](iamkun/dayjs#313)
@EricYangXD
Copy link

import dayjs from 'node_modules/dayjs/esm/index';

works fine for my angular project.

@MagnusOxlund
Copy link

Is there still no ES module support for deployment directly to browsers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.