-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
93 lines (74 loc) · 2.32 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
const { sync: loadPostcssConfig } = require('postcss-load-config')
module.exports = opts => {
// prepare configuration
const pcssConfigCtx = { cwd: __dirname, env: process.env.NODE_ENV }
const pcssConfigPath = __dirname
const pcssConfig = safelyLoadPostcssConfig(pcssConfigCtx, pcssConfigPath, opts)
// rescript configuration used when no PostCSS configuration is present
const pcssNoopConfig = config => config
// rescript configuration used when a PostCSS configuration is present
const pcssUsesConfig = config => {
forEachPcssLoader(config, loader => {
// support *.pcss files
if (loader.test) {
loader.test = updateCssRegExp(loader.test)
}
if (loader.exclude) {
loader.exclude = updateCssRegExp(loader.exclude)
}
// support PostCSS configurations
loader.use.forEach(use => {
const isUseConfigurable = typeof use === 'object'
if (isUseConfigurable) {
// support "map" configuration
const shouldUpdateMap = 'map' in pcssConfig.options
if (shouldUpdateMap) {
const sourceMap = Boolean(pcssConfig.options.map)
use.options.sourceMap = sourceMap
}
// support "plugins" configuration
const shouldUpdatePlugins = use.options.plugins && pcssConfig.plugins.length
if (shouldUpdatePlugins) {
use.options.plugins = pcssConfig.plugins
}
}
})
})
return config
}
return pcssConfig ? pcssUsesConfig : pcssNoopConfig
}
function safelyLoadPostcssConfig (ctx, cwd) {
try {
/**
* @params (ctx, path, options)
* @return {
* plugins,
* options: { cwd, env, ...additionalOptsFromConfigFile },
* file
* }
*/
return loadPostcssConfig(ctx, cwd)
} catch (error) {
return null
}
}
function forEachPcssLoader (config, cb) {
const filterLoader = array => array.filter(object => JSON.stringify(object).includes('postcss-loader'))
filterLoader(config.module.rules).forEach(rule => {
filterLoader(rule.oneOf).forEach(oneOf => {
cb(oneOf)
})
})
}
function updateCssRegExp (prevTestRegExp) {
const cssRegExp = /\.css/g
if (cssRegExp.test(prevTestRegExp)) {
// update \.css references to \.p?css
const prevTestString = String(prevTestRegExp).slice(1, -1)
const nextTestString = prevTestString.replace(/\.css/g, '.p?css')
const nextTestRegExp = new RegExp(nextTestString)
return nextTestRegExp
}
return prevTestRegExp
}