Skip to content

Resolve package exports for SCSS imports without the pkg: prefix - #528

Open
Eugene Ledenev (ledeneveugene) wants to merge 1 commit into
microsoft:mainfrom
ledeneveugene:feat/exports-without-pkg-prefix
Open

Eugene Ledenev (ledeneveugene) wants to merge 1 commit into
microsoft:mainfrom
ledeneveugene:feat/exports-without-pkg-prefix

Conversation

@ledeneveugene

Copy link
Copy Markdown

The exports field of package.json is read only for pkg: imports. Bundlers such as Vite and webpack apply exports to plain module imports as well, so a lot of projects write imports like this and get working builds:

@use '@scope/ui-kit/static/styles/theme';

where the package publishes the file in a build folder:

"exports": {
  "./static/*": "./dist/static/*"
}

The import is resolved to node_modules/@scope/ui-kit/static/styles/theme, which does not exist. Go to definition and hover find nothing, and following the document link offers to create the file. importAliases doesn't help, because the node_modules lookup returns that missing path before the setting is checked.

Changes

In scssNavigation.ts:

  • For module paths without a scheme, a relative file still wins. If there is none, exports of the package in node_modules is looked up and the result is used when the file exists. Otherwise the previous lookup runs unchanged.
  • An export target can be a plain string, not only an object with sass, style or default conditions.
  • A target without a file extension, such as ./dist/static/*, is resolved the way Sass resolves an import: .scss, partials and index files. Targets like ./index.js still don't match.
  • Subpath patterns are matched against the whole subpath, and the pattern with the longest prefix wins, as in Node. Before, the regular expression had no anchors, so a pattern could match part of an unrelated subpath.
  • resolvePkgModulePath is renamed to resolvePackageExports, since pkg: imports and plain imports share it now.

The string target, the extension handling and the pattern matching apply to pkg: imports too.

Tests

The new fixture test/linksTestFixtures/node_modules/@foo/build-exports maps "./static/*": "./build/static/*" and contains a partial, a nested folder and a "./node/*": "./build/node/*.js" entry that is not a stylesheet. Unlike the other fixtures it ships real files, because the new code path checks that the resolved file exists.

The new test covers imports with and without an extension, a partial, a nested path, a conditional export and the root export without the prefix, a non-stylesheet target that still falls back to the plain node_modules path, and a pkg: import that goes through a string subpath pattern. It fails on main.

On Node 24.x, npm test passes compilation, 353 tests and 7 lint checks; main passes 352 tests and the same 7 lint checks. git diff --check is clean.

No dependency or language service API changes.

Bundlers such as Vite and webpack apply the `exports` field of
`package.json` to plain module imports, not only to `pkg:` imports. When
a package maps `"./static/*": "./build/static/*"`, the import string does
not match the file system, so document links, hover and go to definition
resolve to a path that does not exist.

Look up `exports` for bare module paths before falling back to a plain
`node_modules` path. A relative file still takes precedence, like in the
Sass compiler, and the `exports` result is only used when the file
exists, so existing resolution is unchanged when no entry matches.

Along the way `resolvePkgModulePath` (now `resolvePackageExports`, since
both kinds of import share it) learns three things that the case above
needs, and that also apply to `pkg:` imports:

- an export target can be a plain string, not only an object of
  conditions
- a target without a file extension, such as `./build/static/*`, is
  resolved the way Sass resolves an import: `.scss`, partials, index
  files. Targets like `./index.js` still do not match
- subpath patterns are matched against the whole subpath and the pattern
  with the longest prefix wins, as in Node. The regular expression had no
  anchors before
@ledeneveugene

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Export pattern selection still returns incorrect destinations

Two package-resolution cases are reproducible through the public navigation API:

1. Equal-prefix patterns need suffix precedence. With these entries in this order and both target files present:

{"exports":{"./styles/*":{"sass":"./general/*.scss"},"./styles/*-theme":{"sass":"./special/*.scss"}}}

@use "pkg/styles/a-theme" resolves to general/a-theme.scss; the more specific pattern should select special/a.scss. Sorting by prefix length alone leaves these entries in insertion order. Node's pattern comparator also considers the remaining pattern length. Location: src/services/scssNavigation.ts around lines 116-118.

2. A matching excluded export must not fall through. Given:

{"exports":{"./styles/private/*":null,"./styles/*":{"sass":"./general/*.scss"}}}

@use "pkg/styles/private/a" resolves to general/private/a.scss when that file exists, despite the matching explicit exclusion. After selecting the more specific null entry, resolution should remain blocked rather than continue to the broader pattern. Location: around lines 128-132.

Please add navigation fixtures for both cases and correct pattern selection/exclusion handling. Attribution matters: equal-prefix ordering is already wrong for pkg: imports on the base, and the base crashes on the null case; this PR improves the crash but extends incorrect successful resolution to newly supported bare imports. These are not both new regressions in existing pkg: behavior.

Reviewed commit: 1a94c0e.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants