Skip to content

Commit 446e9c1

Browse files
committed
feat: 🎸 pretty-print CSS in DEV mode
1 parent 6338f8d commit 446e9c1

7 files changed

+48
-42
lines changed

‎addon/__tests__/amp.server.test.js

+25-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
var env = require('./env');
66
var create = require('../../index').create;
7-
var addonAmp= require('../../addon/amp').addon;
7+
var addonAmp = require('../../addon/amp').addon;
8+
var isProd = process.env.NODE_ENV === 'production';
89

910
function createNano (config, addonConfig) {
1011
var nano = create(config);
@@ -30,28 +31,30 @@ describe('amp', function () {
3031
color: 'red'
3132
});
3233

33-
expect(nano.raw.replace(/ /g, '')).toBe('.foo{color:red;}.bar{color:red;}');
34+
expect(nano.raw.replace(/[\s\n]+/g, '')).toBe('.foo{color:red;}.bar{color:red;}');
3435
});
3536

36-
it('caps at limit', function () {
37-
var nano = createNano({}, {
38-
limit: 40
39-
});
40-
41-
nano.put('.foo', {
42-
color: 'red'
43-
});
44-
45-
nano.put('.bar', {
46-
color: 'red'
47-
});
48-
49-
nano.put('.baz', {
50-
color: 'red'
51-
});
52-
53-
expect(nano.raw.replace(/ /g, '')).toBe('.foo{color:red;}.bar{color:red;}');
54-
});
37+
if (isProd) {
38+
it('caps at limit', function () {
39+
var nano = createNano({}, {
40+
limit: 40
41+
});
42+
43+
nano.put('.foo', {
44+
color: 'red'
45+
});
46+
47+
nano.put('.bar', {
48+
color: 'red'
49+
});
50+
51+
nano.put('.baz', {
52+
color: 'red'
53+
});
54+
55+
expect(nano.raw.replace(/[\s\n]+/g, '')).toBe('.foo{color:red;}.bar{color:red;}');
56+
});
57+
}
5558

5659
it('warns on !important', function () {
5760
var nano = createNano();
@@ -211,12 +214,10 @@ describe('amp', function () {
211214
color: 'blue'
212215
});
213216

214-
var length = nano.raw.length;
215-
216217
nano.put('.bar', {
217218
behavior: 'something'
218219
});
219220

220-
expect(nano.raw.length).toBe(length);
221+
expect(nano.raw.indexOf('behavior')).toBe(-1);
221222
});
222223
});

‎addon/__tests__/index.test.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('nano-css', function () {
9090
}
9191

9292
if (env.isServer) {
93-
expect(nano.raw).toBe('.foo{color:red;}');
93+
expect(nano.raw.replace(/[\s\n]+/g, '')).toBe('.foo{color:red;}');
9494
}
9595
});
9696

@@ -163,7 +163,9 @@ describe('nano-css', function () {
163163
}
164164

165165
if (env.isServer) {
166-
expect(renderer.raw).toBe('.foo .bar .baz .bazooka{color:red;}');
166+
expect(renderer.raw.indexOf('.foo .bar .baz .bazooka') > -1).toBe(true);
167+
expect(renderer.raw.indexOf('color') > -1).toBe(true);
168+
expect(renderer.raw.indexOf('red') > -1).toBe(true);
167169
}
168170
});
169171

@@ -224,10 +226,10 @@ describe('nano-css', function () {
224226
});
225227

226228
expect(nano.putRaw).toHaveBeenCalledTimes(1);
227-
expect(nano.putRaw.mock.calls[0][0].replace(/ /g, '')).toBe('@mediascreen{.global{color:green;}}');
229+
expect(nano.putRaw.mock.calls[0][0].replace(/[\s\n]+/g, '')).toBe('@mediascreen{.global{color:green;}}');
228230
});
229231

230-
it('supports @media queries - 2', function () {
232+
it('supports @media queries - 3', function () {
231233
var nano = create();
232234

233235
if (env.isClient) {
@@ -248,7 +250,7 @@ describe('nano-css', function () {
248250
}
249251

250252
if (env.isServer) {
251-
expect(nano.raw).toBe('@media screen{.parent .foo{color:red;}}');
253+
expect(nano.raw.replace(/[\s\n]+/g, '')).toBe('@mediascreen{.parent.foo{color:red;}}');
252254
}
253255
});
254256
});

‎addon/__tests__/limit.server.dev.test.js

-7
This file was deleted.

‎addon/__tests__/limit.server.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('limit', function () {
2929
color: 'red'
3030
});
3131

32-
expect(nano.raw.replace(/ /g, '')).toBe('.foo{color:red;}.bar{color:red;}.baz{color:red;}');
32+
expect(nano.raw.replace(/[\s\n]+/g, '')).toBe('.foo{color:red;}.bar{color:red;}.baz{color:red;}');
3333
});
3434

3535
it('caps at limit', function () {
@@ -48,7 +48,7 @@ describe('limit', function () {
4848
nano.put('.baz', {
4949
color: 'red'
5050
});
51-
52-
expect(nano.raw.replace(/ /g, '')).toBe('.foo{color:red;}.bar{color:red;}');
51+
console.log(nano.raw);
52+
expect(nano.raw.replace(/[\s\n]+/g, '')).toBe('.foo{color:red;}.bar{color:red;}');
5353
});
5454
});

‎addon/sheet.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports.addon = function (renderer) {
1515
var onElementModifier = function (elementModifier) {
1616
var styles = map[elementModifier];
1717

18-
if (process.env.NODE_ENV !== 'production') {
18+
if ((process.env.NODE_ENV !== 'production') && renderer.sourcemaps) {
1919
// In dev mode emit CSS immediately to generate sourcemaps.
2020
result[elementModifier] = renderer.rule(styles, block + '-' + elementModifier);
2121
} else {

‎addon/sourcemaps.js

+2
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,6 @@ exports.addon = function (renderer) {
9191
enqueue(rawCSS);
9292
putRaw.apply(null, arguments);
9393
};
94+
95+
renderer.sourcemaps = true;
9496
};

‎index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,20 @@ exports.create = function (config) {
104104
if ((value instanceof Object) && !(value instanceof Array)) {
105105
postponed.push(prop);
106106
} else {
107-
str += renderer.decl(prop, value, selector, atrule);
107+
if ((process.env.NODE_ENV !== 'production') && !renderer.sourcemaps) {
108+
str += ' ' + renderer.decl(prop, value, selector, atrule) + '\n';
109+
} else {
110+
str += renderer.decl(prop, value, selector, atrule);
111+
}
108112
}
109113
}
110114

111115
if (str) {
112-
str = selector + '{' + str + '}';
116+
if ((process.env.NODE_ENV !== 'production') && !renderer.sourcemaps) {
117+
str = '\n' + selector + ' {\n' + str + '}\n';
118+
} else {
119+
str = selector + '{' + str + '}';
120+
}
113121
renderer.putRaw(atrule ? atrule + '{' + str + '}' : str);
114122
}
115123

0 commit comments

Comments
 (0)