Giter VIP home page Giter VIP logo

craco-alias's Introduction

craco-alias

npm

A craco plugin for automatic aliases generation for Webpack and Jest.

List of Contents

Installation

  1. Install craco

  2. Install craco-alias:

    npm i -D craco-alias
  3. Edit craco.config.js:

    const CracoAlias = require("craco-alias");
    
    module.exports = {
      plugins: [
        {
          plugin: CracoAlias,
          options: {
            // see in examples section
          }
        }
      ]
    };
  4. Go to Examples section

Options

  • source:
    One of "options", "jsconfig", "tsconfig"
    Defaults to "options"

  • baseUrl:
    A base url for aliases. (./src for example)
    Defaults to ./ (project root directory)

  • aliases:
    An object with aliases names and paths
    Defaults to {}

  • tsConfigPath:
    A path to tsconfig file
    Only required when source is set to "tsconfig"

  • debug:
    Enable it if you ran into a problem. It will log a useful info in console.
    Defaults to false

Examples

Specify aliases manually (source: "options")

Note: you don't need to add /* part for directories in this case

/* craco.config.js */

const CracoAlias = require("craco-alias");

module.exports = {
  plugins: [
    {
      plugin: CracoAlias,
      options: {
        source: "options",
        baseUrl: "./",
        aliases: {
          "@file": "./src/file.js",
          "@dir": "./src/some/dir",
          // you can alias packages too
          "@material-ui": "./node_modules/@material-ui-ie10"
        }
      }
    }
  ]
};
Use aliases from jsconfig.json (source: "jsconfig")
/* craco.config.js */

const CracoAlias = require("craco-alias");

module.exports = {
  plugins: [
    {
      plugin: CracoAlias,
      options: {
        source: "jsconfig",
        // baseUrl SHOULD be specified
        // plugin does not take it from jsconfig
        baseUrl: "./src"
      }
    }
  ]
};

Note: your jsconfig should always have compilerOptions.paths property. baseUrl is optional for plugin, but some IDEs and editors require it for intellisense.

/* jsconfig.json */

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      // file aliases
      "@baz": ["./baz.js"],
      "@boo": ["./boo.jsx"],

      // folder aliases
      "@root": ["./"],
      "@root/*": ["./*"],
      "@lib": ["./lib"],
      "@lib/*": ["./lib/*"],

      // package aliases (types is optional without ts)
      "@my-react-select": [
        "../node_modules/react-select",
        "../node_modules/@types/react-select"
      ],
      "@my-react-select/*": [
        "../node_modules/react-select/*",
        "../node_modules/@types/react-select"
      ]
    }
  }
}
Use aliases from tsconfig.json (source: "tsconfig")
  1. Go to project's root directory.

  2. Create tsconfig.extend.json.

  3. Edit it as follows:

    {
      "compilerOptions": {
        "baseUrl": "./src",
        "paths": {
          // file aliases
          "@baz": ["./baz.ts"],
          "@boo": ["./boo.tsx"],
    
          // folder aliases
          "@root": ["./"],
          "@root/*": ["./*"],
          "@lib": ["./lib"],
          "@lib/*": ["./lib/*"],
    
          // package aliases
          "@my-react-select": [
            "../node_modules/react-select",
            "../node_modules/@types/react-select"
          ],
          "@my-react-select/*": [
            "../node_modules/react-select/*",
            "../node_modules/@types/react-select"
          ]
        }
      }
    }
  4. Go to tsconfig.json.

  5. Extend tsconfig.json from tsconfig.extend.json:

    {
    + "extends": "./tsconfig.extend.json",
      "compilerOptions": {
        ...
      },
      ...
    }
  6. Edit craco.config.js:

    const CracoAlias = require("craco-alias");
    
    module.exports = {
      plugins: [
        {
          plugin: CracoAlias,
          options: {
            source: "tsconfig",
            // baseUrl SHOULD be specified
            // plugin does not take it from tsconfig
            baseUrl: "./src",
            // tsConfigPath should point to the file where "baseUrl" and "paths" are specified
            tsConfigPath: "./tsconfig.extend.json"
          }
        }
      ]
    };

Ran into a problem?

  1. Make sure your config is valid.

  2. Set debug to true in options.

  3. Run application again.

  4. Copy a printed info.

  5. Here, create an issue describing your problem (do not forget to add the debug info).

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.