@epicinium/cognomen

1.4.3 • Public • Published

@epicinium/cognomen

A resolver in order to shorten deeply nested relative path expression.

Build StatusLicensePackage Version

Table of Contents

Motivation

In monorepo structure or complex directory hierarchy, we used to exhausted by deeply nested import expression. (e.g. import ... from '../../../../package.json' in packages/frontend/components/button/index.jsx)

Prior Art

Installation

$ npm install --save-dev @epicinium/cognomen

Integration

Babel

If import ... from '(frontend)/application' expression in backend/server.js file. that will be transformed like const ... = require('../frontend/releases/application').

{
    "plugins": [
        [
            "module:@epicinium/cognomen",
            {
                "frontend": "releases/frontend",
                "backend": "releases/backend"
            }
        ]
    ]
}

Write or append above codes into your Babel configuration file. (e.g. babel.config.js, .babelrc(.js(on)?)?)

Babel integration has to match to output directories. (e.g. dist, out, build, ...)

ESLint

If you want to integrate with ESLint, you have to ensure eslint-plugin-import installed. and then, set resolver to your configuration file.

{
    "settings": {
        "import/resolver": {
            "@epicinium/cognomen": {
                "frontend": "sources/frontend",
                "backend": "sources/backend"
            }
        }
    }
}

Write or append above codes into your ESLint configuration file. (e.g. .eslintrc(.js(on)?|.ya?ml)?)

ESLint integration has to match to source directories. (e.g. src, ...)

Visual Studio Code

In Visual Studio Code, all JavaScript files are analyzed by internal TypeScript language handler. so, you can just write jsconfig.json file or tsconfig.json for TypeScript project.

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "(frontend)/*": "sources/frontend/*",
            "(backend)/*": "sources/backend/*"
        }
    },
    "include": ["sources"]
}

Visual Studio Code integration has to wrap alias name with parentheses and match to source directories.

Usage

After the integration process, you can write code with aliased scopes. an alias name has to wrapped by parentheses for avoiding name collision with existing modules by indirect deep dependencies.

Before

- import logger from '../../../../common/logger';

After

+ import logger from '(common)/logger';

Trivia

cognomen means alias name or nickname in the Latin Language

License

MIT Licensed.

Package Sidebar

Install

npm i @epicinium/cognomen

Weekly Downloads

0

Version

1.4.3

License

MIT

Unpacked Size

14.4 kB

Total Files

10

Last publish

Collaborators

  • cichol