developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.07k stars 363 forks source link

Alias are specific only? #1063

Closed gabsdotco closed 1 year ago

gabsdotco commented 1 year ago

Hey there,

I'm currently running a Firebase Functions project using Microbundle and I'm changing all my relative imports to absolute imports, using the @/* pattern.

I saw that Microbundle has an --alias flag, but it only works when I define a specific path alias, like microbundle build ... --alias @/loaders/firebase=$PWD/src/loaders/firebase, it doesn't see to work with an alias like --alias @/=$PWD/src or --alias @/*=$PWD/src/*. Am I missing something?

My current code with the absolute import:

import { firestore } from '@/loaders/firebase';

export const myFunction = () => {
  // Some code
}

As I said it only works with --alias @/loaders/firebase=$PWD/src/loaders/firebase, all the other alias show the following error message:

Failed to resolve the module @/loaders/firebase imported by src/utilities/validation/organisation/index.js

I would like to set only one alias that can point to all folders within ./src.

rschristian commented 1 year ago

You're not missing anything, this is the correct behavior. The alias option is meant for aliasing specific modules, such as react -> preact/compat, not as a mechanism to support absolute imports (and I'd highly recommend against them anyhow).

You could use Microbundle's JS API if you really wanted to do this, but for now, this is working as intended.

gabsdotco commented 1 year ago

Hey @rschristian, thanks for the answer. I managed to solve it by using Babel's Module Resolver plugin, now I can define aliases on babel.config.js and everything works great.

Cheers!