2
2
* @copyright 2015, Andrey Popp
3
3
*/
4
4
5
- var path = require ( 'path' ) ;
6
- var Module = require ( 'module' ) ;
7
- var Styling = require ( './Styling' ) ;
8
- var renderStyling = require ( './renderStyling ' ) ;
5
+ var path = require ( 'path' ) ;
6
+ var Module = require ( 'module' ) ;
7
+ var Styling = require ( './Styling' ) ;
8
+ var renderStylingSheet = require ( './renderStylingSheet ' ) ;
9
9
10
10
var NodeTemplatePlugin = require ( 'webpack/lib/node/NodeTemplatePlugin' ) ;
11
11
var NodeTargetPlugin = require ( 'webpack/lib/node/NodeTargetPlugin' ) ;
12
12
var LibraryTemplatePlugin = require ( 'webpack/lib/LibraryTemplatePlugin' ) ;
13
13
var SingleEntryPlugin = require ( 'webpack/lib/SingleEntryPlugin' ) ;
14
14
var LimitChunkCountPlugin = require ( 'webpack/lib/optimize/LimitChunkCountPlugin' ) ;
15
15
16
+ var renderStylingSheetMod = require . resolve ( './renderStylingSheet' ) ;
17
+
18
+ var extractTextWebpackPluginKey ;
19
+ try {
20
+ extractTextWebpackPluginKey = path . dirname ( require . resolve ( 'extract-text-webpack-plugin' ) ) ;
21
+ } catch ( error ) { }
16
22
17
23
module . exports = function styling ( content ) {
18
24
this . cacheable ( ) ;
19
- return content ;
25
+ if ( this [ __dirname ] === false ) {
26
+ return '' ;
27
+ } else if ( typeof this [ extractTextWebpackPluginKey ] === 'function' ) {
28
+ var cb = this . async ( ) ;
29
+ var request = this . request . split ( '!' ) . slice ( this . loaderIndex + 1 ) . join ( '!' ) ;
30
+ produce ( this , request , function ( err , result ) {
31
+ if ( err ) {
32
+ cb ( err ) ;
33
+ } else {
34
+ setCompiledStyling ( this , result ) ;
35
+ cb ( null , '' ) ;
36
+ }
37
+ } . bind ( this ) ) ;
38
+ } else if ( this [ extractTextWebpackPluginKey ] === false ) {
39
+ return getCompiledStyling ( this ) ;
40
+ } else {
41
+ return '' ;
42
+ }
20
43
} ;
21
44
22
- module . exports . pitch = function stylingPitch ( request , precedingRequest ) {
45
+ module . exports . pitch = function stylingPitch ( request , precedingRequest , data ) {
46
+ this . cacheable ( ) ;
23
47
if ( this [ __dirname ] === false ) {
48
+ // if we already inside the loader
49
+ return ;
50
+ } else if ( extractTextWebpackPluginKey in this ) {
51
+ // if extract-text-webpack-plugin is active we do all work in a loader phase
24
52
return ;
53
+ } else {
54
+ var cb = this . async ( ) ;
55
+ produce ( this , request , cb ) ;
25
56
}
26
- var childFilename = "styling-output-filename" ;
27
- var outputOptions = { filename : childFilename } ;
28
- var childCompiler = this . _compilation . createChildCompiler ( "styling-compiler" , outputOptions ) ;
57
+ } ;
58
+
59
+ function produce ( loader , request , cb ) {
60
+ var outputFilename = "styling-output-filename" ;
61
+ var outputOptions = { filename : outputFilename } ;
62
+ var childCompiler = loader . _compilation . createChildCompiler ( "styling-compiler" , outputOptions ) ;
29
63
childCompiler . apply ( new NodeTemplatePlugin ( outputOptions ) ) ;
30
64
childCompiler . apply ( new LibraryTemplatePlugin ( null , "commonjs2" ) ) ;
31
65
childCompiler . apply ( new NodeTargetPlugin ( ) ) ;
32
- childCompiler . apply ( new SingleEntryPlugin ( this . context , "!!" + request ) ) ;
66
+ childCompiler . apply ( new SingleEntryPlugin ( loader . context , "!!" + request ) ) ;
33
67
childCompiler . apply ( new LimitChunkCountPlugin ( { maxChunks : 1 } ) ) ;
34
68
35
69
var subCache = "subcache " + __dirname + " " + request ;
36
70
37
71
childCompiler . plugin ( "compilation" , function ( compilation ) {
38
72
if ( compilation . cache ) {
39
- if ( ! compilation . cache [ subCache ] )
73
+ if ( ! compilation . cache [ subCache ] ) {
40
74
compilation . cache [ subCache ] = { } ;
75
+ }
41
76
compilation . cache = compilation . cache [ subCache ] ;
42
77
}
43
78
} ) ;
@@ -47,12 +82,15 @@ module.exports.pitch = function stylingPitch(request, precedingRequest) {
47
82
childCompiler . plugin ( "this-compilation" , function ( compilation ) {
48
83
compilation . plugin ( "normal-module-loader" , function ( loaderContext ) {
49
84
loaderContext [ __dirname ] = false ;
85
+ if ( extractTextWebpackPluginKey in loader ) {
86
+ loaderContext [ extractTextWebpackPluginKey ] = loader [ extractTextWebpackPluginKey ] ;
87
+ }
50
88
} ) ;
51
89
} ) ;
52
90
53
91
var source ;
54
92
childCompiler . plugin ( "after-compile" , function ( compilation , callback ) {
55
- source = compilation . assets [ childFilename ] && compilation . assets [ childFilename ] . source ( ) ;
93
+ source = compilation . assets [ outputFilename ] && compilation . assets [ outputFilename ] . source ( ) ;
56
94
57
95
// Remove all chunk assets
58
96
compilation . chunks . forEach ( function ( chunk ) {
@@ -64,11 +102,13 @@ module.exports.pitch = function stylingPitch(request, precedingRequest) {
64
102
callback ( ) ;
65
103
} ) ;
66
104
67
- var callback = this . async ( ) ;
105
+ function callback ( error , result ) {
106
+ cb ( error , result ) ;
107
+ }
68
108
69
- childCompiler . runAsChild ( function ( err , entries , compilation ) {
70
- if ( err ) {
71
- return callback ( err ) ;
109
+ childCompiler . runAsChild ( function ( error , entries , compilation ) {
110
+ if ( error ) {
111
+ return callback ( error ) ;
72
112
}
73
113
if ( compilation . errors . length > 0 ) {
74
114
return callback ( compilation . errors [ 0 ] ) ;
@@ -77,23 +117,14 @@ module.exports.pitch = function stylingPitch(request, precedingRequest) {
77
117
return callback ( new Error ( "Didn't get a result from child compiler" ) ) ;
78
118
}
79
119
compilation . fileDependencies . forEach ( function ( dep ) {
80
- this . addDependency ( dep ) ;
81
- } , this ) ;
120
+ loader . addDependency ( dep ) ;
121
+ } ) ;
82
122
compilation . contextDependencies . forEach ( function ( dep ) {
83
- this . addContextDependency ( dep ) ;
84
- } , this ) ;
123
+ loader . addContextDependency ( dep ) ;
124
+ } ) ;
85
125
try {
86
- var exports = this . exec ( source , request ) ;
87
- var stylesheet = [ ] ;
88
- for ( var key in exports ) {
89
- if ( exports . hasOwnProperty ( key ) ) {
90
- var styling = exports [ key ] ;
91
- if ( Styling . is ( styling ) ) {
92
- stylesheet = stylesheet . concat ( renderStyling ( key , styling ) ) ;
93
- }
94
- }
95
- }
96
- var text = stylesheet . join ( '\n\n' ) ;
126
+ var exports = loader . exec ( source , request ) ;
127
+ var text = renderStylingSheet ( exports ) ;
97
128
} catch ( e ) {
98
129
return callback ( e ) ;
99
130
}
@@ -102,5 +133,41 @@ module.exports.pitch = function stylingPitch(request, precedingRequest) {
102
133
} else {
103
134
callback ( ) ;
104
135
}
105
- } . bind ( this ) ) ;
106
- } ;
136
+ } ) ;
137
+ }
138
+
139
+ function findInArray ( array , func ) {
140
+ for ( var i = 0 ; i < array . length ; i ++ ) {
141
+ if ( func ( array [ i ] ) ) {
142
+ return i ;
143
+ }
144
+ }
145
+ return - 1 ;
146
+ }
147
+
148
+ function getRootCompilation ( loader ) {
149
+ var compiler = loader . _compiler ;
150
+ var compilation = loader . _compilation ;
151
+ while ( compiler . parentCompilation ) {
152
+ compilation = compiler . parentCompilation ;
153
+ compiler = compilation . compiler ;
154
+ }
155
+ return compilation ;
156
+ }
157
+
158
+ function setCompiledStyling ( loader , styling ) {
159
+ var compilation = getRootCompilation ( loader ) ;
160
+ if ( compilation . __stylingCache === undefined ) {
161
+ compilation . __stylingCache = { } ;
162
+ }
163
+ compilation . __stylingCache [ loader . request ] = styling ;
164
+ }
165
+
166
+ function getCompiledStyling ( loader ) {
167
+ var compilation = getRootCompilation ( loader ) ;
168
+ if ( compilation . __stylingCache === undefined ) {
169
+ return undefined ;
170
+ } else {
171
+ return compilation . __stylingCache [ loader . request ] ;
172
+ }
173
+ }
0 commit comments