-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
219 lines (176 loc) · 5.24 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const fs = require('fs');
const path = require('path');
const watchFile = require('node-watch');
const meow = require('meow');
const fileExtension = require('file-extension');
const postcss = require('postcss');
const sass = require('node-sass');
const less = require('less');
const cssParser = require('css');
require('log-timestamp');
const cli = meow(
`
Usage
$ cessie <input> -o filename.css
Options
--out-file, -o Name of the out file
--minify, -m Minify css. Defaults to true.
--watch, -w Watch for file changes. Defaults to false.
--source-map, -s Generate source map. Defaults to true.
--import-from, -i Import CSS variables from file (https://github.com/postcss/postcss-custom-properties#importfrom)
--export-to, -e Export CSS variables to file (https://github.com/postcss/postcss-custom-properties#exportto)
Examples
$ cessie bundle.css -o ie11.css
`,
{
flags: {
'out-file': {
type: 'string',
alias: 'o',
default: 'ie11.css',
},
minify: {
type: 'boolean',
alias: 'm',
default: true,
},
watch: {
type: 'boolean',
alias: 'w',
default: false,
},
'source-map': {
type: 'boolean',
alias: 's',
default: true,
},
'import-from': {
type: 'string',
alias: 'i',
default: '',
},
'export-to': {
type: 'string',
alias: 'e',
default: '',
},
},
}
);
const [inputFile] = cli.input;
const { outFile, minify, watch, sourceMap, importFrom, exportTo } = cli.flags;
const transpileCss = (content) => {
try {
cssParser.parse(content, { source: inputFile });
return { css: content, map: '' };
} catch (e) {
console.error(e);
}
};
const transpileSass = (content) => {
try {
const { css, map } = sass.renderSync({
data: content,
sourceMap: true,
outFile,
});
return { css, map };
} catch (e) {
console.error(e);
}
};
const transpileLess = async (content) => {
try {
const { css, map } = await less.render(content, {
sourceMap: {
outputFilename: outFile + '.map',
},
});
return { css, map };
} catch (e) {
console.error(e);
}
};
async function transpile(content) {
const extension = fileExtension(inputFile);
switch (extension) {
case 'css':
return transpileCss(content);
case 'scss':
return await transpileSass(content);
case 'sass':
return await transpileSass(content);
case 'less':
return await transpileLess(content);
}
}
async function getFileContent() {
const chunks = [];
return new Promise((resolve, reject) =>
fs
.createReadStream(inputFile)
.on('data', (chunk) => chunks.push(chunk))
.on('error', (err) => reject(err))
.on('close', () => resolve(chunks.join('')))
);
}
async function writeFileContent(outFile, content) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(outFile);
file.write(content);
file.end(() => resolve());
});
}
function addSourceMapURL(css) {
if (sourceMap) {
css += ` /* # sourceMappingURL=${path.basename(outFile)}.map */`;
}
return css;
}
async function generateCSS() {
const transpiled = await transpile(await getFileContent());
const plugins = [];
plugins.push(
...[
require('postcss-custom-properties')({ preserve: false, exportTo, importFrom }),
require('postcss-calc'),
require('autoprefixer'),
require('postcss-preset-env'),
]
);
if (minify && !watch) {
plugins.push(require('postcss-csso'));
}
const { css } = await postcss(plugins).process(transpiled.css, { from: false });
return {
css: addSourceMapURL(css),
map: transpiled.map,
};
}
async function main() {
if (!inputFile) {
console.log(' [x] No input file');
return;
}
console.log(` [*] Transpiling ${inputFile} to ${outFile}`);
if (watch) {
const folder = path.resolve(path.dirname(inputFile));
const opts = {
filter: new RegExp(`\\${path.extname(inputFile)}$`),
recursive: true,
};
const watcher = watchFile(folder, opts, async (event, name) => {
console.log(` [*] Watching and writing ${inputFile} to ${outFile}`);
const { css } = await generateCSS();
await writeFileContent(outFile, css);
});
watcher.on('error', (err) => console.error(' [x] %s', err));
}
const { css, map } = await generateCSS();
writeFileContent(outFile, css);
if (sourceMap) {
writeFileContent(outFile + '.map', map);
}
console.log(` [*] Finished writing file: ${outFile}`);
}
main();