-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup.js
46 lines (36 loc) · 1.37 KB
/
setup.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
'use strict';
const {readFileSync} = require('fs');
const {basename, resolve} = require('path');
const postcss = require('postcss');
const cssmodulesLocalByDefault = require('postcss-modules-local-by-default');
const cssmodulesExtractImports = require('postcss-modules-extract-imports');
const cssmodulesScope = require('postcss-modules-scope');
const cssmodulesValues = require('postcss-icss-values');
const cssmodulesResolveImports = require('../index.js');
const LOADER = {
values: () => cssmodulesValues,
'local-by-default': () => cssmodulesLocalByDefault,
'extract-imports': () => cssmodulesExtractImports,
scope: () => new cssmodulesScope({generateScopedName}),
self: () => cssmodulesResolveImports,
};
module.exports = setup;
function setup(...plugins) {
const loadedPlugins = plugins.map(name =>
typeof name === 'string' ? LOADER[name]() : name);
return setupCase;
function setupCase(directory) {
const runner = postcss(loadedPlugins);
const sourcepath = resolve(directory, 'source.css');
const source = readFileSync(sourcepath, 'utf8');
const lazyResult = runner.process(source, {from: sourcepath});
return {
exports: lazyResult.root.exports,
resulting: lazyResult.css.replace(/\r/g, ''),
source,
};
}
}
function generateScopedName(local, filename) {
return `_${basename(filename).split('.').shift()}_${local}`;
}