question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support regex in alias

See original GitHub issue

πŸ™‹ feature request

Will Parcel support regex in alias?

πŸ€” Expected Behavior

Having the project structure such this:

❯ tree
.
β”œβ”€β”€ main.js
β”œβ”€β”€ modules
β”‚   └── lazyload
β”‚       └── index.js
└── utilities
    β”œβ”€β”€ filters.js
    β”œβ”€β”€ helpers
    β”‚   └── index.js
    β”œβ”€β”€ selectors
    β”‚   └── index.js
    └── utils.js

5 directories, 6 files

Parcel should be able to use regex in import alias, such:

alias: {
    '@modules': './src/scripts/modules',
    '@utilities': './src/scripts/utilities',
  },

😯 Current Behavior

Currently, we have to specify explicitly each alias:

"alias": {
    "@modules/lazyload": "./src/scripts/modules/lazyload/index.js",
    "@utilities/selectors": "./src/scripts/utilities/selectors/index.js",
    "@utilities/helpers": "./src/scripts/utilities/helpers/index.js"
  },

πŸ’ Possible Solution

How other bundler approaches the problem. In other Bundler, I just need to specify part of alias (only top-level modules) :

Webpack:

  alias: {
        "@utilities": path.resolve(__dirname, "src/site/includes/js/utilities"),
        "@modules": path.resolve(__dirname, "src/site/includes/js/modules"),
      },

Snowpack:

alias: {
    '@modules': './src/scripts/modules',
    '@utilities': './src/scripts/utilities',
  },

esbuild:

// Redirect all paths starting with "@modules/" to "src/scripts/modules"
    build.onResolve({ filter: /^@modules\// }, (args) => {
      // get such `@modules/mobile-nav` without the `@module` part
      let moduleName = args.path.split("/")[1]
      return { path: path.resolve("src/scripts/modules", moduleName, "index.js") }
    })

    build.onResolve({ filter: /^@utilities\// }, (args) => {
      let moduleName = args.path.split("/")[1]
      return { path: path.resolve("src/scripts/utilities", moduleName, "index.js") }
    })

πŸ”¦ Context

Imagine having more than 10 import to the different directory with the alias. In Parcel we need to specify explicitly 10 alias in package.json to concrete file instead of using regex.

"parcel": "^2.0.0-beta.3.1",

Related: https://github.com/parcel-bundler/parcel/discussions/6390

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mischniccommented, Jun 2, 2021

We should probably add that to the docs.

I still think we should support '@modules': './src/scripts/modules' for cross-compatibility reasons. Aliasing a folder to another folder should already work and this is just that one special case of mapping the org part of a package name.

1reaction
DeMoorJaspercommented, Aug 30, 2021

@purp1eeeee using ~/ might be the issue as that is already handled by parcel itself, probably better to use something like @/ instead

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to use regex in alias? - Unix & Linux Stack Exchange
I have created it, but it is not working. Is there any syntax error in this command? bash Β· regular-expression Β· alias.
Read more >
Can we use regex in alias command? - bash - Super User
No you can't use regex in alias command. To achieve what you want, in Bash, you can use the command_not_found_handle .
Read more >
How to use regex in bash aliases - Stack Overflow
No. Aliases run simple prefix substitution, and aren't powerful enough for much else. However, in Bash 4, you can use a function calledΒ ......
Read more >
Alias with regex - Server Fault
Solved it using regex in the virtual alias table in postfix' /etc/postfix/main.cf virtual_alias_maps = regexp:/etc/postfix/virtual. and then
Read more >
Regular expression (regex) reference - Pexip Infinity Docs
Character Description Example . Matches any single character. a.c matches aac, abc, azc, a2c, a$c e... \d Matches a decimal digit character (i.e. 0‑9). a\d...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found