0.3.1 • Published 4 years ago

node-import-module v0.3.1

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

node-import-module

This is an npm package that saves you from long paths to modules.

Installation

$ npm install node-import-module

Usage

File structure

- components
  - app
    - actions
      - rest
        - create-app.js
    - config
      - app-config.json
    - launcher
      - launcher.js
      - index.js
    - app.exports.js
- main.js

app.exports.js

'use struct'

module.exports = {
  id: 'app',
  exports: {
    launcher: './launcher',
    config: './config/app-config.json',
    create: './actions/rest/create-app.js'
  }
};

main.js

'use strict'

const importModule = require('node-import-module');

const appLauncher = importModule('app', 'launcher');
// appLauncher === require('./components/app/launcher')

const appConfig = importModule('app', 'config');
// appConfig === require('./components/app/config/app-config.json')

const createApp = importModule('app', 'create');
// createApp === require('./components/app/actions/rest/create-app')

/**
 * OR with object syntax
*/

const importModule = require('node-import-module');

const appLauncher = importModule.app('launcher');
const appConfig = importModule.app('config');
const createApp = importModule.app('create');

/**
 * OR with destructuring syntax
*/

const {app: importApp} = require('node-import-module');
// same as
const {importApp} = require('node-import-module'); // see description below

const appLauncher = importApp('launcher');
const appConfig = importApp('config');
const createApp = importApp('create');

How does this work?

After the first import the package finds all exports files relative to the calling module. It gets the id fields of the found files and saves their paths as id => path if everthing is okay.

It throws an exception if it did not find the id field or found a few files with the same id field

This action will happen once. It is desirable that the first import be in the main file. If you do not need to use the package in the main file, simply put the following code at the beginning.

/** your code before */

require('node-import-module');

/** your code after */

API

Import function

importModule( exportsFileID : String, moduleName : String ) : Module

Returns a found module or throw exception if an error happens.

Parameters:

  • exportsFileID : String The id field in one of the exports files.

  • moduleName : String One of the keys of the exports object in the exports file.

Object syntax

importModule.app( moduleName : String ) : Module

You can use the import function as an object, where properties should be equal the id fields of the exports files.

Parameters: see description above.

Destructuring syntax

When using the object syntax you can use destructuring (ES6):

const {app} = require('node-import-module');
const someModule = app('some-module');

// The code above is the same as using the object syntax:
const importModule = require('node-import-module');
const someModule = importModule.app('some-module');

But it does not look very good. You can solve it by renaming the property name:

const {app: importApp} = require('node-import-module');
const someModule = importApp('some-module');

And this is not the best way. To make the code cleaner node-import-module supports smart destructuring: a property name can be replaced with the template import[exportsFileID], which should be in camelCase.

const {importApp} = require('node-import-module');
// === {app: importApp}

Exports file

A file that contains the paths of files to be exported. The exports file name can be any, but must satisfy the pattern *.exports.js.

Content:

  • id : String The unique identificator of the exports file.

  • exports : Object List of export modules name => path.

    • name : String - The unique name of the module.
    • path - The path to the module relative to the current exports file.

Import global modules

You can define some modules as global modules. To do this, you should create an exports file with the following content:

global.exports.js (for example)

'use strict'

module.exports = {
  id: '$', // '$' - symbol that defines a global exports file
  exports: {
    // your global modules
  }
};

And use as:

const importModule = require('node-import-module');
const someGlobalModule = importModule('some-global-module');
// note: only one parameter - moduleName

// OR with objext syntax

const someGlobalModule = importModule.$('some-global-module');

// OR with destructuring syntax

const {$} = require('node-import-module');
const someGlobalModule = $('some-global-module');

Features

  • Write a working version
  • Object syntax
  • Destructuring syntax
  • Global modules
  • Custom name of import function
  • Anything else

Footer

Issues

Author

Shcherbakov Stanislav - github/shcherbak-ssa

License

Copyright © 2020 Stanislav Shcherbakov. Released under the MIT License.

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago