2019-01-12 03:33:27 +00:00
|
|
|
const less = require('less');
|
|
|
|
const { readFileSync } = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const postcss = require('postcss');
|
|
|
|
const NpmImportPlugin = require('less-plugin-npm-import');
|
|
|
|
const postcssConfig = require('./postcssConfig');
|
2018-03-18 11:59:38 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function transformLess(lessFile, config = {}) {
|
|
|
|
const { cwd = process.cwd() } = config;
|
|
|
|
const resolvedLessFile = path.resolve(cwd, lessFile);
|
2018-03-18 11:59:38 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let data = readFileSync(resolvedLessFile, 'utf-8');
|
|
|
|
data = data.replace(/^\uFEFF/, '');
|
2018-03-18 11:59:38 +00:00
|
|
|
|
|
|
|
// Do less compile
|
|
|
|
const lessOpts = {
|
|
|
|
paths: [path.dirname(resolvedLessFile)],
|
|
|
|
filename: resolvedLessFile,
|
2019-01-12 03:33:27 +00:00
|
|
|
plugins: [new NpmImportPlugin({ prefix: '~' })],
|
2019-01-20 13:13:55 +00:00
|
|
|
javascriptEnabled: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
return less
|
|
|
|
.render(data, lessOpts)
|
|
|
|
.then(result => {
|
|
|
|
const source = result.css;
|
|
|
|
return postcss(postcssConfig.plugins).process(source);
|
2018-03-18 11:59:38 +00:00
|
|
|
})
|
2019-01-12 03:33:27 +00:00
|
|
|
.then(r => {
|
|
|
|
return r.css;
|
|
|
|
});
|
2018-03-18 11:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
module.exports = transformLess;
|