ant-design-vue/antd-tools/transformLess.js

31 lines
896 B
JavaScript
Raw Normal View History

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