scss-to-css/index.js

183 lines
3.7 KiB
JavaScript
Raw Permalink 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')
2018-12-03 10:57:30 +08:00
const fs = require('iofs')
2018-12-27 18:44:32 +08:00
const ScssLib = require('./lib/index.js')
2018-12-03 10:00:21 +08:00
const postcss = require('postcss')
const autoprefixer = require('autoprefixer')
2018-12-03 14:08:33 +08:00
let prefixer
2018-12-03 10:00:21 +08:00
const log = console.log
2018-12-03 10:00:21 +08:00
2018-12-03 10:57:30 +08:00
const render = function(style, file) {
return new Promise((resolve, reject) => {
2018-12-27 18:44:32 +08:00
ScssLib(file, { style: ScssLib.Sass.style[style] }, res => {
if (res && res.text) {
resolve(res.text)
2018-12-03 10:57:30 +08:00
} else {
2018-12-27 18:44:32 +08:00
reject(res)
2018-12-03 10:57:30 +08:00
}
})
})
}
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 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 => {
vsc.window.showInformationMessage(err)
2018-12-03 10:00:21 +08:00
})
},
/**
* 条件过滤
* 用于保存时编译的动作, 右键编译时, 不过滤这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)
}
}
2019-01-05 04:24:17 +08:00
function __init__() {
let conf = vsc.workspace.getConfiguration('Scss2css')
Object.assign(options, conf)
options.output = options.output.split('|').map(it => it.trim())
}
2018-12-03 10:00:21 +08:00
function activate(ctx) {
2018-12-03 14:08:33 +08:00
let folders = vsc.workspace.workspaceFolders
let wsf = ''
let browsersrc = ''
if (folders && folders.length) {
wsf = folders[0].uri.path
}
if (wsf) {
browsersrc = path.join(wsf, '.browserslistrc')
} else {
let editor = vsc.window.activeTextEditor
if (editor) {
wsf = path.dirname(editor.document.fileName)
browsersrc = path.join(wsf, '.browserslistrc')
}
2018-12-03 14:08:33 +08:00
}
if (fs.exists(browsersrc)) {
options.browsers = fs
.cat(browsersrc)
.toString()
.split(/[\n\r]/)
}
prefixer = postcss().use(
autoprefixer({
browsers: options.browsers
})
)
2019-01-05 04:24:17 +08:00
__init__()
vsc.workspace.onDidChangeConfiguration(__init__)
2018-12-03 10:00:21 +08:00
vsc.workspace.onDidSaveTextDocument(doc => {
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)
2018-12-03 10:00:21 +08:00
}
function deactivate() {}
exports.activate = activate
exports.deactivate = deactivate