-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
78 lines (57 loc) · 1.97 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
// Webpack loader that consumes JS files created by css-loader and creates
// .cljc files out of them. That allows us to use CSS Modules from comfort of
// Clojure/ClojureScript
// It doesn't change its' sources anyhow, so you may use it after css-loader
// and still consume css-loader artifacts in next loaders
const fs = require('fs');
const { Parser } = require('acorn');
const { getOptions } = require('loader-utils');
const last = x => x[x.length - 1];
const snakeCaseToKebabCase = str => str.replace(/_/g, '-');
const kebabCaseToSnakeCase = str => str.replace(/-/g, '_');
const astToDefs = ast => {
let defs = '';
ast.body.forEach(node => {
if (node.type === 'ExpressionStatement' && node.expression.left) {
const { right, left } = node.expression;
const { object, property } = left;
if (
object &&
object.name === 'exports' &&
property &&
property.name === 'locals'
) {
defs = right.properties
.map(property => {
const key = property.key.value;
const value = property.value.value;
const cljsDef = `(def ${key} "${value}")`;
return cljsDef;
})
.join('\n');
}
}
});
return defs;
};
function transform(source) {
const MyParser = Parser.extend();
const ast = MyParser.parse(source);
const name = last(this.resourcePath.split('/'));
const options = getOptions(this);
const defs = astToDefs(ast);
const [file, ext] = name.split('.');
const kebabFileName = snakeCaseToKebabCase(file);
const nsPrefix = options.nsPrefix || 'styles';
const fileContent = `(ns ${nsPrefix}.${kebabFileName})\n`.concat(defs);
const path = options.path || './styles';
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
const snakeFileName = kebabCaseToSnakeCase(file);
fs.writeFile(`${path}/${snakeFileName}.cljc`, fileContent, function(err) {
if (err) throw err;
});
return source;
}
module.exports = transform;