@@ -215,9 +215,10 @@ Force a selector to be scoped within a component to prevent style inheritance on
215215
216216## Import styles from an external stylesheet
217217
218- Alternatively, styles can be created into an external file and imported onto a svelte component from the ` <script> ` tag. The name referring to the import can then be used in the markup targetting any existing classname of the stylesheet.
218+ Alternatively, styles can be created into an external file and imported onto a svelte component from the ` <script> ` tag. The name referring to the import can then be used in the markup targetting any existing classname of the stylesheet.
219219
220- The css file must follow the convention ` FILENAME.module.css ` in order to be processed.
220+ - The option ` parseExternalSylesheet ` need to be enabled.
221+ - The css file must follow the convention ` FILENAME.module.css ` in order to be processed.
221222
222223** Note:** * The import option is only meant for stylesheets relative to the component. You will have to set your own bundler in order to import * node_modules* packages css files.*
223224
@@ -497,27 +498,16 @@ Pass an object of the following properties
497498
498499| Name | Type | Default | Description |
499500| ------------- | ------------- | ------------- | ------------- |
500- | ` mode ` | ` native\|mixed\|scoped ` | ` native ` | The preprocess mode to use
501- | ` localIdentName ` | ` {String} ` | ` "[local]-[hash:base64:6]" ` | A rule using any available token |
501+ | ` getLocalIdent ` | ` Function ` | ` undefined ` | Generate the classname by specifying a function instead of using the built-in interpolation |
502502| ` hashSeeder ` | ` {Array} ` | ` ['style', 'filepath', 'classname'] ` | An array of keys to base the hash on |
503- | ` allowedAttributes ` | ` {Array} ` | ` [] ` | An array of attributes to parse along with ` class ` |
503+ | ` includeAttributes ` | ` {Array} ` | ` [] ` | An array of attributes to parse along with ` class ` |
504504| ` includePaths ` | ` {Array} ` | ` [] ` (Any) | An array of paths to be processed |
505- | ` getLocalIdent ` | ` Function ` | ` undefined ` | Generate the classname by specifying a function instead of using the built-in interpolation |
506-
507- ** ` localIdentName ` **
505+ | ` localIdentName ` | ` {String} ` | ` "[local]-[hash:base64:6]" ` | A rule using any available token |
506+ | ` mode ` | ` native\|mixed\|scoped ` | ` native ` | The preprocess mode to use
507+ | ` parseExternalStylesheet ` | ` {Boolean} ` | ` false ` | Enable parsing on imported external stylesheet |
508+ | ` parseStyleTag ` | ` {Boolean} ` | ` true ` | Enable parsing on style tag |
509+ | ` useAsDefaultScoping ` | ` {Boolean} ` | ` false ` | Replace svelte scoping globally |
508510
509- Inspired by [ webpack interpolateName] ( https://github.com/webpack/loader-utils#interpolatename ) , here is the list of tokens:
510-
511- - ` [local] ` the targeted classname
512- - ` [ext] ` the extension of the resource
513- - ` [name] ` the basename of the resource
514- - ` [path] ` the path of the resource
515- - ` [folder] ` the folder the resource is in
516- - ` [contenthash] ` or ` [hash] ` * (they are the same)* the hash of the resource content (by default it's the hex digest of the md4 hash)
517- - ` [<hashType>:contenthash:<digestType>:<length>] ` optionally one can configure
518- - other hashTypes, i. e. ` sha1 ` , ` md4 ` , ` md5 ` , ` sha256 ` , ` sha512 `
519- - other digestTypes, i. e. ` hex ` , ` base26 ` , ` base32 ` , ` base36 ` , ` base49 ` , ` base52 ` , ` base58 ` , ` base62 ` , ` base64 `
520- - and ` length ` the length in chars
521511
522512** ` hashSeeder ` **
523513
@@ -560,7 +550,31 @@ preprocess: [
560550 </style >
561551```
562552
563- ** ` allowedAttributes ` **
553+ ** ` getLocalIdent ` **
554+
555+ Customize the creation of the classname instead of relying on the built-in function.
556+
557+ ``` ts
558+ function getLocalIdent(
559+ context : {
560+ context: string , // the context path
561+ resourcePath: string , // path + filename
562+ },
563+ localIdentName : {
564+ template: string , // the template rule
565+ interpolatedName: string , // the built-in generated classname
566+ },
567+ className : string , // the classname string
568+ content : {
569+ markup: string , // the markup content
570+ style: string , // the style content
571+ }
572+ ): string {
573+ return ` your_generated_classname ` ;
574+ }
575+ ```
576+
577+ ** ` includeAttributes ` **
564578
565579Add other attributes than ` class ` to be parsed by the preprocesser
566580
@@ -569,7 +583,7 @@ Add other attributes than `class` to be parsed by the preprocesser
569583...
570584preprocess: [
571585 cssModules ({
572- allowedAttributes : [' data-color' , ' classname' ],
586+ includeAttributes : [' data-color' , ' classname' ],
573587 })
574588],
575589...
@@ -594,29 +608,6 @@ preprocess: [
594608 </style >
595609```
596610
597- ** ` getLocalIdent ` **
598-
599- Customize the creation of the classname instead of relying on the built-in function.
600-
601- ``` ts
602- function getLocalIdent(
603- context : {
604- context: string , // the context path
605- resourcePath: string , // path + filename
606- },
607- localIdentName : {
608- template: string , // the template rule
609- interpolatedName: string , // the built-in generated classname
610- },
611- className : string , // the classname string
612- content : {
613- markup: string , // the markup content
614- style: string , // the style content
615- }
616- ): string {
617- return ` your_generated_classname ` ;
618- }
619- ```
620611
621612* Example of use*
622613
@@ -652,6 +643,50 @@ preprocess: [
652643...
653644```
654645
646+ ** ` localIdentName ` **
647+
648+ Inspired by [ webpack interpolateName] ( https://github.com/webpack/loader-utils#interpolatename ) , here is the list of tokens:
649+
650+ - ` [local] ` the targeted classname
651+ - ` [ext] ` the extension of the resource
652+ - ` [name] ` the basename of the resource
653+ - ` [path] ` the path of the resource
654+ - ` [folder] ` the folder the resource is in
655+ - ` [contenthash] ` or ` [hash] ` * (they are the same)* the hash of the resource content (by default it's the hex digest of the md4 hash)
656+ - ` [<hashType>:contenthash:<digestType>:<length>] ` optionally one can configure
657+ - other hashTypes, i. e. ` sha1 ` , ` md4 ` , ` md5 ` , ` sha256 ` , ` sha512 `
658+ - other digestTypes, i. e. ` hex ` , ` base26 ` , ` base32 ` , ` base36 ` , ` base49 ` , ` base52 ` , ` base58 ` , ` base62 ` , ` base64 `
659+ - and ` length ` the length in chars
660+
661+ ** ` useAsDefaultScoping ` **
662+
663+ Globally replace the default svelte scoping by the cssModules scoping. As a result, the ` module ` attribute to ` <style> ` becomes unnecessary.
664+
665+ ``` js
666+ // Preprocess config
667+ ...
668+ preprocess: [
669+ cssModules ([
670+ useAsDefaultScoping: true
671+ ]),
672+ ],
673+ ...
674+ ```
675+
676+ ``` html
677+ <h1 class =" title" >Welcome</h1 >
678+ <style >
679+ .title { color : blue }
680+ </style >
681+ ```
682+
683+ * Generating*
684+ ``` html
685+ <h1 class =" title-erYt1" >Welcome</h1 >
686+ <style >
687+ .title-erYt1 { color : blue }
688+ </style >
689+ ```
655690## Migrating from v1
656691If you want to migrate an existing project to ` v2 ` keeping the approach of the 1st version, follow the steps below:
657692
@@ -674,7 +709,6 @@ If you want to migrate an existing project to `v2` keeping the approach of the 1
674709 </style >
675710 ```
676711
677-
678712## Code Example
679713
680714* Rollup Config*
@@ -702,32 +736,27 @@ export default {
702736<style module>
703737 .modal {
704738 position : fixed ;
705- ...
706739 }
707- header {
708- ...
740+ .wrapper {
741+ padding : 0.5 rem 1 rem ;
709742 }
710743 .body {
711744 flex : 1 0 0 ;
712- ...
713- }
714- footer {
715- ...
716745 }
717746 button {
718- ...
747+ background-color : white ;
719748 }
720749 .cancel {
721750 background-color : #f2f2f2 ;
722751 }
723752 </style >
724753
725754<section class =" modal" >
726- <header >My Modal Title</header >
727- <div class =" body" >
728- <p >Lorem ipsum dolor sit, amet consectetur adipisicing elit .</p >
755+ <header class = " wrapper " >My Modal Title</header >
756+ <div class =" body wrapper " >
757+ <p >Lorem ipsum dolor sit, amet consectetur.</p >
729758 </div >
730- <footer >
759+ <footer class = " wrapper " >
731760 <button >Ok</button >
732761 <button class =" cancel" >Cancel</button >
733762 </footer >
@@ -740,22 +769,20 @@ export default {
740769/* * style.module.css */
741770.modal {
742771 position : fixed ;
743- ...
744772}
745-
746- [...]
773+ ...
747774```
748775```html
749776<script >
750777 import style from './style .module .css';
751778</script >
752779
753780<section class={style .modal }>
754- <header > My Modal Title</header >
755- <div class={style .body }>
756- <p > Lorem ipsum dolor sit, amet consectetur adipisicing elit .</p >
781+ <header class={ style . wrapper } > My Modal Title</header >
782+ <div class=" {style .body } { style . wrapper }" >
783+ <p > Lorem ipsum dolor sit, amet consectetur.</p >
757784 </div >
758- <footer >
785+ <footer class={ style . wrapper } >
759786 <button > Ok</button >
760787 <button class={style .cancel }> Cancel</button >
761788 </footer >
@@ -768,32 +795,27 @@ export default {
768795<style >
769796 ._329TyLUs9c {
770797 position : fixed ;
771- ...
772798 }
773- header {
774- ...
799+ .Re123xDTGv {
800+ padding : 0.5 rem 1 rem ;
775801 }
776802 ._1HPUBXtzNG {
777803 flex : 1 0 0 ;
778- ...
779- }
780- footer {
781- ...
782804 }
783805 button {
784- ...
806+ background-color : white ;
785807 }
786808 ._1xhJxRwWs7 {
787809 background-color : #f2f2f2 ;
788810 }
789811</style >
790812
791813<section class="_329TyLUs9c">
792- <header > My Modal Title</header >
793- <div class="_1HPUBXtzNG">
794- <p > Lorem ipsum dolor sit, amet consectetur adipisicing elit .</p >
814+ <header class="Re123xDTGv" > My Modal Title</header >
815+ <div class="_1HPUBXtzNG Re123xDTGv ">
816+ <p > Lorem ipsum dolor sit, amet consectetur.</p >
795817 </div >
796- <footer >
818+ <footer class="Re123xDTGv" >
797819 <button > Ok</button >
798820 <button class="_1xhJxRwWs7"> Cancel</button >
799821 </footer >
@@ -802,7 +824,7 @@ export default {
802824
803825## Why CSS Modules on Svelte
804826
805- While the native CSS Scoped system should be largely enough to avoid class conflict, it could find its limit when working on a hybrid project. On a non full Svelte application, the thought on the class naming would not be less different than what we would do on a regular html page . For example, on the modal component above, It would have been wiser to namespace some of the classes such as `.modal-body` and `.modal-cancel` in order to prevent inheriting styles from other `.body` and `.cancel` classes.
827+ While the native CSS Scoped system should be largely enough to avoid class conflict, it could find its limit when working on a hybrid project. On a non full Svelte application, paying attention to the name of a class would be no less different than to a regular html project . For example, on the modal component above, It would have been wiser to namespace some of the classes such as `.modal-body` and `.modal-cancel` in order to prevent inheriting styles from other `.body` and `.cancel` classes.
806828
807829## License
808830
0 commit comments