Skip to content
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

Fix css modules, and JSON import #3770

Closed
wants to merge 1 commit into from
Closed

Fix css modules, and JSON import #3770

wants to merge 1 commit into from

Conversation

procopenco
Copy link

@procopenco procopenco commented Oct 11, 2021

Changes

Fix for #2998
Fix for #3760

In this PR I did a couple of changes to fix development and production environments when CSS modules are used.

  1. First change was related to fix the generated JSON based on CSS module.
    I moved JSON generation after transform plugins are applied, because the .sass, .less or .css (with postcss) should be transformed to CSS, and after that resulted CSS is ready to be transformed to JSON.

  2. Second change is related to fileBuilder. When .js, .jsx, .ts or .tsx file is transformed through fileBuilder for the case when it contains a .module.css import, it requires to add an extra import to the generated JSON. It MUST be part of the final bundle
    Ex. We have import styles from './test.module.css'

    1. import './test.module.css'
    2. import styles from './test.module.css.json'
      Why we need this?
      Both files should be part of final bundle. test.module.css' - will be part of final index.css, and test.module.css.json will be part of final index.js
  3. Third change is to allow esbuild to add JSON files into the final bundle.
    As I said the generated JSON based on *.module.css should be part of the bundle.

Example

test.module.css - I am using postcss with a couple of additional plugins

.block {
  display: block;
  box-sizing: border-box;
}

@each $side in top, right, bottom, left {
  .margin-$(side) {
    margin: 1rem;
  }
}

Before change

test.module.css.json generated JSON which is not included in the final bundle

{"block":"_block_re84t_1","margin-$(side)":"_margin-$(side)_re84t_7"}

content in JS bundle

// build/dist/lib/controls/test/test.module.css
var _default = {};

NOTE: content in CSS bundle - everything is OK

After change

test.module.css.json is included in the final bundle and it looks

// build/dist/lib/controls/test/test.module.css.json
var block = "_block_1jm6y_1";
var margin_top = "_margin-top_1jm6y_6";
var margin_right = "_margin-right_1jm6y_10";
var margin_bottom = "_margin-bottom_1jm6y_14";
var margin_left = "_margin-left_1jm6y_18";
var block_module_css_default = {block, "margin-top": margin_top, "margin-right": margin_right, "margin-bottom": margin_bottom, "margin-left": margin_left};

content from CSS bundle

/* build/dist/lib/controls/test/test.module.css */
._block_1jm6y_1 {
  display: block;
  box-sizing: border-box;
}
._margin-top_1jm6y_6 {
  margin: 1rem;
}
._margin-right_1jm6y_10 {
  margin: 1rem;
}
._margin-bottom_1jm6y_14 {
  margin: 1rem;
}
._margin-left_1jm6y_18 {
  margin: 1rem;
}

Testing

  • Tested locally

Docs

@changeset-bot
Copy link

changeset-bot bot commented Oct 11, 2021

⚠️ No Changeset found

Latest commit: ff710cb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Oct 11, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/2nCRxoNa8LvwvzGYxMyyeEUw4ppq
✅ Preview: https://snowpack-git-fork-maxpro-fun-fix-css-modules-pikapkg.vercel.app

@procopenco
Copy link
Author

@drwpow PTAL

@procopenco procopenco changed the title Fix css modules Fix css modules, and JSON import Oct 11, 2021
@procopenco
Copy link
Author

It seems this repo is dead :(((

@procopenco procopenco closed this Oct 29, 2021
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.

None yet

1 participant