Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepted practice for an ES6 module imported just for side-effects?

I like to keep my code modular, so I put this kind of code in a separate file (overrides/extra.js):

import Ember from 'ember';

Ember.RSVP.configure('onerror', function(error) {
    ....
});

export default null;

This has only the side effect of configuring Ember.RSVP but does not export anything of value. I would then import this in app.js:

import dummy from './overrides/extra';

Is this accepted practice?

like image 580
blueFast Avatar asked Jan 06 '16 08:01

blueFast


People also ask

Can ES6 modules have side effects?

Pure and Non Pure Modules A module with side-effects is one that changes the scope in other ways then returning something, and it's effects are not always predictable, and can be affected by outside forces (non pure function).

How can I conditionally import an ES6 module?

To conditionally import an ES6 module with JavaScript, we can use the import function. const myModule = await import(moduleName); in an async function to call import with the moduleName string to import the module named moduleName . And then we get the module returned by the promise returned by import with await .

How do I import ES6 into browser?

Safari, Chrome, Firefox and Edge all support the ES6 Modules import syntax. Here's what they look like. Simply add type="module" to your script tags and the browser will load them as ES Modules. The browser will follow all import paths, downloading and executing each module only once.


1 Answers

Yes this is accepted if your module doesn't need to export any data, but there's no need to export anything from a module if it's not required:

import Ember from 'ember';

Ember.RSVP.configure('onerror', function(error) {
    ....
});

app.js:

import './overrides/extra';
like image 68
CodingIntrigue Avatar answered Oct 25 '22 11:10

CodingIntrigue