Coder Social home page Coder Social logo

require-alias's Introduction

require-aliases

Require aliases loads aliases from .aliasrc.json or any file like tsconfig.json into import resolver. Also supports eslint import resolver.

Install

# with npm
npm install require-aliases

# or with Yarn
yarn add require-aliases

Usage

As early as possible in your application, import require-aliases.

require('require-aliases');

Create a .aliasrc.json file in the root directory of your project.

.aliasrc.json

{
  "baseUrl": ".",
  "paths": {
    "@redis": ["connect/redis"]
  }
}
const redis = require('@redis'); // that resolves ${baseSrc}/connect/redis

For eslint

Add require-aliases/eslint before node in eslintrc.js settings import resolver property.

.eslintrc.js

module.exports = {
  settings: {
    'import/resolver': ['require-aliases/eslint', 'node'],
  },
};

Configuring

Theese fields are defaults

require("require-aliases")({
  src: '.aliasrc.json', // File that contains paths and baseUrl option
  from: 'paths', // Aliases place
  baseSrc: 'baseUrl' // BaseSrc place
})

/**
 * Above code means your config file is `.aliasrc.json`
 * And your aliases defined in config files `paths` property
 * And your baseSrc defined in config files `baseUrl` property
 */

You can use this for tsconfig.json

require("require-aliases")({
 src: 'tsconfig.json', // File that contains paths and baseUrl option
 from: 'compilerOptions.paths', // Aliases place
 baseSrc: 'compilerOptions.baseUrl' // BaseSrc place
})

And for .eslintrc.json

module.exports = {
  settings: {
    "import/resolver": {
      "require-aliases/eslint":{
         src: 'tsconfig.json',
         from: 'compilerOptions.paths',
         baseSrc: 'compilerOptions.baseUrl'
      },
      node: {}
    }
  },
};

You have to refresh the window whenever you make a change.

Note

The tsconfig.json or jsconfig.json file is required for vscode to resolve imports.

Why should i use this instead of module-alias

First of all module-alias does not support eslint. And vscode doesnt resolves aliases at importing. If you want to vscode recognize aliases you must define aliases twice. But with this module you define aliases only once and vscode and eslint resolves aliases.

License

Distributed under the MIT License. See LICENSE for more information.

require-alias's People

Contributors

virtueer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.