Skip to content

Commit ec9b9b9

Browse files
committed
Add support for making applied rules important
1 parent 47a3b93 commit ec9b9b9

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

__tests__/applyAtRule.test.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,36 @@ test("it copies a class's declarations into itself", () => {
1414
})
1515
})
1616

17-
test('it removes important from applied classes', () => {
18-
const output = '.a { color: red !important; } .b { color: red; }'
17+
test('it removes important from applied classes by default', () => {
18+
const input = `
19+
.a { color: red !important; }
20+
.b { @apply .a; }
21+
`
1922

20-
return run('.a { color: red !important; } .b { @apply .a; }').then(result => {
21-
expect(result.css).toEqual(output)
23+
const expected = `
24+
.a { color: red !important; }
25+
.b { color: red; }
26+
`
27+
28+
return run(input).then(result => {
29+
expect(result.css).toEqual(expected)
30+
expect(result.warnings().length).toBe(0)
31+
})
32+
})
33+
34+
test('applied rules can be made !important', () => {
35+
const input = `
36+
.a { color: red; }
37+
.b { @apply .a !important; }
38+
`
39+
40+
const expected = `
41+
.a { color: red; }
42+
.b { color: red !important; }
43+
`
44+
45+
return run(input).then(result => {
46+
expect(result.css).toEqual(expected)
2247
expect(result.warnings().length).toBe(0)
2348
})
2449
})

src/lib/substituteClassApplyAtRules.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import _ from 'lodash'
22
import postcss from 'postcss'
33
import escapeClassName from '../util/escapeClassName'
44

5-
function normalizeClassNames(classNames) {
6-
return classNames.map(className => {
7-
return `.${escapeClassName(_.trimStart(className, '.'))}`
8-
})
5+
function normalizeClassName(className) {
6+
return `.${escapeClassName(_.trimStart(className, '.'))}`
97
}
108

119
function findMixin(css, mixin, onError) {
@@ -52,14 +50,17 @@ export default function() {
5250
return _.startsWith(mixin, '--')
5351
})
5452

55-
const decls = _.flatMap(normalizeClassNames(classes), mixin => {
56-
return findMixin(css, mixin, message => {
57-
throw atRule.error(message)
53+
const decls = _(classes)
54+
.reject(mixin => mixin === '!important')
55+
.flatMap(mixin => {
56+
return findMixin(css, normalizeClassName(mixin), message => {
57+
throw atRule.error(message)
58+
})
5859
})
59-
})
60+
.value()
6061

61-
decls.forEach(decl => {
62-
decl.important = false
62+
_.tap(_.last(mixins) === '!important', important => {
63+
decls.forEach(decl => (decl.important = important))
6364
})
6465

6566
atRule.before(decls)

0 commit comments

Comments
 (0)