Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Schad committed Aug 22, 2019
0 parents commit 1548ff5
Show file tree
Hide file tree
Showing 12 changed files with 574 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
coverage/
.nyc_output/
yarn.lock
package-lock.json
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- 'node'
- '10'
- '8'
os:
- windows
- linux
- osx
after_success:
- 'npm run coveralls'
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2017-08-22

### Added

- Everything
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Dennis Schad

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# rosid-handler-less

[![Travis Build Status](https://travis-ci.org/freedeebee/rosid-handler-less.svg?branch=master)](https://travis-ci.org/freedeebee/rosid-handler-less) [![Coverage Status](https://coveralls.io/repos/github/freedeebee/rosid-handler-less/badge.svg?branch=master)](https://coveralls.io/github/freedeebee/rosid-handler-less?branch=master) [![Dependencies](https://david-dm.org/freedeebee/rosid-handler-less.svg)](https://david-dm.org/freedeebee/rosid-handler-less#info=dependencies) [![Greenkeeper badge](https://badges.greenkeeper.io/freedeebee/rosid-handler-less.svg)](https://greenkeeper.io/)

A function that loads a LESS file, transforms it to CSS, adds vendor prefixes and minifies the output.

## Install

```
npm install rosid-handler-less
```

## Usage

### API

```js
const handler = require('rosid-handler-less')

handler('main.less').then((data) => {})
handler('main.css', { optimize: true }).then((data) => {})
```

### Rosid

Add the following object to your `rosidfile.json`, `rosidfile.js` or [routes array](https://github.com/electerious/Rosid/blob/master/docs/Routes.md). `rosid-handler-less` will transform all matching LESS files in your source folder to CSS.

```json
{
"name" : "LESS",
"path" : "[^_]*.{css,less}*",
"handler" : "rosid-handler-less"
}
```

```less
/* main.less */
.class {
color: white;
}
```

```css
/* main.css (output) */
.class { color: white; }
```

## Parameters

- `filePath` `{String}` Absolute path to file.
- `opts` `{?Object}` Options.
- `optimize` `{?Boolean}` - Optimize output. Defaults to `false`.

## Returns

- `{Promise<String|Buffer>}` The transformed file content.
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "rosid-handler-less",
"version": "1.0.0",
"authors": [
"Dennis Schad <dennis.schad1991@gmail.com>"
],
"description": "Load LESS and transform to CSS, add vendor prefixes and minify",
"main": "src/index.js",
"keywords": [
"rosid",
"handler",
"transform",
"compile",
"less",
"postcss",
"autoprefixer",
"cssnano"
],
"license": "MIT",
"homepage": "https://github.com/freedeebee/rosid-handler-less",
"repository": {
"type": "git",
"url": "https://github.com/freedeebee/rosid-handler-less.git"
},
"files": [
"src"
],
"scripts": {
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"test": "nyc node_modules/mocha/bin/_mocha"
},
"dependencies": {
"autoprefixer": "^9.4.8",
"cssnano": "^4.1.10",
"less": "^3.10.2",
"postcss": "^7.0.14"
},
"devDependencies": {
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"fsify": "^3.0.0",
"mocha": "^6.0.0",
"nyc": "^14.0.0"
},
"mocha": {
"timeout": "100000"
}
}
69 changes: 69 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'

const util = require('util')
const path = require('path')
const fs = require('fs')
const less = require('./less')
const postcss = require('./postcss')

/**
* Load LESS and transform to CSS, add vendor prefixes and minify.
* @public
* @param {String} filePath - Absolute path to file.
* @param {?Object} opts - Options.
* @returns {Promise<String>} CSS.
*/
module.exports = async function(filePath, opts = {}) {

if (typeof filePath !== 'string') throw new Error(`'filePath' must be a string`)
if (typeof opts !== 'object') throw new Error(`'opts' must be undefined or an object`)

const folderPath = path.dirname(filePath)

opts = Object.assign({
optimize: false
}, opts)

let output = null

output = await util.promisify(fs.readFile)(filePath, 'utf8')
output = await less(folderPath, output, opts)
output = await postcss(filePath, output, opts)

return output

}

/**
* Tell Rosid with which file extension it should load the file.
* @public
* @param {?Object} opts - Options.
* @returns {String} File extension.
*/
module.exports.in = function(opts) {

return (opts != null && opts.in != null) ? opts.in : '.less'

}

/**
* Tell Rosid with which file extension it should save the file.
* @public
* @param {?Object} opts - Options.
* @returns {String} File extension.
*/
module.exports.out = function(opts) {

return (opts != null && opts.out != null) ? opts.out : '.css'

}

/**
* Attach an array to the function, which contains a list of
* file patterns used by the handler. The array will be used by Rosid for caching purposes.
* @public
*/
module.exports.cache = [
'**/*.less',
'**/*.css'
]
31 changes: 31 additions & 0 deletions src/less.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const util = require('util')
const less = require('less')

/**
* Transform LESS to CSS.
* @public
* @param {String} folderPath - Path to the folder containing the LESS file.
* @param {String} str - LESS.
* @param {Object} opts - Optional options for the task.
* @returns {Promise<String>} CSS.
*/
module.exports = async function(folderPath, str, opts) {

// LESS can't handle empty files
if (str === '') return str

// Dismiss sourceMap when output should be optimized
const sourceMap = opts.optimize !== true

const result = await less.render(str, {
paths: [folderPath],
sourceMap: {
sourceMapFileInline: sourceMap,
}
})

return result.css

}
35 changes: 35 additions & 0 deletions src/postcss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const postcss = require('postcss')
const autoprefixer = require('autoprefixer')
const cssnano = require('cssnano')

/**
* Add vendor prefixes and minify CSS.
* @public
* @param {String} filePath - Absolute path to file.
* @param {String} str - CSS.
* @param {Object} opts - Optional options for the task.
* @returns {Promise<String>} Vendor prefixed and minified CSS.
*/
module.exports = async function(filePath, str, opts) {

// Dismiss sourceMap when output should be optimized
const sourceMap = opts.optimize !== true

const result = await postcss([

autoprefixer({ remove: false }),
cssnano({ safe: true })

]).process(str, {

from: filePath,
to: filePath,
map: sourceMap

})

return result.css

}
Loading

0 comments on commit 1548ff5

Please sign in to comment.