scss-to-css/index.js

165 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-12-03 10:00:21 +08:00
/**
*
* @author yutent<yutent@doui.cc>
* @date 2018/11/01 09:37:55
*/
'use strict'
const vsc = require('vscode')
const path = require('path')
const cp = require('child_process')
2018-12-03 10:57:30 +08:00
const fs = require('iofs')
2018-12-03 10:00:21 +08:00
const scss = require('node-sass')
const postcss = require('postcss')
const autoprefixer = require('autoprefixer')
2018-12-03 10:57:30 +08:00
const prefixer = postcss().use(autoprefixer())
2018-12-03 10:00:21 +08:00
const std = vsc.window.createOutputChannel('scss-to-css')
std.out = function() {
std.appendLine(msg)
}
const log = function(...args) {
console.log.apply(console, args)
}
2018-12-03 10:57:30 +08:00
const render = function(style, file) {
return new Promise((resolve, reject) => {
scss.render({ outputStyle: style, file }, (err, { css }) => {
if (err) {
reject(err)
} else {
resolve(css)
}
})
})
}
let options = {
2018-12-03 10:00:21 +08:00
compileOnSave: true,
autoPrefixer: true,
output: 'compressed',
exclude: ''
}
2018-12-03 10:57:30 +08:00
// const exec = function(arr, cb) {
// let cmd = arr.join(' ')
// return new Promise((yes, no) => {
// cp.exec(cmd, (err, out) => {
// if (err) {
// no('err: ' + err)
// } else {
// yes(arr)
// }
// })
// })
// }
const compileCss = (style, entry, output) => {
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 }
}
2018-12-03 10:00:21 +08:00
})
}
const Compiler = {
compile(doc) {
let origin = doc.fileName || ''
let target = origin.replace(/\.scss$/, '.')
let task = []
// 说明不是scss文件
if (origin === target) {
return
}
2018-12-03 10:57:30 +08:00
task = options.output.map(style => {
2018-12-03 10:00:21 +08:00
let ext = 'css'
2018-12-03 10:57:30 +08:00
switch (style) {
2018-12-03 10:00:21 +08:00
case 'compressed':
ext = 'min.' + ext
break
default:
2018-12-03 10:57:30 +08:00
ext = style.slice(0, 1) + '.' + ext
2018-12-03 10:00:21 +08:00
}
2018-12-03 10:57:30 +08:00
return { style, output: target + ext }
2018-12-03 10:00:21 +08:00
})
// 编译单一类型, 则去掉文件名微调
if (task.length === 1) {
2018-12-03 10:57:30 +08:00
task[0].output = target + 'css'
2018-12-03 10:00:21 +08:00
}
task = task.map(item => {
2018-12-03 10:57:30 +08:00
return compileCss(item.style, origin, item.output)
2018-12-03 10:00:21 +08:00
})
Promise.all(task)
.then(list => {
2018-12-03 10:57:30 +08:00
list.forEach(it => {
fs.echo(it.css, it.output)
})
2018-12-03 10:00:21 +08:00
})
.catch(err => {
log(err)
})
},
/**
* 条件过滤
* 用于保存时编译的动作, 右键编译时, 不过滤这2项
*/
filter(doc) {
// 未开启保存时编译
2018-12-03 10:57:30 +08:00
if (!options.compileOnSave) {
2018-12-03 10:00:21 +08:00
return
}
let origin = doc.fileName || ''
// var.scss文件默认不编译
if (/\/var\.scss$/.test(origin)) {
return
}
// 过滤不编译的文件
2018-12-03 10:57:30 +08:00
if (options.exclude) {
let exp = new RegExp(options.exclude, 'i')
2018-12-03 10:00:21 +08:00
if (exp.test(origin)) {
return
}
}
this.compile(doc)
}
}
function activate(ctx) {
// log('hello, the extend scss-compiler is running....')
2018-12-03 10:57:30 +08:00
let conf = vsc.workspace.getConfiguration('Scss2css')
Object.assign(options, conf)
2018-12-03 10:00:21 +08:00
2018-12-03 10:57:30 +08:00
options.output = options.output.split('|').map(it => it.trim())
2018-12-03 10:00:21 +08:00
vsc.workspace.onDidSaveTextDocument(doc => {
Compiler.filter(doc)
})
// let cmd = vsc.commands.registerCommand('ScssCompiler.compile', function(r) {
// log('----------------------------====================-----------------')
// })
// ctx.subscriptions.push(cmd)
}
function deactivate() {}
exports.activate = activate
exports.deactivate = deactivate