Search code examples
typescriptwebpacktsconfig

Monorepo Tsconfig Paths, How to make it work with webpack?


I am trying to build a monorepo with typescript, and need to import modules as same as defined in paths config.

"paths": {
  "@monorepo/*": ["packages/*/src"]
}

As per configuration, I should able to import packages like this;

import X from "@monorepo/web/x" !== //packages/web/src/x

Somehow, this config does not work. But below line works.

import X from "@monorepo/web/src/x" === //packages/web/src/x

Is there any trick to make this work?

note: all packages named like @monorepo/web @monorepo/xyz


Solution

  • You need two *s. One for package name, one for path in package name.

    Recommend only use * for path in package name and list packages manually:

    "paths": {
      "@monorepo/web/*": ["packages/web/src/*"]
      // Add more manually
    }