Skip to content

Commit

Permalink
feat: add extract addon
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 22, 2018
1 parent 102c7d8 commit 8f9fed0
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions addon/extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

exports.addon = function (renderer) {
if (renderer.client) {
console.error(
'You are running nano-css "extract" addon in browser. ' +
'You should use it ONLY on server and ONLY at build time.'
);

return;
}

var sheet = renderer.sheet;

// eslint-disable-next-line no-unused-vars
var dummy;

// Evaluate all lazy-evaluated sheet() styles.
if (sheet) {
renderer.sheet = function (map) {
var styles = sheet.apply(this, arguments);

for (var name in map) dummy = styles[name];

return styles;
};
}

var jsx = renderer.jsx;

// Render jsx component once to extract its static CSS.
if (jsx) {
renderer.jsx = function () {
var jsxComponent = jsx.apply(this, arguments);

process.nextTick(function () {
jsxComponent(jsxComponent.defaultProps || {});
});

return jsxComponent;
};
}

var style = renderer.style;

// Render styled component once with default props
// to extract its static CSS and "default" dynamic CSS.
if (style) {
renderer.style = function (tag, css, dynamicTemplate) {
var styledComponent = style.apply(this, arguments);

if (typeof dynamicTemplate === 'function') {
process.nextTick(function () {
styledComponent(styledComponent.defaultProps || {});
});
}

return styledComponent;
};
}
};

0 comments on commit 8f9fed0

Please sign in to comment.