forked from daief/daisyui-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup-styles-plugin.js
72 lines (61 loc) · 1.55 KB
/
rollup-styles-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const postcss = require('postcss');
// const postcssJs = require('postcss-js');
const path = require('path');
const less = require('less');
const createPlugin = () => {
const tailwindConfig = require(path.resolve(__dirname, 'tailwind.config.js'));
tailwindConfig.content = [];
const process = postcss([
require('tailwindcss')({
...tailwindConfig,
}),
require('postcss-nested')({
bubble: ['screen'],
}),
require('autoprefixer'),
require('cssnano')({
preset: [
'default',
{
mergeRules: false,
},
],
}),
]);
const uid = (() => {
let i = 0;
return () => ++i;
})();
/**
* @type {import('rollup').Plugin}
*/
const plugin = {
async transform(code, id) {
if (!/\.(le|c)ss$/.test(id)) return null;
let cssCode = code;
if (/\.less/i.test(id)) {
// https://lesscss.org/usage/#programmatic-usage
const lessResult = await less.render(code, {
sourceMap: { sourceMapFileInline: true },
});
cssCode = lessResult.css;
}
const postcssResult = await process.process(cssCode, {
map: false,
});
// css 2 js
// const jsObject = postcssJs.objectify(postcssResult.root);
// jsObject[':root'] = void 0;
// return `export default ${JSON.stringify(jsObject)}`;
return `
const css = ${JSON.stringify(postcssResult.css)};
export default {
css,
id: ${uid()},
};
`;
},
};
return plugin;
};
export default createPlugin;