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

Feature/generate scoped name from config file #7

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ module.exports = {

This will load all files with `.mod.css` as CSS modules and load all other files as regular CSS. Notice that the function will only be called for whichever regex you provided in the `transform` option of the Jest config.

Also supports `generateScopedName` property to customize the generated class names. Helpful when using Jest Snapshots and not wanting unnecessary noise from hash generated classnames.

```js
// jesttransformcss.config.js

module.exports = {
modules: true,
generateScopedName: "[path]_[name]_[local]"
// Default value is: '[path][local]-[hash:base64:10]'
};
```
Link to all available [placeholder tokens](https://github.com/webpack/loader-utils#interpolatename) \*Note not all placeholders are working and must be tested.

## Further setup

There are many ways to set up styles in a project (CSS modules, global styles, external global styles, local global styles, CSS in JS, LESS, SASS just to name a few). How to continue from here depends on your project.
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ module.exports = {
require("${postcssRunner}")(
${JSON.stringify({
src,
filename
// config,
filename,
transformConfig: transformConfig.config
// options
})}
)
Expand Down
13 changes: 7 additions & 6 deletions postcss-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const cssModules = require("postcss-modules");

// This script is essentially a PostCSS Runner
// https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#postcss-runner-guidelines
module.exports = ({ src, filename }) => {
module.exports = ({ src, filename, transformConfig }) => {
const ctx = {
// Not sure whether the map is useful or not.
// Disabled for now. We can always enable it once it becomes clear.
Expand Down Expand Up @@ -34,11 +34,12 @@ module.exports = ({ src, filename }) => {
.then(({ plugins, options }) => {
return postcss([
cssModules({
// Should we read generateScopedName from options?
// Does anybody care about the actual names? This is test-only anyways?
// Should be easy to add in case anybody needs it, just pass it through
// from jest.config.js (we have "config" & "options" in css.js)
generateScopedName: "[path][local]-[hash:base64:10]",
// Add 'generateScopedName' property to 'jesttransformcss.config.js' for custom name generation.
// List of available placeholder tokens: https://github.com/webpack/loader-utils#interpolatename
// *Note some placeholder tokens appear to not be working
generateScopedName:
transformConfig && transformConfig.generateScopedName ||
"[path][local]-[hash:base64:10]",
getJSON: (cssFileName, exportedTokens, outputFileName) => {
tokens = exportedTokens;
}
Expand Down