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