|
1 | 1 | import hook from './hook'; |
2 | 2 | import { readFileSync } from 'fs'; |
3 | 3 | import { dirname, sep, relative, resolve } from 'path'; |
4 | | -import { identity, removeQuotes } from './fn'; |
| 4 | +import { get, removeQuotes } from './utility'; |
| 5 | +import identity from 'lodash.identity'; |
5 | 6 | import postcss from 'postcss'; |
6 | 7 |
|
7 | 8 | import ExtractImports from 'postcss-modules-extract-imports'; |
@@ -32,57 +33,30 @@ export default function setup(opts = {}) { |
32 | 33 | importNr = 0; |
33 | 34 | tokensByFile = {}; |
34 | 35 |
|
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 | | - } |
| 36 | + postProcess = get('processCss', null, 'function', opts) || null; |
| 37 | + rootDir = get('rootDir', ['root', 'd'], 'string', opts) || process.cwd(); |
51 | 38 |
|
52 | | - return void (plugins = opts.use); |
| 39 | + const customPlugins = get('use', ['u'], 'array', opts); |
| 40 | + if (customPlugins) { |
| 41 | + return void (plugins = customPlugins); |
53 | 42 | } |
54 | 43 |
|
55 | 44 | plugins = []; |
56 | 45 |
|
57 | | - if (opts.mode) { |
58 | | - if (typeof opts.mode !== 'string') { |
59 | | - throw new Error('should specify string for mode'); |
60 | | - } |
| 46 | + const mode = get('mode', null, 'string', opts); |
| 47 | + plugins.push(mode |
| 48 | + ? new LocalByDefault({mode: opts.mode}) |
| 49 | + : LocalByDefault); |
61 | 50 |
|
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 | | - } |
| 51 | + const createImportedName = get('createImportedName', null, 'function', opts); |
| 52 | + plugins.push(createImportedName |
| 53 | + ? new ExtractImports({createImportedName: opts.createImportedName}) |
| 54 | + : ExtractImports); |
81 | 55 |
|
82 | | - plugins.push(new Scope({generateScopedName: opts.generateScopedName})); |
83 | | - } else { |
84 | | - plugins.push(Scope); |
85 | | - } |
| 56 | + const generateScopedName = get('generateScopedName', null, 'function', opts); |
| 57 | + plugins.push(generateScopedName |
| 58 | + ? new Scope({generateScopedName: opts.generateScopedName}) |
| 59 | + : Scope); |
86 | 60 | } |
87 | 61 |
|
88 | 62 | /** |
|
0 commit comments