Git Product home page Git Product logo

alias-imports's Introduction

alias-imports

Create bare aliases in the imports map in package.json.

Features

  • Supports both ESM import and CommonJS require()
  • Subpath patterns support
  • Conditions support
  • Overwrite import/require()s in dependencies

Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️

Install

npm i -D alias-imports

Usage

Declare aliases in the imports map in package.json:

{
    "imports": {
        // Aliases
        "lodash": "./custom-lodash.js"
    }
}

Then, run your script with the alias-imports loader:

node --loader alias-imports ./file.js

Whenever lodash is imported, ./custom-lodash.js will be loaded.

Subpath patterns

Like Node.js, alias-imports supports subpath patterns.

With this configuration, all lodash/* imports will be aliased to ./custom-lodash/*:

{
    "imports": {
        "lodash/*": "./custom-lodash/*"
    }
}

Conditions

Like Node.js, alias-imports supports conditions.

With this configuration, lodash will be aliased to ./custom-lodash.js by default. But when --conditions underscore is passed in, it will resolve to underscore instead:

{
    "imports": {
        "lodash": {
            "underscore": "underscore",
            "default": "./custom-lodash.js"
        }
    }
}

Pass in a condition:

node --loader alias-imports --conditions underscore ./file.js

Examples

Toggle between Webpack 4 & 5

package.json

{
    // Setup imports to load webpack4 by default
    // and webpack 5 when the webpack5 condition is specified
    "imports": {
        "webpack": {
            "webpack5": "webpack5",
            "default": "webpack4"
        },

        // This entry maps webpack subpaths to webpack4 or webpack5
        "webpack/*": {
            "webpack5": "webpack5/*",
            "default": "webpack4/*"
        }
    },

    // Install Webpack 4 & 5 to webpack4 & webpack5 respectively
    "devDependencies": {
        "webpack4": "npm:[email protected]",
        "webpack5": "npm:[email protected]"
    }
}

Load Webpack 4 (default)

node --loader alias-imports ./file.js

Load Webpack 5

Specify the webpack5 condition via the --conditions, -C flag.

node --loader alias-imports -C webpack5 ./file.js

API

Loader

Pass in alias-import/loader to --loader to load the import hook. This will only add alias support to ESM imports.

node --loader alias-imports/loader ./file.js

Require hook

Pass in alias-import/require to --require to load the require hook. This will only add alias support to require() calls.

node --require alias-import/require ./file.js

Both

Passing in alias-imports loads both the loader and the require hook:

node --loader alias-imports ./file.js

Debugging

Set the DEBUG_ALIAS_IMPORTS environment variable to see which imports aliases are being resolved by whom.

DEBUG_ALIAS_IMPORTS=1 node --loader alias-imports ./file.js

Dependencies

Used to resolve imports in package.json.

FAQ

How is it different from native imports?

  • Native aliases in imports must be prefixed with #, whereas aliases with alias-imports don't.

  • Because this loader allows you to create unprefixed aliases, they can be used to overwrite import paths (e.g. lodash can point to underscore).

  • Affects dependency packages as well, not just the current package

When should I use this instead of native imports?

In general, you should use native imports when possible. If you're creating a new alias, prefer to use the # prefix.

However, if you're trying to overwrite an import path in a dependency package (e.g. lodash to underscore), you can use this package to achieve that.

Can I use this in published packages?

Published packages should not use this because it relies on the consumer to start the Node.js process with the alias-imports loader.

However, it can be used in application codebases to overwrite import paths in dependencies.

alias-imports's People

Contributors

privatenumber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.