-
Notifications
You must be signed in to change notification settings - Fork 115
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
feature request: support conditional exports (package.json exports
field)
#492
Comments
The module we use for resolution doesn't support Presumably, we'd want to support some kind of style-specific conditional export as well, so you could export both JS and CSS at |
No sir, not that I know of, hence the conflict. Regarding "JS and CSS at same import", I would not recommend that! You can import from "." with postcss-import by using the nonstandard "style" field; to use exports, you must pick one. Pure CSS modules could export at ".", and normal JS modules could simply export at "/styles" (which is what I do). /* NBD! */
@import "my-package/styles" |
I was also looking for this just now. I assumed it would be finding exports (example of how the popular slider component Swiper does it: https://github.com/nolimits4web/swiper/blob/e54f07d6047f56e0469fd7d3024ede36e5b942fe/src/copy/package.json) |
Ideally the identifier would just be run through native Node resolution to get the entry point and prefer the This way the specifiers are "true to form", and |
Also relevant : nodejs/node#46994 |
This can surely be improved upon, but it was enough to have postcss-import find self-referenced subpath exports ( resolve: async (id, baseDir) => {
const manifest = await readPackageUp({ cwd: baseDir });
if (manifest === undefined) {
throw new Error(`Failed to find manifest for "${baseDir}"`);
}
const resolved = resolve(manifest.packageJson, id, {
conditions: ['style'],
});
if (resolved === undefined) {
throw new Error(
`Failed to find subpath import or export pattern matching "${id}"`,
);
}
const [relative, ...rest] = resolved;
if (relative === undefined) {
throw new Error(
`Failed to find subpath import or export pattern matching "${id}"`,
);
}
if (rest.length > 0) {
throw new Error(
`Found multiple subpath import or export patterns matching "${id}"`,
);
}
const absolute = new URL(relative, `file://${manifest.path}`).pathname;
// The above will build a relative and then an absolute URL for any
// import pattern, regardless of whether or not it's compatible with the
// manifest (e.g. manifest name "foo" and absolute import from "bar").
// We'll workaround this in a slightly hacky way by just checking if the
// resolved path exists or not. This isn't airtight but we're unlikely
// to hit a false positive.
if (!fs.existsSync(absolute)) return id;
return absolute;
}, Uses read-package-up and resolve.exports. |
Love the
style
support, but would really helpful for conditional exports to also be legible topostcss-import
:The main reason I suggest this is because I cannot specify an
@import "my-package/styles"
statement in a way that is supported by both esbuild (supports standardized package.jsonexports
, but not nonstandardstyle
) andpostcss-import
(where situation is reversed).my-package/styles
is the only thing that works for esbuild, butmy-package
is the only thing that works forpostcss-import
.I would recommend following the Node doctrine of "if
exports
exists, it overrides anything else" (i.e.style
field). Also thank you for maintaining this project, it is a core part of modern development workflows IMO, only reason I suggest the enhancement.The text was updated successfully, but these errors were encountered: