@@ -3,21 +3,84 @@ import hook from './hook';
33import postcss from 'postcss' ;
44import { dirname , join , parse , relative , resolve , sep } from 'path' ;
55import { readFileSync } from 'fs' ;
6+ import isPlainObject from 'lodash.isplainobject' ;
67
78import ExtractImports from 'postcss-modules-extract-imports' ;
89import LocalByDefault from 'postcss-modules-local-by-default' ;
910import Scope from 'postcss-modules-scope' ;
1011import Parser from './parser' ;
1112
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+
1259const escapedSeparator = sep . replace ( / ( .) / g, '\\$1' ) ;
1360const relativePathPattern = new RegExp ( `^.{1,2}$|^.{1,2}${ escapedSeparator } ` ) ;
14-
15- const defaultRoot = process . cwd ( ) ;
1661const tokensByFile = { } ;
17- let plugins = [ LocalByDefault , ExtractImports , Scope ] ;
18- let root = defaultRoot ;
1962let importNr = 0 ;
2063
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+
2184/**
2285 * @param {string } pathname
2386 * @return {boolean }
@@ -38,7 +101,7 @@ function load(sourceString, sourcePath, trace, pathFetcher) {
38101 const lazyResult = postcss ( plugins . concat ( new Parser ( { pathFetcher, trace } ) ) )
39102 . process ( sourceString , { from : sourcePath } ) ;
40103
41- return { injectableSource : lazyResult . css , exportTokens : lazyResult . root . tokens } ; ;
104+ return { injectableSource : lazyResult . css , exportTokens : lazyResult . root . tokens } ;
42105}
43106
44107/**
@@ -53,7 +116,7 @@ function fetch(_newPath, _relativeTo, _trace) {
53116
54117 const relativeDir = dirname ( _relativeTo ) ;
55118 const rootRelativePath = resolve ( relativeDir , newPath ) ;
56- let fileRelativePath = resolve ( join ( root , relativeDir ) , newPath ) ;
119+ let fileRelativePath = resolve ( join ( rootDir , relativeDir ) , newPath ) ;
57120
58121 if ( isModule ( newPath ) ) {
59122 fileRelativePath = require . resolve ( newPath ) ;
@@ -69,23 +132,14 @@ function fetch(_newPath, _relativeTo, _trace) {
69132
70133 tokensByFile [ fileRelativePath ] = exportTokens ;
71134
135+ if ( typeof processCss === 'function' ) {
136+ processCss ( injectableSource ) ;
137+ }
138+
72139 return exportTokens ;
73140}
74141
75- hook ( filename => fetch ( `.${ sep } ${ relative ( root , filename ) } ` , '/' ) ) ;
142+ // setting defaults
143+ buildOptions ( ) ;
76144
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