From d945af6d3ecef80d61c1fd30063b12866c358b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Thu, 14 Feb 2019 13:38:11 +0800 Subject: [PATCH] 2.0.0 --- CHANGELOG.md | 7 +++++++ README.md | 15 +++++++++++++- README_ZH.md | 15 +++++++++++++- index.js | 55 ++++++++++++++++++++++++++++++++------------------ package.json | 2 +- test/.scssrc | 4 ++++ test/demo.css | 2 +- test/demo.scss | 3 ++- 8 files changed, 78 insertions(+), 25 deletions(-) create mode 100644 test/.scssrc diff --git a/CHANGELOG.md b/CHANGELOG.md index ccde29c..a904412 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,15 @@ # 更新日志(Changed Logs) + +## [2.0.0] 2019-02-14 +- 大版本改动,移除 `.browserslistrc`的支持, 改用`.scssrc`; +- 新增`outdir`选项支持, 允许将编译后的css文件统一存放. + + ## [1.2.0] 2019-01-05 - 配置优化 + ## [1.1.1] 2018-12-27 - 更换sass库,兼容win10 [#1](https://github.com/yutent/scss-to-css/issues/1) diff --git a/README.md b/README.md index 3e491f5..1a3a91e 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Live demo: > Search `scss-to-css` and install in the marketplace. -## .browserslistrc DEMO +## .browserslistrc DEMO (Deprecated in 2.x) > Just for demo, you can change it by youself. If not exists, the default value will be `last 2 version`. ``` @@ -43,3 +43,16 @@ Android >= 4.4 ff > 38 Chrome > 38 ``` + +## .scssrc DEMO (in 2.x or above) +> instead of using `.browserslistrc`, we recommend to use `.scssrc`. + +```json +{ + "browsers": ["ie > 9", "iOS > 8", "Android >= 4.4", "ff > 38", "Chrome > 38"], + "outdir": "dist" // relative path of this '.scssrc' file. +} + +// You can also set other config in this file. +// eg. compileOnSave,autoPrefixer,output,exclude +``` \ No newline at end of file diff --git a/README_ZH.md b/README_ZH.md index 3bac454..5eb8f0a 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -31,7 +31,7 @@ > 直接在商店搜索安装即可。 -## .browserslistrc 示例 +## .browserslistrc 示例(2.x版之后已弃用) > 这只是个示例, 可自行根据项目需求, 修改配置。 没有配置则默认为 `last 2 version`。 ``` @@ -41,3 +41,16 @@ Android >= 4.4 ff > 38 Chrome > 38 ``` + +## .scssrc DEMO (2.x版及以上) +> 与其使用 `.browserslistrc`, 我们更推荐使用`.scssrc`文件来配置编译选项。且写在该文件上的配置, 优先级要高于全局的配置项。 + +```json +{ + "browsers": ["ie > 9", "iOS > 8", "Android >= 4.4", "ff > 38", "Chrome > 38"], + "outdir": "dist" //相对.scssrc文件的目录 +} + +// 你也可以在此配置里设置其他选项, 将会覆盖全局的配置. +// 如: compileOnSave,autoPrefixer,output,exclude +``` \ No newline at end of file diff --git a/index.js b/index.js index ad18543..ffba278 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,10 @@ let options = { } const compileCss = (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 => { @@ -51,7 +55,7 @@ const compileCss = (style, entry, output) => { const Compiler = { compile(doc) { let origin = doc.fileName || '' - let target = origin.replace(/\.scss$/, '.') + let target = origin.replace(/\.scss$/, '') let task = [] // 说明不是scss文件 @@ -60,14 +64,14 @@ const Compiler = { } task = options.output.map(style => { - let ext = 'css' + let ext = '.css' switch (style) { case 'compressed': - ext = 'min.' + ext + ext = '.min' + ext break default: - ext = style.slice(0, 1) + '.' + ext + ext = style.slice(0, 1) + ext } return { style, output: target + ext } @@ -75,7 +79,7 @@ const Compiler = { // 编译单一类型, 则去掉文件名微调 if (task.length === 1) { - task[0].output = target + 'css' + task[0].output = target + '.css' } task = task.map(item => { @@ -124,40 +128,51 @@ const Compiler = { 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()) -} -function activate(ctx) { - let folders = vsc.workspace.workspaceFolders - let wsf = '' - let browsersrc = '' if (folders && folders.length) { - wsf = folders[0].uri.path + wsDir = folders[0].uri.path } - if (wsf) { - browsersrc = path.join(wsf, '.browserslistrc') + + if (wsDir) { + configFile = path.join(wsDir, '.scssrc') } else { let editor = vsc.window.activeTextEditor if (editor) { - wsf = path.dirname(editor.document.fileName) - browsersrc = path.join(wsf, '.browserslistrc') + wsDir = path.dirname(editor.document.fileName) + configFile = path.join(wsDir, '.scssrc') } } - if (fs.exists(browsersrc)) { - options.browsers = fs - .cat(browsersrc) - .toString() - .split(/[\n\r]/) + // 以配置文件所在目录为根目录(workspace) + if (fs.exists(configFile)) { + options.workspace = path.dirname(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) + } } + prefixer = postcss().use( autoprefixer({ browsers: options.browsers }) ) +} +function activate(ctx) { __init__() vsc.workspace.onDidChangeConfiguration(__init__) diff --git a/package.json b/package.json index fbe8305..50f2542 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "scss-to-css", "displayName": "scss-to-css", "description": "🔥 The easiest way to compile scss file to css. And autoprefixer at the same time.", - "version": "1.2.0", + "version": "2.0.0", "publisher": "yutent", "author": "Yutent [@yutent]", "icon": "logo.png", diff --git a/test/.scssrc b/test/.scssrc new file mode 100644 index 0000000..2a4233d --- /dev/null +++ b/test/.scssrc @@ -0,0 +1,4 @@ +{ + "browsers": ["ie > 9", "iOS > 8", "Android >= 4.4", "ff > 38", "Chrome > 38"], + "outdir": "dist" +} diff --git a/test/demo.css b/test/demo.css index 31109c5..330218f 100644 --- a/test/demo.css +++ b/test/demo.css @@ -1 +1 @@ -ul{display:flex}ul li{flex:2;color:#fff} +ul{display:-webkit-flex;display:-ms-flexbox;display:flex}ul li{-webkit-flex:2;-ms-flex:2;flex:2;color:#fff} diff --git a/test/demo.scss b/test/demo.scss index 7231d5e..c9b3fd9 100644 --- a/test/demo.scss +++ b/test/demo.scss @@ -2,4 +2,5 @@ ul { display: flex; li {flex: 2;color: #fff;} -} \ No newline at end of file +} +