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:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.@purp1eeeee using
~/
might be the issue as that is already handled by parcel itself, probably better to use something like@/
instead