Skip to content

postcss-bundler: conditional style exports support #1543

New issue

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

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

Already on GitHub? # to your account

Closed
3 tasks done
pascalduez opened this issue Dec 23, 2024 · 2 comments
Closed
3 tasks done

postcss-bundler: conditional style exports support #1543

pascalduez opened this issue Dec 23, 2024 · 2 comments
Labels
feature request New feature or request

Comments

@pascalduez
Copy link
Contributor

pascalduez commented Dec 23, 2024

What would you want to propose?

Greetings,

I'm currently trying to use postcss-bundler to import styles coming from a package declaring entry points and conditional exports: https://nodejs.org/api/packages.html#conditional-exports

The package exports:

"name": "@org/tokens",
"type": "module",
"main": "./dist/tokens.cjs",
"module": "./dist/tokens.js",
"style": "./dist/tokens.css",
"exports": {
  ".": {
    "import": "./dist/tokens.js",
    "require": "./dist/tokens.cjs",
    "types": "./dist/tokens.d.ts",
    "sass": "./dist/tokens.scss",
    "style": "./dist/tokens.css"
  },
},

The CSS file importing from the package:

@import url('node_modules:@org/tokens') layer(base);

Currently resulting in a CSSSyntaxError because it picks up the commonjs file ./dist/tokens.cjs.
I understand though that the style condition is not yet "standardized"?
Sass already honor the sass one though.

Suggested solution

Not exactly sure yet, will dive more into the code. Happy to get advises.
But given that postcss-bundler is aimed at CSS, supporting the style condition seems like a good addition.
Alternatively just add support for the style key at package.json root. (like postcss-import).

Additional context

Some references:

Validations

  • Follow our Code of Conduct
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

Would you like to open a PR for this feature?

  • I'm willing to open a PR
@pascalduez pascalduez added the feature request New feature or request label Dec 23, 2024
@romainmenke
Copy link
Member

romainmenke commented Dec 23, 2024

Hi @pascalduez,

This has to be added upstream in node.
Re-implementing node require purely for this feature is beyond the scope of postcss-bundler. Re-implementing node require from scratch is needed because node does not expose the building blocks needed to override these conditions (as far as I know, please correct me if this does exist).

Conditional exports work fine and are supported but only in so far as node itself supports these. And as you say the style condition isn't supported yet in node.

You can however write:

"type": "module",
"main": "dist/tokens.cjs",
"module": "dist/tokens.js",
"style": "./dist/tokens.css",
"exports": {
  ".": {
    "import": "./dist/tokens.js",
    "require": "./dist/tokens.cjs",
    "types": "./dist/tokens.d.ts",
    "sass": "./dist/tokens.scss"
  },
  "./css": {
    "default": "./dist/tokens.css"
  }
},

Allowing you to write: @import url('node_modules:@org/tokens/css') layer(base);


Edit:

It might be worth it if you opened an issue in node to detail your use case.
This might accelerate the adoption of the style condition.

@romainmenke romainmenke closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2024
@romainmenke
Copy link
Member

This issue also seems relevant: nodejs/node#51855
If that does ever ship I will update postcss-bundler to make use of the proposed features.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants