How does import with tilde (~) work in parcel-2?

1.6k views Asked by At

In parcel 1, I was able to import with tilde (~) which usually refers to the nearest directory or the root directory of the project. But, I recently upgraded to parcel 2 and it's complaining. How do I enable the ~ path resolvers in a typescript/react repo ? Do I need to write a resolver for that or is there any build-in support ? Is there any example for this setup ?

3

There are 3 answers

0
Johansrk On

works fine here. this is my package.json

{
  "name": "my-pacakge",
  "version": "1.0.9-alpha",
  "description": "something",
  "scripts": {
    "start": "parcel index.html --open",
    "build": "parcel build --public-url . index.html",
    "pack": "npm-pack-zip"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {
    "bootstrap": "^5.0.2",
    "npm-pack-zip": "^1.2.9",
    "parcel": "^2.0.1"
  },
  "peerDependencies": {
    "@parcel/transformer-sass": "^2.0.1"
  }
}
0
Taitu-lism On

I'm afraid currently that can't be done (July 2022, parcel v2.6.2) even though the official docs say you can. I followed the docs, it didn't work. VS Code respects my tsconfig and resolves tilde imports as expected but the parcel command complains in the terminal.

It looks like a known issue (and another related issue)

Solution?

There's a plugin (parcel-resolver-tspaths) that supposedly solves this issue but I haven't tried it yet.

See also a short blog post about this issue

0
Fateh Mohamed On

try to add this to your tsconfig.json

...
"baseUrl": "./src",
"paths": {
  "~/*": ["./*"]
},
...