Skip to content

Commit 4937248

Browse files
committed
small refactoring
1 parent 94e2189 commit 4937248

File tree

1 file changed

+34
-92
lines changed

1 file changed

+34
-92
lines changed

src/index.js

Lines changed: 34 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,52 @@
11
import debug from 'debug';
2-
import genericNames from 'generic-names';
32
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';
83
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';
208

219
// cache
22-
let importNr = 0;
2310
let tokensByFile = {};
24-
// processing functions
11+
// global
12+
let extractorOptions;
13+
let processorOptions = {};
2514
let preProcess = identity;
2615
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');
3319

3420
/**
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
4426
*/
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;
5033

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);
5834
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));
6836
}
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-
];
9037
}
9138

9239
/**
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.
9642
* @return {object}
9743
*/
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);
10146
// 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);
10550

10651
// checking cache
10752
let tokens = tokensByFile[filename];
@@ -111,19 +56,16 @@ function fetch(_to, _from, _trace) {
11156
}
11257

11358
debugFetch({cache: false, filename});
114-
const rootRelativePath = sep + relative(rootDir, filename);
11559
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}));
11663

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
12365
lazyResult.warnings().forEach(message => console.warn(message.text));
12466

125-
tokens = lazyResult.root.tokens;
12667
// updating cache
68+
tokens = lazyResult.root.tokens;
12769
tokensByFile[filename] = tokens;
12870

12971
if (postProcess) {

0 commit comments

Comments
 (0)