1
+ 'use strict' ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+
7
+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
8
+
9
+ exports . flush = flush ;
10
+
11
+ var _react = require ( 'react' ) ;
12
+
13
+ var _react2 = _interopRequireDefault ( _react ) ;
14
+
15
+ var _addStyles = require ( 'style-loader/addStyles' ) ;
16
+
17
+ var _addStyles2 = _interopRequireDefault ( _addStyles ) ;
18
+
19
+ var _hoistNonReactStatics = require ( 'hoist-non-react-statics' ) ;
20
+
21
+ var _hoistNonReactStatics2 = _interopRequireDefault ( _hoistNonReactStatics ) ;
22
+
23
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
24
+
25
+ function _toConsumableArray ( arr ) { if ( Array . isArray ( arr ) ) { for ( var i = 0 , arr2 = Array ( arr . length ) ; i < arr . length ; i ++ ) { arr2 [ i ] = arr [ i ] ; } return arr2 ; } else { return Array . from ( arr ) ; } }
26
+
27
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
28
+
29
+ function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
30
+
31
+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; } /* eslint react/no-multi-comp:0 */
32
+
33
+ var ssrStyleElId = 'next-style-ssr' ;
34
+ var isServer = typeof window === 'undefined' ;
35
+
36
+ var serverStyles = ! isServer ? null : [ ] ;
37
+
38
+ function applyStylesOnServer ( styles ) {
39
+ styles = Array . isArray ( styles ) ? styles : [ styles ] ;
40
+
41
+ return function ( WrappedComponent ) {
42
+ var ApplyStyles = function ( _Component ) {
43
+ _inherits ( ApplyStyles , _Component ) ;
44
+
45
+ function ApplyStyles ( ) {
46
+ _classCallCheck ( this , ApplyStyles ) ;
47
+
48
+ return _possibleConstructorReturn ( this , ( ApplyStyles . __proto__ || Object . getPrototypeOf ( ApplyStyles ) ) . apply ( this , arguments ) ) ;
49
+ }
50
+
51
+ _createClass ( ApplyStyles , [ {
52
+ key : 'componentWillMount' ,
53
+ value : function componentWillMount ( ) {
54
+ var _serverStyles ;
55
+
56
+ // Concatenate styles
57
+ ( _serverStyles = serverStyles ) . push . apply ( _serverStyles , _toConsumableArray ( styles ) ) ;
58
+ }
59
+ } , {
60
+ key : 'render' ,
61
+ value : function render ( ) {
62
+ return _react2 . default . createElement ( WrappedComponent , this . props ) ;
63
+ }
64
+ } ] ) ;
65
+
66
+ return ApplyStyles ;
67
+ } ( _react . Component ) ;
68
+
69
+ var displayName = WrappedComponent . displayName || WrappedComponent . name || 'Component' ;
70
+
71
+ ApplyStyles . displayName = 'ApplyStyles(' + displayName + ')' ;
72
+ ApplyStyles . ComposedComponent = WrappedComponent ;
73
+
74
+ return ( 0 , _hoistNonReactStatics2 . default ) ( ApplyStyles , WrappedComponent ) ;
75
+ } ;
76
+ }
77
+
78
+ // ---------------------------------------------------
79
+
80
+ var removedSsrStyleEl = isServer ? null : false ;
81
+ var removeSsrStyleElDelay = isServer ? null : 1000 ;
82
+
83
+ function applyStylesOnClient ( styles ) {
84
+ styles = Array . isArray ( styles ) ? styles : [ styles ] ;
85
+
86
+ return function ( WrappedComponent ) {
87
+ var ApplyStyles = function ( _Component2 ) {
88
+ _inherits ( ApplyStyles , _Component2 ) ;
89
+
90
+ function ApplyStyles ( ) {
91
+ _classCallCheck ( this , ApplyStyles ) ;
92
+
93
+ return _possibleConstructorReturn ( this , ( ApplyStyles . __proto__ || Object . getPrototypeOf ( ApplyStyles ) ) . apply ( this , arguments ) ) ;
94
+ }
95
+
96
+ _createClass ( ApplyStyles , [ {
97
+ key : 'componentWillMount' ,
98
+ value : function componentWillMount ( ) {
99
+ // Insert component styles
100
+ this . _updateNextStyles = ( 0 , _addStyles2 . default ) ( styles . map ( function ( style ) {
101
+ return [ style . id , style . content , '' , style . sourceMap ] ;
102
+ } ) ) ;
103
+ }
104
+ } , {
105
+ key : 'componentDidMount' ,
106
+ value : function componentDidMount ( ) {
107
+ // Remove the server-rendered style tag
108
+ if ( ! removedSsrStyleEl ) {
109
+ removedSsrStyleEl = true ;
110
+
111
+ setTimeout ( function ( ) {
112
+ var headEl = document . head || document . getElementsByTagName ( 'head' ) [ 0 ] ;
113
+ var styleEl = document . getElById ( ssrStyleElId ) ;
114
+
115
+ styleEl && headEl . removeChild ( styleEl ) ;
116
+ } , removeSsrStyleElDelay ) ;
117
+ }
118
+ }
119
+ } , {
120
+ key : 'componentWillUnmount' ,
121
+ value : function componentWillUnmount ( ) {
122
+ // Remove component styles
123
+ this . _updateNextStyles ( [ ] ) ;
124
+ }
125
+ } , {
126
+ key : 'render' ,
127
+ value : function render ( ) {
128
+ return _react2 . default . createElement ( WrappedComponent , this . props ) ;
129
+ }
130
+ } ] ) ;
131
+
132
+ return ApplyStyles ;
133
+ } ( _react . Component ) ;
134
+
135
+ var displayName = WrappedComponent . displayName || WrappedComponent . name || 'Component' ;
136
+
137
+ ApplyStyles . displayName = 'ApplyStyles(' + displayName + ')' ;
138
+ ApplyStyles . ComposedComponent = WrappedComponent ;
139
+
140
+ return ( 0 , _hoistNonReactStatics2 . default ) ( ApplyStyles , WrappedComponent ) ;
141
+ } ;
142
+ }
143
+
144
+ // ---------------------------------------------------
145
+
146
+ exports . default = isServer ? applyStylesOnServer : applyStylesOnClient ;
147
+ function flush ( ) {
148
+ if ( ! isServer ) {
149
+ throw new Error ( 'flush() should only be called on the server' ) ;
150
+ }
151
+
152
+ var flushedStyles = serverStyles ;
153
+
154
+ serverStyles = [ ] ;
155
+
156
+ return {
157
+ tag : _react2 . default . createElement (
158
+ 'style' ,
159
+ { id : ssrStyleElId } ,
160
+ flushedStyles . reduce ( function ( concatenated , style ) {
161
+ return concatenated + style . content ;
162
+ } , '' )
163
+ ) ,
164
+ styles : flushedStyles
165
+ } ;
166
+ }
0 commit comments