1
1
import debug from 'debug' ;
2
- import genericNames from 'generic-names' ;
3
2
import hook from './hook' ;
4
- import { readFileSync } from 'fs' ;
5
- import { dirname , sep , relative , resolve } from 'path' ;
6
- import { get , removeQuotes } from './utility' ;
7
- import assign from 'lodash.assign' ;
8
3
import identity from 'lodash.identity' ;
9
- import pick from 'lodash.pick' ;
10
- import postcss from 'postcss' ;
11
-
12
- import Values from 'postcss-modules-values' ;
13
- import ExtractImports from 'postcss-modules-extract-imports' ;
14
- import LocalByDefault from 'postcss-modules-local-by-default' ;
15
- import Scope from 'postcss-modules-scope' ;
16
- import Parser from './parser' ;
17
-
18
- const debugFetch = debug ( 'css-modules:fetch' ) ;
19
- const debugSetup = debug ( 'css-modules:setup' ) ;
4
+ import { get } from './extractor' ;
5
+ import { readFileSync } from 'fs' ;
6
+ import { dirname , resolve } from 'path' ;
7
+ import { removeQuotes } from './utility' ;
20
8
21
9
// cache
22
- let importNr = 0 ;
23
10
let tokensByFile = { } ;
24
- // processing functions
11
+ // global
12
+ let extractorOptions ;
13
+ let processorOptions = { } ;
25
14
let preProcess = identity ;
26
15
let postProcess ;
27
- // defaults
28
- let lazyResultOpts = { } ;
29
- let plugins = [ Values , LocalByDefault , ExtractImports ] ;
30
- let terminalPlugins = [ ] ;
31
- let rootDir = process . cwd ( ) ;
32
- let generateScopedName = genericNames ( '[name]__[local]___[hash:base64:5]' , { context : rootDir } ) ;
16
+
17
+ const debugFetch = debug ( 'css-modules:fetch' ) ;
18
+ const debugSetup = debug ( 'css-modules:setup' ) ;
33
19
34
20
/**
35
- * @param {object } opts
36
- * @param {function } opts.createImportedName
37
- * @param {function } opts.generateScopedName
38
- * @param {function } opts.preprocessCss
39
- * @param {function } opts.processCss
40
- * @param {string } opts.rootDir
41
- * @param {string } opts.to
42
- * @param {array } opts.use
43
- * @param {array } opts.extensions
21
+ * @param {array } options.extensions
22
+ * @param {function } options.preprocessCss
23
+ * @param {function } options.processCss
24
+ * @param {string } options.to
25
+ * @param {object } options.rest
44
26
*/
45
- export default function setup ( opts = { } ) {
46
- debugSetup ( opts ) ;
47
- // clearing cache
48
- importNr = 0 ;
49
- tokensByFile = { } ;
27
+ export default function setup ( { extensions : extraExtensions , preprocessCss, processCss, to, ...rest } = { } ) {
28
+ debugSetup ( arguments [ 0 ] ) ;
29
+ extractorOptions = rest ;
30
+ processorOptions = { to} ;
31
+ preProcess = preprocessCss || identity ;
32
+ postProcess = processCss || null ;
50
33
51
- preProcess = get ( 'preprocessCss' , null , 'function' , opts ) || identity ;
52
- postProcess = get ( 'processCss' , null , 'function' , opts ) || null ;
53
- rootDir = get ( 'rootDir' , [ 'root' , 'd' ] , 'string' , opts ) || process . cwd ( ) ;
54
- // https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts
55
- lazyResultOpts = pick ( opts , [ 'to' ] ) ;
56
-
57
- const extraExtensions = get ( 'extensions' , null , 'array' , opts ) ;
58
34
if ( extraExtensions ) {
59
- extraExtensions . forEach ( ( extension ) => {
60
- hook ( filename => fetch ( filename , filename ) , extension ) ;
61
- } ) ;
62
- }
63
-
64
- // Warning. Options, which aren't affected by plugins, should be processed above.
65
- const customPlugins = get ( 'use' , [ 'u' ] , 'array' , opts ) ;
66
- if ( customPlugins ) {
67
- return void ( plugins = customPlugins ) ;
35
+ extraExtensions . forEach ( ( extension ) => hook ( filename => fetch ( filename , filename ) , extension ) ) ;
68
36
}
69
-
70
- terminalPlugins = get ( 'append' , null , 'array' , opts ) || [ ] ;
71
- const prepend = get ( 'prepend' , null , 'array' , opts ) || [ ] ;
72
- const mode = get ( 'mode' , null , 'string' , opts ) ;
73
- const createImportedName = get ( 'createImportedName' , null , 'function' , opts ) ;
74
- const scopeOption = get ( 'generateScopedName' , null , [ 'function' , 'string' ] , opts )
75
- || genericNames ( '[name]__[local]___[hash:base64:5]' , { context : rootDir } ) ;
76
- generateScopedName = typeof scopeOption === 'string'
77
- ? genericNames ( scopeOption , { context : rootDir } )
78
- : scopeOption ;
79
-
80
- plugins = [
81
- ...prepend ,
82
- Values ,
83
- mode
84
- ? new LocalByDefault ( { mode : opts . mode } )
85
- : LocalByDefault ,
86
- createImportedName
87
- ? new ExtractImports ( { createImportedName : opts . createImportedName } )
88
- : ExtractImports ,
89
- ] ;
90
37
}
91
38
92
39
/**
93
- * @param {string } _to Absolute or relative path. Also can be path to the Node.JS module.
94
- * @param {string } _from Absolute path (relative to root).
95
- * @param {string } _trace
40
+ * @param {string } _to Absolute or relative path. Also can be path to the Node.JS module.
41
+ * @param {string } from Absolute path.
96
42
* @return {object }
97
43
*/
98
- function fetch ( _to , _from , _trace ) {
99
- const trace = _trace || String . fromCharCode ( importNr ++ ) ;
100
- const newPath = removeQuotes ( _to ) ;
44
+ function fetch ( _to , from ) {
45
+ const to = removeQuotes ( _to ) ;
101
46
// getting absolute path to the processing file
102
- const filename = / \w / . test ( newPath [ 0 ] )
103
- ? require . resolve ( newPath )
104
- : resolve ( dirname ( _from ) , newPath ) ;
47
+ const filename = / \w / i . test ( to [ 0 ] )
48
+ ? require . resolve ( to )
49
+ : resolve ( dirname ( from ) , to ) ;
105
50
106
51
// checking cache
107
52
let tokens = tokensByFile [ filename ] ;
@@ -111,19 +56,16 @@ function fetch(_to, _from, _trace) {
111
56
}
112
57
113
58
debugFetch ( { cache : false , filename} ) ;
114
- const rootRelativePath = sep + relative ( rootDir , filename ) ;
115
59
const CSSSource = preProcess ( readFileSync ( filename , 'utf8' ) , filename ) ;
60
+ // https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts
61
+ const lazyResult = get ( extractorOptions , fetch )
62
+ . process ( CSSSource , Object . assign ( processorOptions , { from : filename } ) ) ;
116
63
117
- const lazyResult = postcss ( plugins . concat (
118
- new Scope ( { generateScopedName : ( name , _ , css ) => generateScopedName ( name , filename , css ) } ) ,
119
- terminalPlugins ,
120
- new Parser ( { fetch, filename, trace } ) )
121
- ) . process ( CSSSource , assign ( lazyResultOpts , { from : rootRelativePath } ) ) ;
122
-
64
+ // https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings
123
65
lazyResult . warnings ( ) . forEach ( message => console . warn ( message . text ) ) ;
124
66
125
- tokens = lazyResult . root . tokens ;
126
67
// updating cache
68
+ tokens = lazyResult . root . tokens ;
127
69
tokensByFile [ filename ] = tokens ;
128
70
129
71
if ( postProcess ) {
0 commit comments