babel-plugin-module-resolver icon indicating copy to clipboard operation
babel-plugin-module-resolver copied to clipboard

How to alias a path to another project?

Open Drecula opened this issue 7 years ago • 3 comments

Hi,

For Project_B, I need to use some components from Project_A

Project_B's .babelrc is in the root of it's directory, and I need to specify a path above it so I can access Project_B.

I've tried several things using cwd and root, but no luck.

Please help. Thanks!

root --Project_A ----src ------components --Project B ----src

Drecula avatar Apr 12 '18 01:04 Drecula

Hi !

I am having the same issue here. I assumed option "cwd" was supposed to resolve this, but either it doesn't work, or I am doing something wrong.

I have a project "A", in which I have aliases.

// .babelrc in project A
{
  "plugins": [
    "transform-es2015-modules-commonjs",
    "transform-class-properties",
    ["transform-object-rest-spread", { "useBuiltIns": true }],
    ["module-resolver", {
      "cwd": "babelrc",
      "root": ["./"],
      "alias": {
        "@project-a-alias": "./a-directory-from-project-a",
      }
    }]
  ]
}

Now it works fine in project A. But I have a project B, which uses project A as a dependance.

// .babelrc in project B
// the ignore allows me to use babel on project A since it's in ./node_modules/project-a
{
  "ignore": "node_modules(?!\/project-a)",
  "plugins": [
    "transform-es2015-modules-commonjs",
    "transform-class-properties",
    ["transform-object-rest-spread", { "useBuiltIns": true }],
    ["module-resolver", {
      "cwd": "babelrc",
      "root": ["./"],
      "alias": {
        "@project-b-alias": "./a-directory-from-project-b",
      }
    }]
  ]
}

When I import a file from project A in project B, the aliases from project A are not working.

The error i'm receiving :

ERROR in ./node_modules/project-a/path/to/some/file.js
Module not found: Error: Can't resolve '@project-a-alias/path/to/other/file' in 'D:\project-b\node_modules\project-a\path\to\some\file'

Is this a bug, or am i doing something wrong ?

rgranger avatar May 16 '18 10:05 rgranger

In general it seems like Babel can't apply the transform properly - probably the other config is not taken into consideration at all. I'd recommend putting everything into one .babelrc file, otherwise I'm not sure what the behavior will be. It's more about babel than about the plugin itself.

fatfisz avatar May 21 '18 19:05 fatfisz

Do you have a babelrc file in ./node_modules/project-a?

tleunen avatar Jul 31 '18 15:07 tleunen