11import hook from './hook' ;
22import { readFileSync } from 'fs' ;
33import { dirname , sep , relative , resolve } from 'path' ;
4- import { identity , removeQuotes } from './fn' ;
4+ import { get , removeQuotes } from './utility' ;
5+ import assign from 'lodash.assign' ;
6+ import identity from 'lodash.identity' ;
7+ import pick from 'lodash.pick' ;
58import postcss from 'postcss' ;
69
710import ExtractImports from 'postcss-modules-extract-imports' ;
@@ -16,73 +19,50 @@ let tokensByFile = {};
1619const preProcess = identity ;
1720let postProcess ;
1821// defaults
22+ let lazyResultOpts = { } ;
1923let plugins = [ LocalByDefault , ExtractImports , Scope ] ;
2024let rootDir = process . cwd ( ) ;
2125
2226/**
23- * @param {object } opts
27+ * @param {object } opts
2428 * @param {function } opts.createImportedName
2529 * @param {function } opts.generateScopedName
2630 * @param {function } opts.processCss
2731 * @param {string } opts.rootDir
32+ * @param {string } opts.to
2833 * @param {array } opts.use
2934 */
3035export default function setup ( opts = { } ) {
3136 // clearing cache
3237 importNr = 0 ;
3338 tokensByFile = { } ;
3439
35- if ( opts . processCss && typeof opts . processCss !== 'function' ) {
36- throw new Error ( 'should specify function for processCss' ) ;
37- }
38-
39- postProcess = opts . processCss || null ;
40-
41- if ( opts . rootDir && typeof opts . rootDir !== 'string' ) {
42- throw new Error ( 'should specify string for rootDir' ) ;
43- }
44-
45- rootDir = opts . rootDir || process . cwd ( ) ;
46-
47- if ( opts . use ) {
48- if ( ! Array . isArray ( opts . use ) ) {
49- throw new Error ( 'should specify array for use' ) ;
50- }
40+ postProcess = get ( 'processCss' , null , 'function' , opts ) || null ;
41+ rootDir = get ( 'rootDir' , [ 'root' , 'd' ] , 'string' , opts ) || process . cwd ( ) ;
42+ // https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts
43+ lazyResultOpts = pick ( opts , [ 'to' ] ) ;
5144
52- return void ( plugins = opts . use ) ;
45+ const customPlugins = get ( 'use' , [ 'u' ] , 'array' , opts ) ;
46+ if ( customPlugins ) {
47+ return void ( plugins = customPlugins ) ;
5348 }
5449
5550 plugins = [ ] ;
5651
57- if ( opts . mode ) {
58- if ( typeof opts . mode !== 'string' ) {
59- throw new Error ( 'should specify string for mode' ) ;
60- }
52+ const mode = get ( ' mode' , null , 'string' , opts ) ;
53+ plugins . push ( mode
54+ ? new LocalByDefault ( { mode : opts . mode } )
55+ : LocalByDefault ) ;
6156
62- plugins . push ( new LocalByDefault ( { mode : opts . mode } ) ) ;
63- } else {
64- plugins . push ( LocalByDefault ) ;
65- }
66-
67- if ( opts . createImportedName ) {
68- if ( typeof opts . createImportedName !== 'function' ) {
69- throw new Error ( 'should specify function for createImportedName' ) ;
70- }
71-
72- plugins . push ( new ExtractImports ( { createImportedName : opts . createImportedName } ) ) ;
73- } else {
74- plugins . push ( ExtractImports ) ;
75- }
76-
77- if ( opts . generateScopedName ) {
78- if ( typeof opts . generateScopedName !== 'function' ) {
79- throw new Error ( 'should specify function for generateScopedName' ) ;
80- }
57+ const createImportedName = get ( 'createImportedName' , null , 'function' , opts ) ;
58+ plugins . push ( createImportedName
59+ ? new ExtractImports ( { createImportedName : opts . createImportedName } )
60+ : ExtractImports ) ;
8161
82- plugins . push ( new Scope ( { generateScopedName : opts . generateScopedName } ) ) ;
83- } else {
84- plugins . push ( Scope ) ;
85- }
62+ const generateScopedName = get ( ' generateScopedName' , null , 'function' , opts ) ;
63+ plugins . push ( generateScopedName
64+ ? new Scope ( { generateScopedName : opts . generateScopedName } )
65+ : Scope ) ;
8666}
8767
8868/**
@@ -109,7 +89,7 @@ function fetch(_newPath, _sourcePath, _trace) {
10989 const CSSSource = preProcess ( readFileSync ( filename , 'utf8' ) ) ;
11090
11191 const result = postcss ( plugins . concat ( new Parser ( { fetch, trace } ) ) )
112- . process ( CSSSource , { from : rootRelativePath } )
92+ . process ( CSSSource , assign ( lazyResultOpts , { from : rootRelativePath } ) )
11393 . root ;
11494
11595 tokens = result . tokens ;
0 commit comments