@@ -3,21 +3,84 @@ import hook from './hook';
3
3
import postcss from 'postcss' ;
4
4
import { dirname , join , parse , relative , resolve , sep } from 'path' ;
5
5
import { readFileSync } from 'fs' ;
6
+ import isPlainObject from 'lodash.isplainobject' ;
6
7
7
8
import ExtractImports from 'postcss-modules-extract-imports' ;
8
9
import LocalByDefault from 'postcss-modules-local-by-default' ;
9
10
import Scope from 'postcss-modules-scope' ;
10
11
import Parser from './parser' ;
11
12
13
+ let processCss ;
14
+ let rootDir ;
15
+ let plugins ;
16
+
17
+ /**
18
+ * @param {object } opts
19
+ * @param {function } opts.generateScopedName
20
+ * @param {function } opts.processCss|.p
21
+ * @param {string } opts.rootDir|.root|.d
22
+ * @param {array } opts.use|.u
23
+ */
24
+ export default function buildOptions ( opts = { } ) {
25
+ if ( ! isPlainObject ( opts ) ) {
26
+ throw new Error ( 'Use plain object' ) ;
27
+ }
28
+
29
+ processCss = get ( opts , 'processCss|p' ) ;
30
+ rootDir = get ( opts , 'rootDir|root|d' ) ;
31
+ rootDir = rootDir ? resolve ( rootDir ) : process . cwd ( ) ;
32
+
33
+ const customPlugins = get ( opts , 'use|u' ) ;
34
+ if ( Array . isArray ( customPlugins ) ) {
35
+ return void ( plugins = customPlugins ) ;
36
+ }
37
+
38
+ plugins = [ ] ;
39
+
40
+ plugins . push (
41
+ opts . mode
42
+ ? new LocalByDefault ( { mode : opts . mode } )
43
+ : LocalByDefault
44
+ ) ;
45
+
46
+ plugins . push (
47
+ opts . createImportedName
48
+ ? new ExtractImports ( { createImportedName : opts . createImportedName } )
49
+ : ExtractImports
50
+ ) ;
51
+
52
+ plugins . push (
53
+ opts . generateScopedName
54
+ ? new Scope ( { generateScopedName : opts . generateScopedName } )
55
+ : Scope
56
+ ) ;
57
+ }
58
+
12
59
const escapedSeparator = sep . replace ( / ( .) / g, '\\$1' ) ;
13
60
const relativePathPattern = new RegExp ( `^.{1,2}$|^.{1,2}${ escapedSeparator } ` ) ;
14
-
15
- const defaultRoot = process . cwd ( ) ;
16
61
const tokensByFile = { } ;
17
- let plugins = [ LocalByDefault , ExtractImports , Scope ] ;
18
- let root = defaultRoot ;
19
62
let importNr = 0 ;
20
63
64
+ /**
65
+ * @param {object } object
66
+ * @param {string } keys 'a|b|c'
67
+ * @return {* }
68
+ */
69
+ function get ( object , keys ) {
70
+ let key ;
71
+
72
+ keys . split ( '|' ) . some ( k => {
73
+ if ( ! object [ k ] ) {
74
+ return false ;
75
+ }
76
+
77
+ key = k ;
78
+ return true ;
79
+ } ) ;
80
+
81
+ return key ? object [ key ] : null ;
82
+ }
83
+
21
84
/**
22
85
* @param {string } pathname
23
86
* @return {boolean }
@@ -38,7 +101,7 @@ function load(sourceString, sourcePath, trace, pathFetcher) {
38
101
const lazyResult = postcss ( plugins . concat ( new Parser ( { pathFetcher, trace } ) ) )
39
102
. process ( sourceString , { from : sourcePath } ) ;
40
103
41
- return { injectableSource : lazyResult . css , exportTokens : lazyResult . root . tokens } ; ;
104
+ return { injectableSource : lazyResult . css , exportTokens : lazyResult . root . tokens } ;
42
105
}
43
106
44
107
/**
@@ -53,7 +116,7 @@ function fetch(_newPath, _relativeTo, _trace) {
53
116
54
117
const relativeDir = dirname ( _relativeTo ) ;
55
118
const rootRelativePath = resolve ( relativeDir , newPath ) ;
56
- let fileRelativePath = resolve ( join ( root , relativeDir ) , newPath ) ;
119
+ let fileRelativePath = resolve ( join ( rootDir , relativeDir ) , newPath ) ;
57
120
58
121
if ( isModule ( newPath ) ) {
59
122
fileRelativePath = require . resolve ( newPath ) ;
@@ -69,23 +132,14 @@ function fetch(_newPath, _relativeTo, _trace) {
69
132
70
133
tokensByFile [ fileRelativePath ] = exportTokens ;
71
134
135
+ if ( typeof processCss === 'function' ) {
136
+ processCss ( injectableSource ) ;
137
+ }
138
+
72
139
return exportTokens ;
73
140
}
74
141
75
- hook ( filename => fetch ( `.${ sep } ${ relative ( root , filename ) } ` , '/' ) ) ;
142
+ // setting defaults
143
+ buildOptions ( ) ;
76
144
77
- /**
78
- * @param {object } opts
79
- * @param {array } opts.u
80
- * @param {array } opts.use
81
- */
82
- export default function configure ( opts = { } ) {
83
- const customPlugins = opts . u || opts . use ;
84
- plugins = Array . isArray ( customPlugins )
85
- ? customPlugins
86
- : [ LocalByDefault , ExtractImports , Scope ] ;
87
-
88
- root = opts . root && typeof opts . root === 'string'
89
- ? opts . root
90
- : defaultRoot ;
91
- }
145
+ hook ( filename => fetch ( `.${ sep } ${ relative ( rootDir , filename ) } ` , '/' ) ) ;
0 commit comments