-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (86 loc) · 2.11 KB
/
app.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
import cssNano from './cssnano';
import { saveAs } from 'file-saver';
new Vue({
el: '#app',
data: {
fileSelected: false,
isDragging: false,
inputHasFocus: false,
result: '',
selectedFiles: [],
worker: null,
},
mounted() {
if (window.Worker) {
this.worker = new Worker('worker.js');
this.worker.onmessage = e => {
this.result = e.data;
};
}
},
methods: {
openFileInput() {
this.$refs.fileInput.click();
},
readFiles() {
return Promise.all(this.selectedFiles.map(file => readFilePromise(file))).then(data => data.join(''));
},
drop(e) {
this.isDragging = false;
this.fileSelected = true;
this.selectedFiles = [
...this.selectedFiles,
...Array.from(e.dataTransfer.files)
];
if (this.worker) {
this.worker.postMessage(this.selectedFiles);
} else {
this.readFiles()
.then(cssString => cssNano(cssString))
.then(result => {
this.result = result.css
});
}
},
fileInputChange(e) {
this.isDragging = false;
this.fileSelected = true;
this.selectedFiles = [
...this.selectedFiles,
...Array.from(e.target.files)
];
if (this.worker) {
this.worker.postMessage(this.selectedFiles);
} else {
this.readFiles()
.then(cssString => cssNano(cssString))
.then(result => {
this.result = result.css
});
}
},
mergeCSS() {
const blobObj = new Blob([this.result], {type: "text/plain;charset=utf-8"});
saveAs(blobObj, 'merged.css');
// this.readFiles().then(cssNano).then(str => console.log(str.css));
},
},
filters: {
fileSizeFormat(val) {
const size = parseFloat(val) || 0;
if (!size) {
return '0B';
}
if (size < 1000) {
return `${size}B`;
}
if (size < 1000000) {
return `${Math.round(size / 1000)}KB`
}
if (size < 8000000) {
return `${Math.round(size / 1000000)}MB`
}
return 'Too large';
}
}
});