-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 make first version of sourcemap addon work
- Loading branch information
Showing
3 changed files
with
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {createElement as h} from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
const {action} = require('@storybook/addon-actions'); | ||
const {linkTo} = require('@storybook/addon-links'); | ||
const {create} = require('../index'); | ||
const {addon} = require('../addon/rule'); | ||
const {addon: addonSourcemaps} = require('../addon/sourcemaps'); | ||
|
||
const nano = create(); | ||
addon(nano); | ||
addonSourcemaps(nano); | ||
const {rule} = nano; | ||
|
||
const className1 = rule({ | ||
border: '1px solid red' | ||
}, 'RedBorder'); | ||
|
||
storiesOf('Addons/sourcemaps', module) | ||
.add('Default', () => | ||
h('div', {className: className1}, 'Hello world') | ||
) |
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,82 @@ | ||
'use strict'; | ||
|
||
var StackTrace = require('stacktrace-js'); | ||
var SourcemapCodec = require('sourcemap-codec'); | ||
|
||
exports.addon = function (renderer) { | ||
var queue = []; | ||
var timeout = null; | ||
var sourceCache = {}; | ||
|
||
function flush () { | ||
timeout = null; | ||
|
||
var sources = []; | ||
var segments = []; | ||
var rules = []; | ||
|
||
for (var i = 0; i < queue.length; i++) { | ||
var item = queue[i]; | ||
|
||
rules.push(item.rule); | ||
segments.push([[0, sources.length, item.lineNumber - 1, 0]]); | ||
sources.push(item.fileName); | ||
} | ||
|
||
queue = []; | ||
|
||
var mappings = SourcemapCodec.encode(segments); | ||
var map = { | ||
version: 3, | ||
sources: sources, | ||
mappings: mappings, | ||
sourcesContent: sources.map(function (source) { | ||
return sourceCache[source]; | ||
}), | ||
}; | ||
|
||
var json = JSON.stringify(map); | ||
var base64 = window.btoa(json); | ||
var css = rules.join('\n') + '\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64 + ' */'; | ||
var style = document.createElement('style'); | ||
|
||
style.appendChild(document.createTextNode(css)); | ||
document.head.appendChild(style); | ||
} | ||
|
||
function enqueue (rawCss) { | ||
StackTrace.get({sourceCache: sourceCache}) | ||
.then(function (stackframes) { | ||
var frame = stackframes[4]; | ||
|
||
if (!frame) { | ||
return; | ||
} | ||
|
||
var pos = rawCss.indexOf('{'); | ||
|
||
if (pos < 1) return; | ||
|
||
var selector = rawCss.substr(0, pos).trim(); | ||
|
||
queue.push({ | ||
selector: selector, | ||
rule: rawCss, | ||
fileName: frame.fileName, | ||
lineNumber: frame.lineNumber, | ||
}); | ||
|
||
if (!timeout) { | ||
timeout = setTimeout(flush, 100); | ||
} | ||
// eslint-disable-next-line no-console | ||
}, console.log); | ||
} | ||
|
||
var putRaw = renderer.putRaw; | ||
|
||
renderer.putRaw = function (rawCSS) { | ||
enqueue(rawCSS); | ||
putRaw.apply(null, arguments); | ||
}; | ||
}; |
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
038b2c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Build version:
2.0.2-feat-sourcemaps.24
🤞feat-sourcemaps
on CircleCI 🎉