-
Notifications
You must be signed in to change notification settings - Fork 146
feat: add the :global {} rule support #161
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
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
46722ba
feat: add the @global {} rule support
illright 399482b
docs: fix whitespace in the Stylus example
illright 10511da
docs: document the addition and add a notice about `:local`
illright eeb1f4a
test: fix a typo in the test message
illright 3c6a574
feat: replace the @global for :global for CSS modules compliance
illright 89515d0
docs: update docs to reflect the change, fix mistakes
illright 6c99240
refactor: 💡 move things a bit
kaisermann 6294750
fix: 🐛 run globalRule only if postcss is installed
kaisermann 72393d2
refactor: 💡 simplify transformerOptions type
kaisermann 2ae68a0
chore(release): v3.9.0 :tada:
kaisermann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { PreprocessorGroup } from '../types'; | ||
|
|
||
| export default (): PreprocessorGroup => { | ||
| return { | ||
| async style({ content, filename }) { | ||
| const { default: transformer } = await import( | ||
| '../transformers/globalRule' | ||
| ); | ||
|
|
||
| return transformer({ content, filename }); | ||
| }, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import postcss from 'postcss'; | ||
|
|
||
| import { Transformer } from '../types'; | ||
| import { wrapSelectorInGlobal } from './globalStyle'; | ||
|
|
||
| const globalifyRulePlugin = (root: any) => { | ||
| root.walkRules(/:global(?!\()/, (rule: any) => { | ||
| const [beginning, ...rest] = rule.selector.split(/:global(?!\()/); | ||
| rule.selector = ( | ||
| beginning.trim() + ' ' | ||
| + rest.filter((x: string) => !!x).map(wrapSelectorInGlobal).join(' ') | ||
| ).trim(); | ||
| }); | ||
| }; | ||
|
|
||
| const transformer: Transformer<never> = async ({ content, filename }) => { | ||
| const { css, map: newMap } = await postcss() | ||
| .use(globalifyRulePlugin) | ||
| .process(content, { from: filename, map: true }); | ||
|
|
||
| return { code: css, map: newMap }; | ||
| }; | ||
|
|
||
| export default transformer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import autoProcess from '../../src'; | ||
| import { preprocess } from '../utils'; | ||
|
|
||
| describe('transformer - globalRule', () => { | ||
| it('wraps selector in :global(...) modifier', async () => { | ||
| const template = `<style>:global div{color:red}:global .test{}</style>`; | ||
| const opts = autoProcess(); | ||
| const preprocessed = await preprocess(template, opts); | ||
| expect(preprocessed.toString()).toContain( | ||
| `:global(div){color:red}:global(.test){}`, | ||
| ); | ||
| }); | ||
|
|
||
| it('wraps selector in :global(...) only if needed', async () => { | ||
| const template = `<style>:global .test{}:global :global(.foo){}</style>`; | ||
| const opts = autoProcess(); | ||
| const preprocessed = await preprocess(template, opts); | ||
| expect(preprocessed.toString()).toContain( | ||
| `:global(.test){}:global(.foo){}`, | ||
| ); | ||
| }); | ||
|
|
||
| it('wraps selector in :global(...) on multiple levels', async () => { | ||
| const template = '<style>:global div .cls{}</style>'; | ||
| const opts = autoProcess(); | ||
| const preprocessed = await preprocess(template, opts); | ||
| expect(preprocessed.toString()).toMatch( | ||
| // either be :global(div .cls){} | ||
| // or :global(div) :global(.cls){} | ||
| /(:global\(div .cls\)\{\}|:global\(div\) :global\(\.cls\)\{\})/, | ||
| ); | ||
| }); | ||
|
|
||
| it('wraps selector in :global(...) on multiple levels when in the middle', async () => { | ||
| const template = '<style>div :global span .cls{}</style>'; | ||
| const opts = autoProcess(); | ||
| const preprocessed = await preprocess(template, opts); | ||
| expect(preprocessed.toString()).toMatch( | ||
| // either be div :global(span .cls) {} | ||
| // or div :global(span) :global(.cls) {} | ||
| /div (:global\(span .cls\)\{\}|:global\(span\) :global\(\.cls\)\{\})/, | ||
| ); | ||
| }); | ||
|
|
||
| it('does not break when at the end', async () => { | ||
| const template = '<style>span :global{}</style>'; | ||
| const opts = autoProcess(); | ||
| const preprocessed = await preprocess(template, opts); | ||
| expect(preprocessed.toString()).toContain('span{}'); | ||
| }); | ||
|
|
||
| it('works with collapsed nesting several times', async () => { | ||
| const template = '<style>div :global span :global .cls{}</style>'; | ||
| const opts = autoProcess(); | ||
| const preprocessed = await preprocess(template, opts); | ||
| expect(preprocessed.toString()).toMatch( | ||
| // either be div :global(span .cls) {} | ||
| // or div :global(span) :global(.cls) {} | ||
| /div (:global\(span .cls\)\{\}|:global\(span\) :global\(\.cls\)\{\})/, | ||
| ); | ||
| }); | ||
|
|
||
| it('does not interfere with the :global(...) syntax', async () => { | ||
| const template = '<style>div :global(span){}</style>'; | ||
| const opts = autoProcess(); | ||
| const preprocessed = await preprocess(template, opts); | ||
| expect(preprocessed.toString()).toContain('div :global(span){}'); | ||
| }); | ||
|
|
||
| it('allows mixing with the :global(...) syntax', async () => { | ||
| const template = '<style>div :global(span) :global .cls{}</style>'; | ||
| const opts = autoProcess(); | ||
| const preprocessed = await preprocess(template, opts); | ||
| expect(preprocessed.toString()).toMatch( | ||
| // either be div :global(span .cls) {} | ||
| // or div :global(span) :global(.cls) {} | ||
| /div (:global\(span .cls\)\{\}|:global\(span\) :global\(\.cls\)\{\})/, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.