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

Should @parcel/resolver-glob support importing from packages? #7945

Closed
marcins opened this issue Apr 13, 2022 · 1 comment · Fixed by #8097
Closed

Should @parcel/resolver-glob support importing from packages? #7945

marcins opened this issue Apr 13, 2022 · 1 comment · Fixed by #8097

Comments

@marcins
Copy link
Contributor

marcins commented Apr 13, 2022

💬 RFC

The glob resolver (@parcel/resolver-glob) only supports a file path as a specifier, and will not do node style resolution if a package is specified.

If the import references a path within a package, should the glob resolver "resolve" the package to a path first? Or should it at least warn or error if the file list the glob resolves to is empty?

🔦 Context

I have a component I'm trying to bundle that currently uses Webpack's dynamic expressions, something like: import(`@scope/pkg/dist/i18n/${locale}`).

As part of migrating to Parcel, I tried replacing this with import('@scope/pkg/dist/i18n/*.js')[locale] however this did not work correctly, as it is assumed the specifier is a path -

specifier = path.resolve(path.dirname(sourceFile), specifier);

In the example above, this doesn't work because the resolver tries to find files in /path/to/my/repo/src/path/@scope/pkg/dist/i18n/*.js (assuming a source path of /path/to/my/repo/src/path/)

Changing the import to a relative path obviously makes it work (i.e. import('../../../node_modules/@scope/pkg/dist/i18n/*.js')) but is potentially fragile if things get moved around, and similar to the case above would fail to find any files silently.

Should the glob resolver resolve any packages to paths first? I could see the argument for either the current behaviour or resolving, so it's not cut and dry in my mind - hence the RFC.

Another potential option, if package resolution is determined to not be appropriate here, would be to at least warn (or error?) if the list of files is empty? Because I didn't realise what the problem was until I debugged the build and stepped through the glob resolver. i.e. after the line below, warn if files.length === 0:

let files = await glob(normalized, options.inputFS, {

@devongovett
Copy link
Member

Seems like a reasonable feature. I suspect it will get more complicated once package.json#exports are supported though (#4155).

As for how to implement it, I guess you'd need to:

  • Detect that it is not an absolute or relative path (i.e. starts with /, ~, or .)
  • Split the module into the module name (i.e. @scope/pkg) and subpath similar to this function:
    getModuleParts(name: string): [FilePath, ?string] {
    .
  • Use @parcel/node-resolver-core to resolve the path of the module name
  • Append the subpath to the module name, and resolve it as a glob.

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

Successfully merging a pull request may close this issue.

2 participants