-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathrequireCssModule.js
138 lines (111 loc) · 3.79 KB
/
requireCssModule.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
// @flow
import {
dirname,
resolve
} from 'path';
import {
readFileSync
} from 'fs';
import postcss from 'postcss';
import genericNames from 'generic-names';
import ExtractImports from 'postcss-modules-extract-imports';
import LocalByDefault from 'postcss-modules-local-by-default';
import Parser from 'postcss-modules-parser';
import Scope from 'postcss-modules-scope';
import Values from 'postcss-modules-values';
import type {
GenerateScopedNameConfigurationType,
StyleModuleMapType
} from './types';
import findMatchedFiletype from './findMatchedFiletype';
import optionsDefaults from './schemas/optionsDefaults';
type FiletypeOptionsType = {|
+syntax: string,
+plugins?: $ReadOnlyArray<string | $ReadOnlyArray<[string, mixed]>>
|};
type FiletypesConfigurationType = {
[key: string]: FiletypeOptionsType
};
type OptionsType = {|
filetypes: FiletypesConfigurationType,
generateScopedName?: GenerateScopedNameConfigurationType,
context?: string
|};
const getFiletypeOptions = (cssSourceFilePath: string, filetypes: FiletypesConfigurationType): ?FiletypeOptionsType => {
const matchedKey = findMatchedFiletype(cssSourceFilePath, Object.keys(filetypes));
return matchedKey ? filetypes && filetypes[matchedKey] : null;
};
// eslint-disable-next-line flowtype/no-weak-types
const getSyntax = (filetypeOptions: FiletypeOptionsType): ?(Function | Object) => {
if (!filetypeOptions || !filetypeOptions.syntax) {
return null;
}
// eslint-disable-next-line import/no-dynamic-require, global-require
return require(filetypeOptions.syntax);
};
// eslint-disable-next-line flowtype/no-weak-types
const getExtraPlugins = (filetypeOptions: ?FiletypeOptionsType): $ReadOnlyArray<*> => {
if (!filetypeOptions || !filetypeOptions.plugins) {
return [];
}
return filetypeOptions.plugins.map((plugin) => {
if (Array.isArray(plugin)) {
const [pluginName, pluginOptions] = plugin;
// eslint-disable-next-line import/no-dynamic-require, global-require
return require(pluginName)(pluginOptions);
}
// eslint-disable-next-line import/no-dynamic-require, global-require
return require(plugin);
});
};
const getTokens = (runner, cssSourceFilePath: string, filetypeOptions: ?FiletypeOptionsType): StyleModuleMapType => {
// eslint-disable-next-line flowtype/no-weak-types
const options: Object = {
from: cssSourceFilePath
};
if (filetypeOptions) {
options.syntax = getSyntax(filetypeOptions);
}
const lazyResult = runner
.process(readFileSync(cssSourceFilePath, 'utf-8'), options);
lazyResult
.warnings()
.forEach((message) => {
// eslint-disable-next-line no-console
console.warn(message.text);
});
return lazyResult.root.tokens;
};
export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMapType => {
// eslint-disable-next-line prefer-const
let runner;
let generateScopedName;
if (options.generateScopedName && typeof options.generateScopedName === 'function') {
generateScopedName = options.generateScopedName;
} else {
generateScopedName = genericNames(options.generateScopedName || optionsDefaults.generateScopedName, {
context: options.context || process.cwd()
});
}
const filetypeOptions = getFiletypeOptions(cssSourceFilePath, options.filetypes);
const fetch = (to: string, from: string) => {
const fromDirectoryPath = dirname(from);
const toPath = resolve(fromDirectoryPath, to);
return getTokens(runner, toPath, filetypeOptions);
};
const extraPlugins = getExtraPlugins(filetypeOptions);
const plugins = [
...extraPlugins,
Values,
LocalByDefault,
ExtractImports,
new Scope({
generateScopedName
}),
new Parser({
fetch
})
];
runner = postcss(plugins);
return getTokens(runner, cssSourceFilePath, filetypeOptions);
};