forked from ibigbug/antd-build
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
.b { | ||
color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |