-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerateTypingsForScss.js
48 lines (42 loc) · 1.11 KB
/
generateTypingsForScss.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
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const sass = require('node-sass');
const DtsCreator = require('typed-css-modules');
console.log('Generating typings for styles');
const creator = new DtsCreator({
camelCase: 'dashes',
});
glob('{,!(node_modules)/**/}*.scss', (e, matches) => {
if (e) {
console.error('Error in glob()', e);
process.exit(1);
}
matches.forEach((match) => {
const content = sass
.renderSync({
file: match,
importer: (url, prev, done) => {
if (!url.startsWith('~')) {
return null;
}
let filePath;
try {
filePath = require.resolve(url.replace('~', '') + '.scss');
} catch (e) {
filePath = require.resolve(url.replace('~', ''));
}
return { file: filePath };
},
})
.css.toString();
creator
.create(match, content)
.then((content) => {
content.writeFile();
})
.catch((e) => {
console.error(`Error when generating typings for ${match}`, e);
});
});
});