@@ -4,7 +4,7 @@ if (!global.Promise) { global.Promise = require('promise-polyfill') }
4
4
var fs = require ( 'fs' ) ;
5
5
var path = require ( 'path' ) ;
6
6
var through = require ( 'through' ) ;
7
- var Core = require ( 'css-modules-loader-core ' ) ;
7
+ var extractor = require ( './extractor ' ) ;
8
8
var FileSystemLoader = require ( 'css-modules-loader-core/lib/file-system-loader' ) ;
9
9
var assign = require ( 'object-assign' ) ;
10
10
var stringHash = require ( 'string-hash' ) ;
@@ -14,7 +14,8 @@ var ReadableStream = require('stream').Readable;
14
14
Custom `generateScopedName` function for `postcss-modules-scope`.
15
15
Short names consisting of source hash and line number.
16
16
*/
17
- function generateShortName ( name , filename , css ) {
17
+ function generateShortName ( name , filename , css , context ) {
18
+ filename = path . relative ( context , filename ) ;
18
19
// first occurrence of the name
19
20
// TOOD: better match with regex
20
21
var i = css . indexOf ( '.' + name ) ;
@@ -28,7 +29,8 @@ function generateShortName (name, filename, css) {
28
29
Custom `generateScopedName` function for `postcss-modules-scope`.
29
30
Appends a hash of the css source.
30
31
*/
31
- function generateLongName ( name , filename ) {
32
+ function generateLongName ( name , filename , css , context ) {
33
+ filename = path . relative ( context , filename ) ;
32
34
var sanitisedPath = filename . replace ( / \. [ ^ \. \/ \\ ] + $ / , '' )
33
35
. replace ( / [ \W _ ] + / g, '_' )
34
36
. replace ( / ^ _ | _ $ / g, '' ) ;
@@ -93,59 +95,50 @@ var sourceByFile = {};
93
95
94
96
module . exports = function ( browserify , options ) {
95
97
options = options || { } ;
96
-
97
- // if no root directory is specified, assume the cwd
98
- var rootDir = options . rootDir || options . d ;
99
- if ( rootDir ) { rootDir = path . resolve ( rootDir ) ; }
100
- if ( ! rootDir ) { rootDir = process . cwd ( ) ; }
98
+ options . rootDir = options . rootDir || options . d || undefined ;
99
+ options . append = options . postcssAfter || options . after || [ ] ;
100
+ options . use = options . use || options . u || undefined ;
101
101
102
102
var cssOutFilename = options . output || options . o ;
103
103
var jsonOutFilename = options . json || options . jsonOutput ;
104
104
105
- // PostCSS plugins passed to FileSystemLoader
106
- var plugins = options . use || options . u ;
107
- if ( ! plugins ) {
108
- plugins = getDefaultPlugins ( options ) ;
109
- }
110
- else {
111
- if ( typeof plugins === 'string' ) {
112
- plugins = [ plugins ] ;
113
- }
114
- }
115
-
116
- var postcssAfter = options . postcssAfter || options . after || [ ] ;
117
- plugins = plugins . concat ( postcssAfter ) ;
118
-
119
- // load plugins by name (if a string is used)
120
- plugins = plugins . map ( function requirePlugin ( name ) {
121
- // assume functions are already required plugins
122
- if ( typeof name === 'function' ) {
123
- return name ;
124
- }
125
-
126
- var plugin = require ( require . resolve ( name ) ) ;
127
-
128
- // custom scoped name generation
129
- if ( name === 'postcss-modules-scope' ) {
130
- options [ name ] = options [ name ] || { } ;
131
- if ( ! options [ name ] . generateScopedName ) {
132
- options [ name ] . generateScopedName = generateLongName ;
105
+ // the compiled CSS stream needs to be avalible to the transform,
106
+ // but re-created on each bundle call.
107
+ var compiledCssStream ;
108
+ var instance = extractor ( options , fetch ) ;
109
+
110
+ function fetch ( _to , from ) {
111
+ var to = _to . replace ( / ^ [ " ' ] | [ " ' ] $ / g, '' ) ;
112
+
113
+ return new Promise ( ( resolve , reject ) => {
114
+ try {
115
+ var filename = / \w / i. test ( to [ 0 ] )
116
+ ? require . resolve ( to )
117
+ : path . resolve ( path . dirname ( from ) , to ) ;
118
+ } catch ( e ) {
119
+ return void reject ( e ) ;
133
120
}
134
- }
135
121
136
- if ( name in options ) {
137
- plugin = plugin ( options [ name ] ) ;
138
- }
139
- else {
140
- plugin = plugin . postcss || plugin ( ) ;
141
- }
122
+ fs . readFile ( filename , 'utf8' , ( err , css ) => {
123
+ if ( err ) {
124
+ return void reject ( err ) ;
125
+ }
142
126
143
- return plugin ;
144
- } ) ;
127
+ instance . process ( css , { from : filename } )
128
+ . then ( function ( result ) {
129
+ var css = result . css ;
130
+ var tokens = result . root . tokens ;
145
131
146
- // the compiled CSS stream needs to be avalible to the transform,
147
- // but re-created on each bundle call.
148
- var compiledCssStream ;
132
+ assign ( tokensByFile , tokens ) ;
133
+ sourceByFile [ filename ] = css ;
134
+ compiledCssStream . push ( css ) ;
135
+
136
+ resolve ( tokens ) ;
137
+ } )
138
+ . catch ( reject ) ;
139
+ } ) ;
140
+ } ) ;
141
+ }
149
142
150
143
function transform ( filename ) {
151
144
// only handle .css files
@@ -156,25 +149,19 @@ module.exports = function (browserify, options) {
156
149
// collect visited filenames
157
150
filenames . push ( filename ) ;
158
151
159
- var loader = new FileSystemLoader ( rootDir , plugins ) ;
160
152
return through ( function noop ( ) { } , function end ( ) {
161
153
var self = this ;
162
154
163
- loader . fetch ( path . relative ( rootDir , filename ) , '/' ) . then ( function ( tokens ) {
164
- var output = 'module.exports = ' + JSON . stringify ( tokens ) ;
165
-
166
- assign ( tokensByFile , loader . tokensByFile ) ;
167
-
168
- // store this file's source to be written out to disk later
169
- sourceByFile [ filename ] = loader . finalSource ;
155
+ fetch ( filename , filename )
156
+ . then ( function ( tokens ) {
157
+ var output = 'module.exports = ' + JSON . stringify ( tokens ) ;
170
158
171
- compiledCssStream . push ( loader . finalSource ) ;
172
-
173
- self . queue ( output ) ;
174
- self . queue ( null ) ;
175
- } , function ( err ) {
176
- self . emit ( 'error' , err ) ;
177
- } ) ;
159
+ self . queue ( output ) ;
160
+ self . queue ( null ) ;
161
+ } )
162
+ . catch ( function ( err ) {
163
+ self . emit ( 'error' , err ) ;
164
+ } ) ;
178
165
} ) ;
179
166
}
180
167
0 commit comments