-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.js
154 lines (128 loc) · 4.37 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const {assign, identity, negate} = require('lodash');
const {dirname, relative, resolve} = require('path');
const {readFileSync} = require('fs');
const {transformTokens} = require('./transformTokens');
const attachHook = require('./attachHook');
const genericNames = require('generic-names');
const globToRegex = require('glob-to-regexp');
const validate = require('./validate');
const postcss = require('postcss');
const Values = require('postcss-modules-values');
const LocalByDefault = require('postcss-modules-local-by-default');
const ExtractImports = require('postcss-modules-extract-imports');
const Scope = require('postcss-modules-scope');
const ResolveImports = require('postcss-modules-resolve-imports');
const debugFetch = require('debug')('css-modules:fetch');
const debugSetup = require('debug')('css-modules:setup');
module.exports = function setupHook({
camelCase,
devMode,
extensions = '.css',
ignore,
preprocessCss = identity,
processCss,
processorOpts,
append = [],
prepend = [],
createImportedName,
generateScopedName,
hashPrefix,
mode,
resolve: resolveOpts,
use,
rootDir: context = process.cwd(),
}) {
debugSetup(arguments[0]);
validate(arguments[0]);
const exts = toArray(extensions);
const tokensByFile = {};
// debug option is preferred NODE_ENV === 'development'
const debugMode = typeof devMode !== 'undefined'
? devMode
: process.env.NODE_ENV === 'development';
let scopedName;
if (generateScopedName)
scopedName = typeof generateScopedName !== 'function'
? genericNames(generateScopedName, {context, hashPrefix}) // for example '[name]__[local]___[hash:base64:5]'
: generateScopedName;
else
// small fallback
scopedName = (local, filename) => Scope.generateScopedName(local, relative(context, filename));
const plugins = use || [
...prepend,
Values,
new LocalByDefault({mode, generateScopedName: scopedName}),
createImportedName
? new ExtractImports({createImportedName})
: ExtractImports,
new Scope({generateScopedName: scopedName}),
new ResolveImports({resolve: Object.assign({}, {extensions: exts}, resolveOpts)}),
...append,
];
// https://github.com/postcss/postcss#options
const runner = postcss(plugins);
/**
* @todo think about replacing sequential fetch function calls with requires calls
* @param {string} _to
* @param {string} from
* @return {object}
*/
function fetch(_to, from) {
// getting absolute path to the processing file
const filename = /[^\\/?%*:|"<>.]/i.test(_to[0])
? require.resolve(_to)
: resolve(dirname(from), _to);
// checking cache
let tokens = tokensByFile[filename];
if (tokens) {
debugFetch(`${filename} → cache`);
debugFetch(tokens);
return tokens;
}
const source = preprocessCss(readFileSync(filename, 'utf8'), filename);
// https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts
const lazyResult = runner.process(source, assign({}, processorOpts, {from: filename}));
// https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings
lazyResult.warnings().forEach(message => console.warn(message.text));
tokens = lazyResult.root.exports || {};
if (!debugMode)
// updating cache
tokensByFile[filename] = tokens;
else
// clearing cache in development mode
delete require.cache[filename];
if (processCss)
processCss(lazyResult.css, filename);
debugFetch(`${filename} → fs`);
debugFetch(tokens);
return tokens;
}
const isException = buildExceptionChecker(ignore);
const hook = filename => {
const tokens = fetch(filename, filename);
return camelCase ? transformTokens(tokens, camelCase) : tokens;
};
// @todo add possibility to specify particular config for each extension
exts.forEach(extension => attachHook(hook, extension, isException));
};
/**
* @param {*} option
* @return {array}
*/
function toArray(option) {
return Array.isArray(option)
? option
: [option];
}
/**
* @param {function|regex|string} ignore glob, regex or function
* @return {function}
*/
function buildExceptionChecker(ignore) {
if (ignore instanceof RegExp)
return filepath => ignore.test(filepath);
if (typeof ignore === 'string')
return filepath => globToRegex(ignore).test(filepath);
return ignore || negate(identity);
}