Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 global import

What is the best way to import some modules in all of the files of the project, so I don't have to write stuff like:

import React from 'react';
import Reflux from 'reflux';
import reactMixin from 'react-mixin';

in almost every single file?

like image 611
Victor Marchuk Avatar asked May 31 '15 18:05

Victor Marchuk


People also ask

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.

What is CommonJS vs ES6?

ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse.

Should I use require or import?

One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. To use the require() statement, a module must be saved with . js extension as opposed to .


2 Answers

The other answer covers this, but not with valid ES6, so I'm adding my own. Make a central file to import your react components, in some central react.js file

export {default as React} from 'react';
export {default as Reflux} from 'reflux';
export {default as reactMixin} from 'react-mixin';

Then in the files where you need to use these three, you could do

import {React, Reflux, reactMixin} from './react';

to import all three into your component file.

like image 69
loganfsmyth Avatar answered Oct 03 '22 04:10

loganfsmyth


Create a "base" that declares common imports, then you can import that one file.

like image 35
eXecute Avatar answered Oct 03 '22 04:10

eXecute