forked from aafrontend/scss-to-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (174 loc) · 4.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
/**
*
* @author yutent<[email protected]>
* @date 2018/11/01 09:37:55
*/
'use strict'
const vsc = require('vscode')
const path = require('path')
const exec = require('child_process').exec
const fs = require('iofs')
const postcss = require('postcss')
const autoprefixer = require('autoprefixer')
let prefixer
const log = console.log
const std = vsc.window.createOutputChannel('scss-to-css')
std.out = function(msg) {
std.appendLine(msg)
}
function run(cmd) {
return new Promise((yes, no) => {
exec(cmd, (err, res) => {
if (err) {
std.out(err)
no(err)
} else {
yes(res)
}
})
})
}
const render = function(style, file) {
return run(`node-sass --output-style ${style} ${file}`)
}
let options = {
compileOnSave: true,
autoPrefixer: true,
output: 'compressed',
exclude: ''
}
const compileScss = (style, entry, output) => {
if (options.outdir) {
let tmp = output.replace(options.workspace, '.')
output = path.join(options.outdir, tmp)
}
return render(style, entry)
.then(css => {
if (options.autoPrefixer) {
return prefixer.process(css, { from: '', to: '' }).then(result => {
return { css: result.css, output }
})
} else {
return { css, output }
}
})
.catch(err => {
std.out(err)
})
}
const Compiler = {
compile(doc) {
let origin = doc.fileName || ''
let target = origin.replace(/\.scss$/, '')
let task = []
// 说明不是scss文件
if (origin === target) {
return
}
task = options.output.map(style => {
let ext = '.css'
switch (style) {
case 'compressed':
ext = '.min' + ext
break
default:
ext = style.slice(0, 1) + ext
}
return { style, output: target + ext }
})
// 编译单一类型, 则去掉文件名微调
if (task.length === 1) {
task[0].output = target + '.css'
}
task = task.map(item => {
return compileScss(item.style, origin, item.output)
})
Promise.all(task)
.then(list => {
list.forEach(it => {
fs.echo(it.css, it.output)
})
})
.catch(err => {
vsc.window.showInformationMessage(err)
})
},
/**
* 条件过滤
* 用于保存时编译的动作, 右键编译时, 不过滤这2项
*/
filter(doc) {
// 未开启保存时编译
if (!options.compileOnSave) {
return
}
let origin = doc.fileName || ''
// var.scss文件默认不编译
if (/\/var\.scss$/.test(origin)) {
return
}
// 过滤不编译的文件
if (options.exclude) {
if (options.exclude.test(origin)) {
return
}
}
this.compile(doc)
}
}
function __init__() {
let conf = vsc.workspace.getConfiguration('Scss2css')
let folders = vsc.workspace.workspaceFolders
let wsDir = ''
let configFile = ''
Object.assign(options, conf)
conf = null
options.output = options.output.split('|').map(it => it.trim())
if (folders && folders.length) {
wsDir = folders[0].uri.path
}
if (wsDir) {
configFile = path.join(wsDir, '.scssrc')
} else {
let editor = vsc.window.activeTextEditor
if (editor) {
wsDir = path.dirname(editor.document.fileName)
configFile = path.join(wsDir, '.scssrc')
}
}
options.workspace = wsDir
// 有配置文件时, 优先使用配置文件的配置
if (fs.exists(configFile)) {
let tmp = JSON.parse(fs.cat(configFile).toString())
Object.assign(options, tmp)
tmp = null
if (options.outdir) {
options.outdir = path.join(options.workspace, options.outdir)
}
}
if (options.exclude) {
options.exclude = new RegExp(options.exclude, 'i')
}
prefixer = postcss().use(
autoprefixer({
browsers: options.browsers
})
)
}
function deactivate() {}
exports.activate = function(ctx) {
__init__()
vsc.workspace.onDidChangeConfiguration(__init__)
vsc.workspace.onDidSaveTextDocument(doc => {
std.clear()
Compiler.filter(doc)
})
let cmd = vsc.commands.registerCommand('Scss2css.compile', _ => {
let editor = vsc.window.activeTextEditor
if (editor) {
Compiler.compile(editor.document)
}
})
ctx.subscriptions.push(cmd)
}
exports.deactivate = deactivate