Skip to content

Support for getLocalIdent #7

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

Merged
merged 7 commits into from
Sep 22, 2020
Merged

Conversation

micantoine
Copy link
Owner

@micantoine micantoine commented Sep 21, 2020

Add option to generate custom classname, issue #6

...
 preprocess: [
   cssModules({
     getLocalIdent: (
       { context, resourcePath }, // context
       { template, interpolatedName }, // localIdentName
       className, // css classname
       { markup, style } // content
     ) => {
       return 'whatever-classname';
     }
   })
 ],
...

Example

# Directory
SvelteApp
└─ src
   ├─ App.svelte
   └─ components
      └─ Button.svelte
<!-- Button.svelte -->
<script>
  export let label = 'Send';
</script>

<button class="$style.red">{label}</button>

<style>
  .red { background-color: red; }
</style>
// Preprocess config
...
 preprocess: [
   cssModules({
     localIdentName: '[path][name]__[local]',
     getLocalIdent: (
       { context, resourcePath }, localIdentName, className, { style }
     ) => {
       console.log(context); // SvelteApp/src/components
       console.log(resourcePath); // SvelteApp/src/components/Button.svelte

       console.log(localIdentName);
       // {
       //    template: '[path][name]__[local]',
       //    interpolatedName: 'SvelteApp_src_components_Button__red',
       // } 

       console.log(className); // red
       console.log(style); // <style>.red { background-color: red; }</style>
       
       const customClass = localIdentName.interpolatedName.toLowerCase().replace('src_', '');
       return customClass; // svelteapp_components_button__red;
     }
   })
 ],
...

@micantoine micantoine linked an issue Sep 21, 2020 that may be closed by this pull request
@micantoine micantoine changed the title Support for getLocalIdentName Support for getLocalIdent Sep 22, 2020
@micantoine micantoine merged commit 84ba05c into master Sep 22, 2020
@micantoine micantoine deleted the 6-support-for-getlocalident branch September 22, 2020 03:12
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.

Support for getLocalIdent
1 participant