-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathextractor.js
70 lines (64 loc) · 2.31 KB
/
extractor.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
66
67
68
69
70
var postcss = require('postcss');
var genericNames = require('generic-names');
var path = require('path');
var Values = require('postcss-modules-values');
var LocalByDefault = require('postcss-modules-local-by-default');
var ExtractImports = require('postcss-modules-extract-imports');
var Scope = require('postcss-modules-scope');
var Parser = require('postcss-modules-parser');
/**
* @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}
*/
module.exports = function extractor(options, fetch) {
options = options || {};
var append = options.append;
var prepend = options.prepend;
var createImportedName = options.createImportedName;
var generateScopedName = options.generateScopedName;
var mode = options.mode;
var use = options.use;
var context = options.rootDir || process.cwd();
var scopedName;
if (generateScopedName) {
scopedName = typeof generateScopedName !== 'function'
? genericNames(generateScopedName || '[name]__[local]___[hash:base64:5]', {context: context})
: function (local, filename, css) {
// had to wrap that function cause i didn't expected,
// that generateShortName() and generateLongName() functions
// use the fake path to file (relative to rootDir)
// which result in the generated class names
return generateScopedName(local, filename, css, context);
};
} else {
// small fallback
scopedName = function (local, filename) {
return Scope.generateScopedName(local, path.relative(context, filename));
}
}
var plugins;
if (use) {
plugins = use;
} else {
plugins = (prepend || [])
.concat([
Values,
mode
? new LocalByDefault({mode: mode})
: LocalByDefault,
createImportedName
? new ExtractImports({createImportedName: createImportedName})
: ExtractImports,
new Scope({generateScopedName: scopedName}),
], append || []);
}
plugins = plugins.concat(new Parser({fetch: fetch}));
return postcss(plugins);
}