vite-css-modules icon indicating copy to clipboard operation
vite-css-modules copied to clipboard

Default classname is ok in default export mode, not named mode

Open mrginglymus opened this issue 11 months ago • 3 comments

I have a class named default, and I'm using default exports mode. I'm getting a lot of confusing warnings from this plugin, which claims it's problematic, but it works, and I can't see how:

const $mangled = '.default-abc123';

export default {
  default: $mangled;
}

is ok, and so shouldn't emit a warning.

Similarly, if they're composed, it shouldn't be an issue at all as we don't export the default named class by name.

Only lightningcss seems to drop the default class in composed modules; both postcss and lightningcss incorrectly export the default class as the default export of the module, as evidenced by the tests in the first PR of this commit. Both, however, do correctly emit the default class in the default export object.

mrginglymus avatar Jan 25 '25 19:01 mrginglymus

I don't know why lightningcss isn't happy with composed classes called default...in my usage it's been ok and I can't see anything in this plugin that's mangling it. Will take a nosey later

mrginglymus avatar Jan 25 '25 19:01 mrginglymus

As far as I can see this is a bug in lightningcss.

Raised https://github.com/parcel-bundler/lightningcss/issues/908

mrginglymus avatar Feb 04 '25 11:02 mrginglymus

It looks like lightningcss is interpreting the value of compses as a css custom ident, and thus forbidding names like default.

mrginglymus avatar Feb 04 '25 13:02 mrginglymus

I think the conflict only occurs when the export mode is both

privatenumber avatar Jun 25 '25 03:06 privatenumber

As the logic currently stands, using exportMode 'named' omits classes named 'default' - if I change to assert for their presence it fails.

JestAssertionError: expect(received).toMatchObject(expected)

- Expected  - 1
+ Received  + 0

  Object {
    "style": Object {
-     "default": "_default_1733f38",
      "typeof": "_typeof_06003d4",
    },
  }
    at <anonymous> (/home/bill/git/vite-css-modules/tests/specs/patched/postcss.spec.ts:1012:22)
    at async file:///home/bill/git/vite-css-modules/node_modules/.pnpm/[email protected]/node_modules/manten/dist/index.mjs:2:1723
    at async J (file:///home/bill/git/vite-css-modules/node_modules/.pnpm/[email protected]/node_modules/manten/dist/index.mjs:2:1323)
    at async K (file:///home/bill/git/vite-css-modules/node_modules/.pnpm/[email protected]/node_modules/manten/dist/index.mjs:2:1666)
✖ vite 6.3.2 › Patched › PostCSS › default as named export › should omit in named exports (1.7s)

I can't remember whether the logic for this is in this plugin or in postcss/lightningcss, but as things stand only "default" mode supports "default" named classes

mrginglymus avatar Jun 25 '25 07:06 mrginglymus

Ah, found the offending line and fixed.

mrginglymus avatar Jun 25 '25 07:06 mrginglymus

:tada: This PR is included in version 1.9.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

privatenumber avatar Jun 27 '25 03:06 privatenumber