-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the @global {} rule support
- Loading branch information
1 parent
e9d7c61
commit 46722ba
Showing
5 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,28 @@ | ||
import postcss from 'postcss'; | ||
|
||
import { Transformer } from '../types'; | ||
import { globalifyPlugin } from './globalStyle'; | ||
|
||
const globalifyRulePlugin = (root: any) => { | ||
root.walkAtRules(/^global$/, (atrule: any) => { | ||
globalifyPlugin(atrule); | ||
let after = atrule; | ||
|
||
atrule.each(function (child: any) { | ||
after.after(child); | ||
after = child; | ||
}); | ||
|
||
atrule.remove(); | ||
}); | ||
}; | ||
|
||
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 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 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,42 @@ | ||
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}.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(...) modifier only inside the rule', async () => { | ||
const template = `<style>@global{div{color:red}}.test{}</style>`; | ||
const opts = autoProcess(); | ||
const preprocessed = await preprocess(template, opts); | ||
expect(preprocessed.toString()).toContain( | ||
`:global(div){color:red}.test{}`, | ||
); | ||
}); | ||
|
||
it('wraps selector in :global(...) only if needed', async () => { | ||
const template = `<style>@global{.test{}:global(.foo){}}</style>`; | ||
const opts = autoProcess(); | ||
const preprocessed = await preprocess(template, opts); | ||
expect(preprocessed.toString()).toContain( | ||
`:global(.test){}:global(.foo){}`, | ||
); | ||
}); | ||
|
||
it("prefixes @keyframes names with '-global-' only if needed", async () => { | ||
const template = `<style> | ||
@global{@keyframes a {from{} to{}}@keyframes -global-b {from{} to{}}} | ||
</style>`; | ||
const opts = autoProcess(); | ||
const preprocessed = await preprocess(template, opts); | ||
expect(preprocessed.toString()).toContain( | ||
`@keyframes -global-a {from{} to{}}@keyframes -global-b {from{} to{}}`, | ||
); | ||
}); | ||
}); |