Skip to content

Commit

Permalink
Allow StyleSheetServer to be minified out in client-only bundles
Browse files Browse the repository at this point in the history
Webpack can be configured to minify out this code when building bundles
that are client-only. This can be accomplished via the DefinePlugin:

```
new webpack.DefinePlugin({
  "typeof window": JSON.stringify("object")
})
```
  • Loading branch information
lencioni committed Mar 15, 2018
1 parent e1a71ff commit 427fc69
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,32 @@ const StyleSheet = {

/**
* Utilities for using Aphrodite server-side.
*
* This can be minified out in client-only bundles by replacing `typeof window`
* with `"object"`, e.g. via Webpack's DefinePlugin:
*
* new webpack.DefinePlugin({
* "typeof window": JSON.stringify("object")
* })
*/
const StyleSheetServer = {
renderStatic(renderFunc /* : RenderFunction */) {
reset();
startBuffering();
const html = renderFunc();
const cssContent = flushToString();

return {
html: html,
css: {
content: cssContent,
renderedClassNames: getRenderedClassNames(),
},
};
},
};
const StyleSheetServer = typeof window !== 'undefined'
? null
: {
renderStatic(renderFunc /* : RenderFunction */) {
reset();
startBuffering();
const html = renderFunc();
const cssContent = flushToString();

return {
html: html,
css: {
content: cssContent,
renderedClassNames: getRenderedClassNames(),
},
};
},
};

/**
* Utilities for using Aphrodite in tests.
Expand Down

0 comments on commit 427fc69

Please sign in to comment.