'Generating CSS modules for React components in Bit

We started sharing a number of React components in our SPA as Bit.dev components and ran into an issue: can't use our components on Bit's dashboard due to CSS-modules as Bit's build process does not create them. We use Bit 0.0.687. Use "bit start" to launch Bit dashboard or "bit export" to publish the components to a remote scope, then open a remote dashboard. Our components which use: import style from './style.css' and relay on a CSS module get undefined "style" under Bit. May someone tell, please, if there is a way to alter Bit's build process to generate CSS modules? In our Application's Webpack build we use:

module: {
  rules: [
    {
      test: /\.css$/i,
      include: /src/,
      exclude: /node_modules/,
      use: [
        {
          loader: require.resolve('style-loader', {
            paths: [require.resolve('webpack-config-single-spa')],
          }),
        },
        {
          loader: require.resolve('css-loader', {
            paths: [require.resolve('webpack-config-single-spa')],
          }),
          options: {
            importLoaders: 1,
            sourceMap: true,
            modules: {
              localIdentName: '[name]__[local]___[hash:base64:5]',
            },
          },
        },
        {
          loader: 'postcss-loader',
          options: {
            sourceMap: true,
          },
        },
      ],
    }
  ]}

and Webpack 5.x.



Solution 1:[1]

As Bit uses CRA under the hood, renaming CSS files to style.module.css helps. CRA recognizes "module" suffix and compiles these CSS as modules.

Solution 2:[2]

Complementary to at54321's solution, you don't actually have to duplicate the struct itself, instead you can use flattening and / or conditional serialisation skipping e.g.

#[derive(Serialize)]
struct User {
    id: String,
    name: String,
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    _hr: Option<HrView>,
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    _admin: Option<AdminView>
}

#[derive(Serialize)]
struct HrView {
    salary: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    boss: Option<Box<User>>
}

#[derive(Serialize)]
struct AdminView {
    password: String,
    rights: Rights,
}

#[derive(Serialize)]
struct Rights {
    create: bool,
    update: bool
}

demo: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=09fd30ce1f013514d0dc18cf6720f80b

Solution 3:[3]

The most commonly used JSON crate in Rust is serde. One way you can achieve what you want is to create different Values, depending on user's role (by using the json! macro or by manually creating Value instances, for example). Another is to use enums with 3 variants - one variant for each user role (see Enum representations).


As a side note, I couldn't help but notice you are returning passwords in your JSON. That really looks like a serious security problem and I can hardly think of situations where I would want to return even encoded passwords to the client. Normally, passwords only go "one way": from the client to the server.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexf2
Solution 2 Masklinn
Solution 3