11import debug from 'debug' ;
22import 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' ;
73import 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' ;
1910
2011// cache
21- let importNr = 0 ;
2212let tokensByFile = { } ;
23- // processing functions
13+ // global
14+ let instance = extractor ( { } , fetch ) ;
15+ let processorOptions = { } ;
2416let preProcess = identity ;
2517let 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' ) ;
3021
3122/**
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
4128 */
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 ;
4436 // clearing cache
45- importNr = 0 ;
4637 tokensByFile = { } ;
4738
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 ) ;
5539 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 ) ) ;
6541 }
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- ] ;
8742}
8843
8944/**
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.
9347 * @return {object }
9448 */
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 ) ;
9851 // 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 ) ;
10255
10356 // checking cache
10457 let tokens = tokensByFile [ filename ] ;
@@ -108,16 +61,15 @@ function fetch(_to, _from, _trace) {
10861 }
10962
11063 debugFetch ( { cache : false , filename} ) ;
111- const rootRelativePath = sep + relative ( rootDir , filename ) ;
11264 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 } ) ) ;
11367
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
11769 lazyResult . warnings ( ) . forEach ( message => console . warn ( message . text ) ) ;
11870
119- tokens = lazyResult . root . tokens ;
12071 // updating cache
72+ tokens = lazyResult . root . tokens ;
12173 tokensByFile [ filename ] = tokens ;
12274
12375 if ( postProcess ) {
0 commit comments