Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for import subpath patterns #7840

Closed
rsimp opened this issue Mar 17, 2022 · 7 comments
Closed

Add support for import subpath patterns #7840

rsimp opened this issue Mar 17, 2022 · 7 comments

Comments

@rsimp
Copy link

rsimp commented Mar 17, 2022

🙋 feature request

Add support for import subpath patterns for framework agnostic aliasing

🤔 Expected Behavior

With the following in package.json:

{
  "imports": {
    "#hooks/*": "./src/common/hooks/*",
  }
}

I should be able to import like the following:

import useHook from '#hooks/useHook';

😯 Current Behavior

Parcel already supports glob aliases to achieve the same thing. However import subpath patterns are a standard supported natively in node.

💁 Possible Solution

Implement code in dependency resolution to support imports in package.json. One part of the spec that helps is that imports entries MUST start with # in order to work. If an import starts with '#' then Parcel should check package.json for the imports section before resolving with it's own alias configuration. Webpack uses this same method, or rather it only will resolve '#' imports with package.json.

🔦 Context

While parcel aliasing is useful it's specific to Parcel. Jest, express, other bundlers, typescript all need their own specific configuration to support the same code. Webpack, express, build scripts, and top level jest imports all already support import subpath patterns. Jest mocking is waiting on a third party dependency to get support for mocking apis as well. It would be nice if typescript/vs code supported it as well, but as more packages implement the standard it puts more pressure on other packages to adopt.

Even in its current state I can at least get framework agnostic support for Parcel AND for node.js backend/scripts just like I can with Webpack.

@rsimp
Copy link
Author

rsimp commented Mar 17, 2022

For more information, webpack achieves this using their enhanced-resolve package. The issue for adding the imports field is: webpack/enhanced-resolve#290

@rsimp
Copy link
Author

rsimp commented Mar 17, 2022

Really this feature request should be for supporting the entire imports field specification. Not just subpath patterns

@rsimp
Copy link
Author

rsimp commented Mar 17, 2022

Also esbuild and rollup support import subpath patterns as well

@mischnic
Copy link
Member

Will be fixed by #8807 in the next release

@cybergrunge
Copy link

Well, it is not. (^2.9.1)

@parcel/core: Failed to resolve '#utils/logger.js' from './src/index.js'

@devongovett
Copy link
Member

Please note that exports/imports support is currently opt-in to avoid breaking changes. To enable it, add the following to your project root package.json:

{
  "@parcel/resolver-default": {
    "packageExports": true
  }
}

See the docs for more details: https://parceljs.org/features/dependency-resolution/#package-exports

@cybergrunge
Copy link

Thanks, and apologies, I only found this issue when searching for informations about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants