Skip to content

Alias resolves to wrong alias when used inside aliased code #96

@Mythli

Description

@Mythli

The behavior I have observed is that when I use an alias inside of another alias one path backward is somehow skimmed in my folder structure.

The same works with webpack which I use on the client side.

Babelrc:

{
  "presets": ["es2015", "stage-0"],

  "plugins": [
    ["module-resolver",
      {
        "root": [
          "./src"
        ],
        "alias": {
          "mythil": "./build/lib/mythil.js/src",
          "libbehoerdenglueck": "./build/lib/libbehoerdenglueck/src"
        }
      }]
  ]
}

Folder structure:

lib
├── libbehoerdenglueck
│   ├── data
│   │   └── ...
│   └── src
│       ├── constants.js
│       ├── hamburg.js
│       └── visit.js
└── mythil.js
    ├── COMMANDS.md
    └── src
        ├── array.js
        ├── dom.js
        ├── md5obj.js
        ├── number.js
        ├── object.js
        ├── string.js
        ├── time.js
        ├── util.js
        └── validation.js

Files are compiled into the build directory using this command:
./node_modules/.bin/babel src -d build && ./node_modules/.bin/babel lib -d build/lib && node build/index.js

The build dir then looks like this:

build
├── NetAppointmentApi.js
├── dump-pouch.js
├── dump.js
├── index.js
├── lib
│   ├── libbehoerdenglueck
│   │   └── src
│   │       ├── constants.js
│   │       ├── hamburg.js
│   │       └── visit.js
│   └── mythil.js
│       └── src
│           ├── array.js
│           ├── dom.js
│           ├── md5obj.js
│           ├── number.js
│           ├── object.js
│           ├── string.js
│           ├── time.js
│           ├── util.js
│           └── validation.js

When running the command compiling with babel works fine. However running the code encounters a module resolve error:

Error: Cannot find module '../../../build/lib/mythil.js/src/time'
    at Function.Module._resolveFilename (module.js:438:15)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/tobiasanhalt/Development/behoerdenglueck/build/lib/libbehoerdenglueck/src/constants.js:20:13)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/tobiasanhalt/Development/behoerdenglueck/build/lib/libbehoerdenglueck/src/visit.js:78:18)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)

In constants.js which is accessed through the alias libbehoerdenglueck the following line fails:
import { mToMs, sToMs, dToMs, hToMs } from 'mythil/time';

babel-plugin-module-resolver translates the alias mythil/time to ../../../build/lib/mythil.js/src/timeinstead of the correct ../../../../build/lib/mythil.js/src/time (one path backward is missing).

All that is pretty sad :(
Would love to see replications of the issue and possible workarounds.

Workaround:
find ./build -type f -name '*.js' -exec sed -i '' s@../../../build@../../../../build@ {} +

Best regards
Mythli

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions