What's the shortest ES6 equivalent of require
function call below?
module.exports = function(app) {...};
require('./routes')(app);
In other words is there a one-liner equivalent in ES6 modules?
What's the shortest ES6 equivalent of require
function call below?
module.exports = function(app) {...};
require('./routes')(app);
In other words is there a one-liner equivalent in ES6 modules?
I've just started to delve into ES6, but I believe that would be something like:
import * as routes from './routes';
...assuming ./routes
is an ES6 module exporting something.
This can then be used immediately like so:
import * as routes from './routes';
doAThing( routes.myVar, routes.myMethod() );
If the module has only a single named export, it's still two lines to import, then call:
import { name } from './routes';
name();
This is the same for any number of exports:
import { name1, name2 } from './routes';
name1();
name2();
A better import is as written above:
import * as routes from './routes';
routes.foo();
routes.bar();
I used the "recommended" format from this Axel Rauschmayer post relating to ES6 modules, but depending on what the module exports your import statement may look different:
import * as fs from 'fs'; // recommended
I find this (1 line to import, 1 line to invoke) syntax clear and readable, so I like it. For some, it may seem unfortunate. However, the bottom line is that there is no one line import/invoke in ES6