Skip to content

Creating path aliases

Wojtek Cichoń edited this page Jan 25, 2021 · 2 revisions

In order to create an import alias for a path you need to do the following.

1. Add the path declaration in tsconfig.json.

{
  "compilerOptions": {
    "paths": {
      "MyPath/*": ["src/my-path/*"]
    }
  }
}

2. Add declaration in the Webpack configuration.

// webpack/paths.js
module.exports = {
  myPath: "./src/my-path"
}

// webpack/resolve.js
module.exports = {
  resolve: {
    alias: {
      MyPath: path.resolve(__dirname, "..", paths.myPath),
    },
  },
}

3. Add declaration to the Jest configuration in jest.config.js.

module.exports = {
  moduleNameMapper: {
    "MyPath/(.*)": "<rootDir>/src/my-path/$1",
  }
}