load-alias

    1.0.8 • Public • Published

    Install

    npm install load-alias

    About

    This module requires files via aliases

    Example

    const LoadAlias = require('load-alias');
    
    const loadConfig = {
      public: 'public/',
      css: '@public/css/',
      img: '@public/img/',
      favicon: '@img/favicon.ico',
      db: 'db/',
      dbModels: '@db/models/',
      upload: 'upload/',
      test: function (alias, replacement) {
    	  return 'some/thing';
      }	
    }
    
    const alias = new LoadAlias(loadConfig); // or new loadAlias(rootPath, loadConfig) 
    
    let modelUser = alias.load('@dbModels/user.js');
    let imgAvatarPath = alias.getPath('@img/avatar.png');  // => 'public/img/avatar.png'
    let imgAvatarFullPath = alias.getFullPath('@img/avatar.png'); // => alias.rootPath + '/public/img/avatar.png'
    let path = alias.require('path'); // gets nodejs path library
    let something = alias.require('./relative/path/'); // doesn't merge with .rootPath

    Root path

    The default alias.rootPath = process.cwd()
    But you can change it: alias.rootPath = something

    Api

    .load(path)

    Loading file using a full path

    .require(path)

    Loading file using a relative path.

    .getConfig()

    Getting the config

    .setConfig(config)

    Config will be merged with the current

    .get(name)

    Getting the alias

    .set(name, alias)

    Adding or updating the alias

    .del(name)

    Deleting the alias

    .getPath(path)

    Getting transformed path without .rootPath

    .getFullPath(path)

    Getting the full transformed path including .rootPath

    Install

    npm i load-alias

    DownloadsWeekly Downloads

    2

    Version

    1.0.8

    License

    MIT

    Unpacked Size

    7.19 kB

    Total Files

    7

    Last publish

    Collaborators

    • ortex