/
api.js
95 lines (78 loc) · 3.28 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import postcss from 'postcss';
import ava from 'ava';
import nano from '..';
import autoprefixer from 'autoprefixer';
import {name} from '../../package.json';
import specName from './util/specName';
function pluginMacro (t, instance) {
const css = 'h1 { color: #ffffff }';
const min = 'h1{color:#fff}';
return instance.process(css).then((result) => {
t.deepEqual(result.css, min, specName('beConsumedByPostCSS'));
});
}
ava('can be used as a postcss plugin', pluginMacro, postcss().use(nano()));
ava('can be used as a postcss plugin (2)', pluginMacro, postcss([nano()]));
ava('can be used as a postcss plugin (3)', pluginMacro, postcss(nano));
ava('can be used as a postcss plugin, with options', t => {
const css = `
h1 {
width: calc(3px * 2 - 1px);
}`;
const exp = `h1{width:calc(3px * 2 - 1px)}`;
return postcss(nano({calc: false})).process(css).then(result => {
t.deepEqual(result.css, exp, specName('notTransformCalcProperty'));
});
});
ava('should use the postcss plugin api', t => {
t.truthy(nano().postcssVersion, 'should be able to access version');
t.deepEqual(nano().postcssPlugin, name, 'should be able to access name');
});
ava('should silently disable features if they are already consumed by postcss', t => {
const css = 'h1{-webkit-border-radius:5px;border-radius:5px}';
return postcss([ autoprefixer({browsers: 'Safari < 5'}), nano() ]).process(css).then(result => {
t.deepEqual(result.css, css, specName('notIncludeAutoprefixerTwice'));
});
});
function disableMacro (t, opts) {
const css = 'h1 { color: #ffffff }';
const min = 'h1{color:#ffffff}';
return postcss().use(nano(opts)).process(css).then(result => {
t.deepEqual(result.css, min, specName('disableColourMinification'));
});
}
ava('should disable features', disableMacro, {'postcss-colormin': false});
ava('should disable features (2)', disableMacro, {postcssColormin: false});
ava('should disable features (3)', disableMacro, {colormin: false});
ava('should not fail when options.safe is enabled', t => {
const css = 'h1 { z-index: 100 }';
const min = 'h1{z-index:100}';
return nano.process(css, {safe: true}).then(result => {
t.deepEqual(result.css, min, specName('beConsumedByPostCSS'));
});
});
ava('should not fail second time when the same options are passed in, with options.safe as enabled', t => {
const css = 'h1 { z-index: 100 }';
const min = 'h1{z-index:100}';
const options = {safe: true};
return nano.process(css, options)
.then(result => {
t.deepEqual(result.css, min, specName('beConsumedByPostCSS'));
})
.then(() => nano.process(css, options))
.then(result => {
t.deepEqual(result.css, min, specName('beConsumedByPostCSS'));
});
});
ava('should work with sourcemaps', t => {
return nano.process('h1{z-index:1}', {map: {inline: true}}).then(({css}) => {
const hasMap = /sourceMappingURL=data:application\/json;base64/.test(css);
t.truthy(hasMap);
});
});
ava('should work with sourcemaps (2)', t => {
return nano.process('h1{z-index:1}', {sourcemap: true}).then(({css}) => {
const hasMap = /sourceMappingURL=data:application\/json;base64/.test(css);
t.truthy(hasMap);
});
});