Simple and lightweight module loader. Require without needing namespaces.


Keywords
import, export, importer, module, loader, include, es6, modules, nodejs
License
GPL-2.0
Install
npm install inport@1.2.7

Documentation

Inport - Node/JS Module Loader

Build Status

Simple and lightweight module loader. You may consider it as #include for Node.js.

I used this small piece of code to get rid of that nasty babelisation each time I needed to include every single object from fileA in fileB without using namespace or {explicitly, specifying, all, object, names}.

Since its only a small extension to Node’s native require(), that makes aliases of export objects in global scope, there really should be no performance impact. But anyway, it was meant for development, not for production usage (See XDK Note).

Installation

sudo npm install -g inport

Export Syntax

require('inport').$(module.exports, ...exports)

General Usage

module.js

// Define exports in the header: 
require('inport').$(module.exports, A, B, C)

function A(){console.log('akke')}
function B(){console.log('ball')}
function C(){console.log('candy')}
  • While it is not strictly required to define exports in the header, its much safer in case of circular dependency scenario

main.js

// Include everything from module as simple as that:
require('./module.js').$

// You dont even need to require inport in every file

// Now all functions from module.js could be accessed directly:
A() // -> "akke"
B() // -> "ball"
C() // -> "candy"

other.js

// You may specify what to load:
require('./module.js').$ = 'B, C'

// Or use built-in approach
const {A} = require('./module.js')

// Now all functions from module.js could be accessed directly:
A() // -> "akke"
B() // -> "ball"
C() // -> "candy"

Global Instances

You may use $$ method to import module and generate global instances of its objects with default arguments:

// Simply add another $ sign
require('./module.js').$$

// Object selections works aswell
require('./module.js').$$ = 'ClassE, ClassF'

// Now you could access global instances of imported objects by prefixing object names with $ sign
$ClassD.method() 
$ClassE.method()
$ClassF.method()

Export Sets

Export sets or «hubs» may be used for grouping export objects together. For example, if you have several files in your module and want to concentrate them in one place to be able to simply import all of them with one line of code.

set1.js

// 1. Require everything you need
module.exports = Object.assign({},
	require('./one.js'),
	require('./two.js'),
	require('./three.js')
)

// 2. Unload module.exports
require('inport').$(module.exports)

main.js

// 3. Simply load the set and you're done
require('./set1.js').$

// 4. You may load other sets and do not worry about export object collisions
require('./set2.js').$
require('./set3.js').$
require('./set4.js').$

Aliases

You could export objects under different names, using array notation

module.js

require('inport').$(module.exports, [A, 'Alpha'], [B, 'Beta'])

function A(){console.log('akke')}
function B(){console.log('ball')}
function C(){console.log('candy')}

main.js

require('./module.js').$

Alpha() // -> "akke"
Beta() // -> "ball"
A() // -> Error

Why?

ES6 imports/exports are great, but we’re not able to import everything from module without using namespace or by explicitly specifying every single object.

Imagine you have a module with lots of declared objects:

function fn1(){console.log('It\'s fn1')}
function fn2(){console.log('It\'s fn2')}
…
function fnN(){console.log('It\'s fnN')}

Using require or ES6 imports/exports, you can't import everything from module in this way:

import * from 'module' // -> <Error>

// So you could directly access any object
fn761() // -> "It's fn61" 
fn1337() // -> "It's fn1337"

Of course, you could import some module objects by directly specifying them:

import {fn1, fn2, fn3, fn4 … fnN} from 'module'

Or by wrapping module in namespace:

import * as NEO from "module";

// And then access object by using namespace prefix:
NEO.fn821() // -> "It's fn821"
NEO.fn9151() // -> "It's fn9151"

But sometimes we may just want to simply include everything from the module:

require('module.js').$

// And be able to access all module objects without using namespace:
fn1337() // -> "It's fn1337"
fn4096() // -> "It's fn4096"

XDK Note

XDK has already more efficient native implementation of Node’s import/export and dependency resolution, so you may just use it in the usual es6 manner:

import * from './module.js' // Imports all defined exports
import {B,C} from './module.js' // Imports only B and C