-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
65 lines (59 loc) · 1.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import postcss from 'postcss';
import genericNames from 'generic-names';
import { relative } from 'path';
import LocalByDefault from 'postcss-modules-local-by-default'
import ExtractImports from 'postcss-modules-extract-imports'
import Scope from 'postcss-modules-scope'
import Parser from 'postcss-modules-parser'
import Values from 'postcss-modules-values'
/**
* @param {array} options.append
* @param {array} options.prepend
* @param {array} options.use
* @param {function} options.createImportedName
* @param {function|string} options.generateScopedName
* @param {string} options.mode
* @param {string} options.rootDir
* @param {function} fetch
* @return {object}
*/
export default function core({
append = [],
prepend = [],
createImportedName,
generateScopedName: scopedName,
rootDir: context = process.cwd(),
mode,
use,
} = {}, _fetch) {
let instance
let generateScopedName
const fetch = function () {
return _fetch.apply(null, Array.prototype.slice.call(arguments).concat(instance));
}
if (scopedName) {
// https://github.com/css-modules/postcss-modules-scope/blob/master/src/index.js#L38
generateScopedName = typeof scopedName !== 'function'
? genericNames(scopedName || '[name]__[local]___[hash:base64:5]', {context})
: (local, filepath, css) => scopedName(local, filepath, css, context)
} else {
generateScopedName = (localName, filepath) => {
return Scope.generateScopedName(localName, relative(context, filepath));
}
}
const plugins = (use || [
...prepend,
Values,
mode
? new LocalByDefault({mode})
: LocalByDefault,
createImportedName
? new ExtractImports({createImportedName})
: ExtractImports,
new Scope({generateScopedName}),
...append,
])
.concat(new Parser({fetch})) // no pushing in order to avoid the possible mutations
instance = postcss(plugins)
return instance;
}