typekit/webfontloader

Add ES6 import instead of require()

mesqueeb opened this issue ยท 6 comments

In the case of NPM, currently the documentation says to:
var WebFont = require('webfontloader')
But i think it'd be great if you could add the ES6 method of importing the library as well!

This isn't really an issue. If you think the docs should include:

import WebFont from 'webfontloader';

WebFont.load({
  google: {
    families: ['Droid Sans', 'Droid Serif']
  }
});

You should submit a pull request to add it.

great! I didn't know the syntax for the import, so couldn't do a PR.

Some libraries require import * as WebFont from 'webfontloader' and some just import WebFont from 'webfontloader'

How did you know which?

Some libraries require import * as WebFont from 'webfontloader' and some just import WebFont from 'webfontloader'

It depends on what is exported. Take a look at this: http://2ality.com/2014/09/es6-modules-final.html

The library does not support module import. When imported in a module using:

import WebFont from 'webfontloader';

you get:

Uncaught SyntaxError: The requested module '../node_modules/webfontloader/webfontloader.js' does not provide an export named 'default'

Indeed, looking at the package, there seems to be no support for ESM. It would be convenient to add such support now that module imports are supported in major browsers.

Trying to use WebFont loader with Polymer. The only import that worked for me was

import * as WebFont from 'webfontloader'

and the use

window.WebFont.load(...);

Which means the imported WebFont is never used and IDE like VS Code suggests removing it.

Would be nice if ES6 import could work the way it supposed to:

import WebFont from 'webfontloader';

WebFont.load(...);