Skip to content

Commit

Permalink
Merge fd5c0ba into 2ab68da
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Apr 22, 2016
2 parents 2ab68da + fd5c0ba commit ded1764
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"less": "~2.6.0",
"less-loader": "~2.2.0",
"map-json-webpack-plugin": "~1.1.0",
"postcss": "^5.0.19",
"postcss-loader": "~0.8.0",
"rucksack-css": "~0.8.5",
"url-loader": "~0.5.6",
Expand Down
50 changes: 50 additions & 0 deletions src/transformLess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import less from 'less';
import { readFileSync } from 'fs';
import { join, dirname, resolve } from 'path';
import postcss from 'postcss';
import rucksack from 'rucksack-css';
import autoprefixer from 'autoprefixer';

function transformLess(lessFile, config = {}) {
const { cwd = process.cwd() } = config;
const resolvedLessFile = resolve(cwd, lessFile);

let data = readFileSync(resolvedLessFile, 'utf-8');
data = data.replace(/^\uFEFF/, '');

return new Promise((resolve, reject) => {

// Do less compile
const lessOpts = {
paths: [ dirname(resolvedLessFile) ],
filename: resolvedLessFile,
};
less.render(data, lessOpts)
.then(result => {

// Do postcss compile
const plugins = [
rucksack(),
autoprefixer({
browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8']
}),
];
const source = result.css;
const postcssOpts = {};

postcss(plugins).process(source, postcssOpts)
.then(result => {
resolve(result.css);
})
.catch(err => {
reject(err);
});
})
.catch(err => {
reject(err);
});

});
}

export default transformLess;
16 changes: 16 additions & 0 deletions test/fixtures/transformLess/a.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import "./b.less";

@green: '#ccc';

.a {
// less variable
color: @green;

// autoprefixer
transform: translateX(1);

// rucksack
.foo {
position: absolute 0;
}
}
4 changes: 4 additions & 0 deletions test/fixtures/transformLess/b.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.b {
color: red;
}
33 changes: 33 additions & 0 deletions test/transformLess-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { join } from 'path';
import { readFileSync } from 'fs';
import expect from 'expect';
import transformLess from '../src/transformLess';

const cwd = process.cwd();

describe('lib/transformLess', () => {
const expected = `.b {
color: red;
}
.a {
color: '#ccc';
-webkit-transform: translateX(1);
-ms-transform: translateX(1);
transform: translateX(1);
}
.a .foo {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
`;

it('should build normally', () => {
return transformLess('./test/fixtures/transformLess/a.less', { cwd })
.then(result => {
expect(result).toEqual(expected);
});
});
});

0 comments on commit ded1764

Please sign in to comment.