forked from a-x-/babel-plugin-transform-import-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostcss.js
84 lines (69 loc) · 3.2 KB
/
postcss.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
const genericNames = require('generic-names');
// const globToRegex = require('glob-to-regexp')
// const validate = require('./validate')
const path = require('path');
const fs = require('fs');
const postcss = require('postcss');
const LocalByDefault = require('postcss-icss-selectors'); // fixme: how to remove? here is postcss@6
const ExtractImports = require('postcss-modules-extract-imports');
const Scope = require('postcss-modules-scope');
const ResolveImports = require('postcss-modules-resolve-imports');
const getPostcssrcPlugins = require('postcss-load-config/src/plugins');
const getPostcssrcOptions = require('postcss-load-config/src/options');
const PWD = process.cwd();
module.exports = {
process (css, fromSrc, options, configPath) {
// TODO: load options, plugins from .postcssrc / postcss.config.js
const { plugins, ...configOptions } = loadConfig(configPath);
const runner = postcss(getPlugins(plugins, options));
// source maps don't needed here because we doesn't transform the css
const lazyResult = runner.process(css, { ...configOptions, ...options, map: false, from: fromSrc });
printWarnings(lazyResult);
const classesMap = lazyResult.root.exports || {};
return { code: lazyResult.css, classesMap };
},
};
function getPlugins(plugins, { generateScopedName, ext } = {}) {
const extensions = [ext || '.css'];
const resolveOpts = {}, prepend = [], append = plugins || [], mode = undefined, hashPrefix = undefined;
const scopedName = normalizeScopedName(generateScopedName, hashPrefix);
return [
...prepend,
// fixme: remove all of this?
new LocalByDefault({ mode, generateScopedName: scopedName }),
new ExtractImports({ createImportedName: undefined }),
new Scope({ generateScopedName: scopedName }),
new ResolveImports({ resolve: { extensions, ...resolveOpts } }),
...append,
];
}
function printWarnings(lazyResult) {
if (lazyResult.error) console.error(lazyResult.error);
// https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings
lazyResult.warnings().forEach(message => console.warn(message.text));
}
function normalizeScopedName(generateScopedName, hashPrefix) {
if (!generateScopedName) {
return (local, filename) => Scope.generateScopedName(local, path.relative(PWD, filename));
}
if (typeof generateScopedName === 'function') return generateScopedName;
// for example '[name]__[local]___[hash:base64:5]'
return genericNames(generateScopedName, { context: PWD, hashPrefix });
}
function loadConfig(configPath) {
const { conf, confPath } = loadRawConf(configPath) || {};
if (!conf) return {};
return { ...conf, plugins: getPostcssrcPlugins(conf, confPath), options: getPostcssrcOptions(conf, confPath) };
}
function loadRawConf(configPath) {
const jsConfPath = /\.js$/.test(configPath) ? configPath : path.resolve(PWD, 'postcss.config.js');
if (fs.existsSync(jsConfPath)) {
const conf = require(jsConfPath);
return { conf, confPath: jsConfPath };
}
const jsonConfPath = /rc$|\.json$/.test(configPath) ? configPath : path.resolve(PWD, '.postcssrc')
if (fs.existsSync(jsonConfPath)) {
const conf = JSON.parse(fs.readFileSync(jsonConfPath, 'utf8'));
return { conf, confPath: jsonConfPath };
}
}