parcel: Simple tilde resolution not working for me
π bug report
Simple tilde paths as described under getting started donβt seem to be resolving.
π€ Expected Behavior
A repo of the error. My folder structure:
node_modules
xhook
dist
xhook.js
folder1
index.html
index.html
package.json
And both index.html files have:
<html>
<body>
<script src="~/node_modules/xhook/dist/xhook.js"></script>
</body>
</html>
When I run parcel folder1/index.html it should build.
π― Current Behavior
parcel index.html works fine but parcel folder1/index.html returns:
D:\dev\parcel-test\folder1\node_modules\xhook\dist\xhook.js: ENOENT: no such file or directory, open 'D:\dev\parcel-test\folder1\node_modules\xhook\dist\xhook.js'
It seems to be treating the tilde as a relative path rather than looking for the folder containing node_modules.
π» Code Sample
π Your Environment
| Software | Version(s) |
|---|---|
| Parcel | 1.12.2 |
| Node | 11.12.0 |
| npm/Yarn | npm 6.7.0 |
| Operating System | Windows 10 |
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 6
- Comments: 34 (11 by maintainers)
The code does appear like itβs set up how itβs supposed to be, but
~is also not working for me as described in the above issue report.~should resolve to the package root (closest node_modules), but it is resolving to the entry point, not the package location. As soon as I changed my entry point from./index.htmlto./src/index.html, my~references broke, and I had to change them to relative folders with...Needing to add
./srcwas pretty much the nature of the bug/missing feature.It should take the root from the TypeScript config
pathsfield. Not sure if they finally fixed this; due to this and a few other problems I had with Parcel I gave up and switched tovite: https://vitejs.dev/With Vite it all works if you import the
vite-tsconfig-pathsplugin. Vite is also crazy faster than Parcel because it usesesbuild. But some TypeScript features arenβt supported. YMMV.Parcel 2 is broken with respect to TypeScript tilde paths.
In the Parcel docs it tells us to use
~to refer to files relative to thesrcfolder. See thistsconfig.jsonfrom the example:And this from the gist:
import { Reset } from '~/ui/shared/Reset'No
srcpath in the example β and in Parcel 1 it worked.Apparently Parcel 2 is using
~as a relative path to thepackage.jsonfolder? That profoundly breaks all TypeScript code which depends on the original example. The original approach is frankly a more sane assumption, too; otherwise youβre addingsrcredundantly to all imports.Understood. But the sane approach includes let us define what the root folder should be. Donβt just pick one arbitrarily; let us have a say in it.
And let us pick
srcin the second example, even thoughindex.htmlis outside ofsrc. In fact, the best case would be to let us pick an array of root folders, and just search for every~file sequentially in each root.Donβt break hundreds of lines of existing code because of some arbitrary new rule youβd like to add. Make the feature actually useful.
Even better: Respect the
pathsentry from the TypeScript file instead of relying on a hack.Still not working for me in latest version (alpha 2.3). TS compiles fine, Visual Studio autocompletes fine, parcel says path doesnβt exist.
tsconfig:
Folder structure:
Command:
parcel src/index.htmlorparcel ./src/index.html.Error:
@parcel/resolver-default: Cannot find module '~/config/Config' from 'C:\project-root\src\components\sidebar'.I also get
Build failed. Error: Got unexpected undefined.sometimes when building, after I clear the cache this undefined error goes away.I also have to note that in Parcel v1 this worked as expected.
An alternative to that symlink is using
alias.Like the
local-moduleentry here: https://v2.parceljs.org/features/module-resolution#aliasesIn your case,
"alias": {"static": "./static"}should work (in a package.json next tosrc)Merging this into https://github.com/parcel-bundler/parcel/issues/202.
The original issue post and the most +1βed comment say
which is already the case in Parcel 2.
WIP docs: https://parcel2-docs.now.sh/plugin-system/resolver/
default resolver plugin: https://github.com/parcel-bundler/parcel/blob/f65aa4c53ab130debfc88f4628eb001f52fb5f63/packages/resolvers/default/src/DefaultResolver.js
default resolver implementation: https://github.com/parcel-bundler/parcel/blob/f65aa4c53ab130debfc88f4628eb001f52fb5f63/packages/utils/node-resolver-core/src/NodeResolver.js
Sure
All of that is typescript specific. Parcel has supported tilde resolution for literally years, and it applies the same way across all file types. TS doesnβt get to dictate how all of Parcel works.
As @mischnic said, Parcel 2 supports custom resolver plugins, and these can be chained. So we could easily support a
@parcel/resolver-typescriptpackage for better TS support, which could override the default resolver for TypeScript files.The problem with Parcel 1βs
~is that was never really wassrcbut rather the nearest parent folder of all entries:@DeMoorJasper I am on the absolute latest version, but I can see when I get home and give you more information.
@YassienW this is broken in Parcel 2