Skip to content

Commit

Permalink
Add a .default property to fix #149 and better support ES6 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Aug 26, 2020
1 parent 3be7e3a commit dcc07d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,24 @@ log.warn("too easy");

### As an ES6 module:

loglevel is written as a UMD module, with a single object exported. ES6 module loaders & transpilers don't all handle this the same way. Some will treat the object as the default export, while others use it as the root exported object. Here are the two cases:
loglevel is written as a UMD module, with a single object exported. Unfortunately ES6 module loaders & transpilers don't all handle this the same way. Some will treat the object as the default export, while others use it as the root exported object. In addition, loglevel includes `default` property on the root object, designed to help handle this differences. Nonetheless, there's two possible syntaxes that might work for you:

For tools like TypeScript and Browserify, which treat root exports as the root value of the module:
For most tools, using the default import is the most convenient and flexible option:

```javascript
import * as log from 'loglevel';
import log from 'loglevel';
log.warn("module-tastic");
```

For tools like Babel, and Node.js's [experimental ESM support](https://nodejs.org/api/esm.html), which treat root exports as the default export of the module:
For some tools though, it might better to wildcard import the whole object:

```javascript
import log from 'loglevel';
import * as log from 'loglevel';
log.warn("module-tastic");
```

There's no major difference, unless you're using TypeScript & building a loglevel plugin (in that case, see https://github.com/pimterry/loglevel/issues/149). In general though, just use whichever suits your environment, and everything should work out fine.

### With noConflict():

If you're using another JavaScript library that exposes a 'log' global, you can run into conflicts with loglevel. Similarly to jQuery, you can solve this by putting loglevel into no-conflict mode immediately after it is loaded onto the page. This resets to 'log' global to its value before loglevel was loaded (typically `undefined`), and returns the loglevel object, which you can then bind to another name yourself.
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ declare namespace log {
* This will return you the dictionary of all loggers created with getLogger, keyed off of their names.
*/
getLoggers(): { [name: string]: Logger };

/**
* A .default property for ES6 default import compatibility
*/
default: RootLogger;
}

interface Logger {
Expand Down
3 changes: 3 additions & 0 deletions lib/loglevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,8 @@
return _loggersByName;
};

// ES6 default export, for compatibility
defaultLogger.default = defaultLogger;

return defaultLogger;
}));

0 comments on commit dcc07d1

Please sign in to comment.