Skip to content

Commit 8b24e44

Browse files
committed
feat: 🎸 improve source maps, make work with jsx and sheet
1 parent 038b2c1 commit 8b24e44

File tree

4 files changed

+85
-22
lines changed

4 files changed

+85
-22
lines changed

‎.storybook/sourcemaps.stories.js

+43-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,58 @@ const {linkTo} = require('@storybook/addon-links');
55
const {create} = require('../index');
66
const {addon} = require('../addon/rule');
77
const {addon: addonSourcemaps} = require('../addon/sourcemaps');
8+
const {addon: addonSheet} = require('../addon/sheet');
9+
const {addon: addonCache} = require('../addon/cache');
10+
const {addon: addonJsx} = require('../addon/jsx');
11+
const {addon: addonStyle} = require('../addon/style');
12+
const {addon: addonStyled} = require('../addon/styled');
813

9-
const nano = create();
14+
const nano = create({
15+
h
16+
});
1017
addon(nano);
18+
addonSheet(nano);
19+
addonCache(nano);
20+
addonJsx(nano);
21+
addonStyle(nano);
22+
addonStyled(nano);
1123
addonSourcemaps(nano);
12-
const {rule} = nano;
24+
const {rule, sheet, jsx, styled} = nano;
1325

1426
const className1 = rule({
1527
border: '1px solid red'
1628
}, 'RedBorder');
1729

30+
const styles1 = sheet({
31+
wrapper: {
32+
border: '1px solid red',
33+
},
34+
button: {
35+
color: 'green',
36+
background: 'black',
37+
}
38+
});
39+
40+
const Button = jsx('button', {
41+
border: '1px solid black',
42+
background: 'black',
43+
color: 'white',
44+
});
45+
46+
const Button2 = styled.button({
47+
border: '1px solid black',
48+
background: 'black',
49+
color: 'red',
50+
});
51+
1852
storiesOf('Addons/sourcemaps', module)
1953
.add('Default', () =>
2054
h('div', {className: className1}, 'Hello world')
2155
)
56+
.add('sheet', () =>
57+
h('div', {className: styles1.wrapper},
58+
h('button', {className: styles1.button}, 'Click me!')
59+
)
60+
)
61+
.add('jsx', () => h(Button, {}, 'Click me!'))
62+
.add('styled', () => h(Button2, {}, 'Click me!'))

‎addon/jsx.js

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ exports.addon = function (renderer) {
1515
var className;
1616
var isElement = typeof fn === 'string';
1717

18+
// In dev mode emit CSS immediately so correct sourcemaps can be generated.
19+
if (process.env.NODE_ENV !== 'production') {
20+
className = renderer.rule(styles, block);
21+
}
22+
1823
var Component = function (props) {
1924
if (!className) {
2025
className = renderer.rule(styles, block);

‎addon/sheet.js

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

18-
Object.defineProperty(result, elementModifier, {
19-
configurable: true,
20-
get: function () {
21-
var classNames = renderer.rule(styles, block + '-' + elementModifier);
22-
23-
Object.defineProperty(result, elementModifier, {
24-
value: classNames
25-
});
26-
27-
return classNames;
28-
},
29-
});
18+
if (process.env.NODE_ENV !== 'production') {
19+
// In dev mode emit CSS immediately to generate sourcemaps.
20+
result[elementModifier] = renderer.rule(styles, block + '-' + elementModifier);
21+
} else {
22+
Object.defineProperty(result, elementModifier, {
23+
configurable: true,
24+
get: function () {
25+
var classNames = renderer.rule(styles, block + '-' + elementModifier);
26+
27+
Object.defineProperty(result, elementModifier, {
28+
value: classNames
29+
});
30+
31+
return classNames;
32+
},
33+
});
34+
}
3035
};
3136

3237
for (var elementModifier in map) {

‎addon/sourcemaps.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,25 @@
33
var StackTrace = require('stacktrace-js');
44
var SourcemapCodec = require('sourcemap-codec');
55

6+
function findStackframe (frames) {
7+
for (var i = 4; i < frames.length; i++) {
8+
if (!frames[i].fileName.match(/addon\/[^.]+\.js/)) {
9+
return frames[i];
10+
}
11+
}
12+
}
13+
614
exports.addon = function (renderer) {
15+
if (process.env.NODE_ENV === 'production') {
16+
// eslint-disable-next-line no-console
17+
console.log(
18+
'nano-css sourcemaps addon should be installed only in development mode. ' +
19+
'Use (process.env.NODE !== "production") to check if you are in development mode.'
20+
);
21+
22+
return;
23+
}
24+
725
var queue = [];
826
var timeout = null;
927
var sourceCache = {};
@@ -40,27 +58,21 @@ exports.addon = function (renderer) {
4058
var css = rules.join('\n') + '\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64 + ' */';
4159
var style = document.createElement('style');
4260

61+
style.setAttribute('data-sourcemaps', '');
4362
style.appendChild(document.createTextNode(css));
4463
document.head.appendChild(style);
4564
}
4665

4766
function enqueue (rawCss) {
4867
StackTrace.get({sourceCache: sourceCache})
4968
.then(function (stackframes) {
50-
var frame = stackframes[4];
69+
var frame = findStackframe(stackframes);
5170

5271
if (!frame) {
5372
return;
5473
}
5574

56-
var pos = rawCss.indexOf('{');
57-
58-
if (pos < 1) return;
59-
60-
var selector = rawCss.substr(0, pos).trim();
61-
6275
queue.push({
63-
selector: selector,
6476
rule: rawCss,
6577
fileName: frame.fileName,
6678
lineNumber: frame.lineNumber,

0 commit comments

Comments
 (0)