UNPKG

5.08 kBJavaScriptView Raw
1import _regeneratorRuntime from 'babel-runtime/regenerator';
2import _JSON$stringify from 'babel-runtime/core-js/json/stringify';
3import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
4import fs from 'fs-extra';
5import 'path';
6import less from 'less';
7import { createFilter } from 'rollup-pluginutils';
8
9/*
10 * create a style tag and append to head tag
11 * @params {String} css style
12 */
13
14function insertStyle(css) {
15 if (!css) return;
16
17 if (typeof window == 'undefined') return;
18 var style = document.createElement('style');
19 style.setAttribute('media', 'screen');
20
21 style.innerHTML = css;
22 document.head.appendChild(style);
23 return css;
24}
25
26var renderSync = function renderSync(code, option) {
27 return less.render(code, option).then(function (output) {
28 return output.css;
29 }, function (error) {
30 throw error;
31 });
32};
33
34var fileCount = 0;
35
36function plugin() {
37 var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
38
39 options.insert = options.insert || false;
40 var filter = createFilter(options.include || ['**/*.less', '**/*.css'], options.exclude || 'node_modules/**');
41
42 var injectFnName = '__$styleInject';
43 return {
44 name: 'less',
45 intro: function intro() {
46 return options.insert ? insertStyle.toString().replace(/insertStyle/, injectFnName) : '';
47 },
48 transform: function transform(code, id) {
49 var _this = this;
50
51 return _asyncToGenerator(_regeneratorRuntime.mark(function _callee() {
52 var css, exportCode;
53 return _regeneratorRuntime.wrap(function _callee$(_context) {
54 while (1) {
55 switch (_context.prev = _context.next) {
56 case 0:
57 if (filter(id)) {
58 _context.next = 2;
59 break;
60 }
61
62 return _context.abrupt('return', null);
63
64 case 2:
65 fileCount++;
66
67 _context.prev = 3;
68
69 options.option = options.option || {};
70 options.option['filename'] = id;
71 options.output = options.output || 'rollup.build.css';
72 if (options.plugins) {
73 options.option['plugins'] = options.plugins;
74 }
75
76 _context.next = 10;
77 return renderSync(code, options.option);
78
79 case 10:
80 css = _context.sent;
81
82 if (!(options.output && isFunc(options.output))) {
83 _context.next = 15;
84 break;
85 }
86
87 _context.next = 14;
88 return options.output(css, id);
89
90 case 14:
91 css = _context.sent;
92
93 case 15:
94
95 if (options.output && isString(options.output)) {
96 if (fileCount == 1) {
97 //clean the output file
98 fs.removeSync(options.output);
99 }
100 fs.appendFileSync(options.output, css);
101 }
102
103 exportCode = '';
104
105
106 if (options.insert != false) {
107 exportCode = 'export default ' + injectFnName + '(' + _JSON$stringify(css.toString()) + ');';
108 } else {
109 exportCode = 'export default ' + _JSON$stringify(css.toString()) + ';';
110 }
111 return _context.abrupt('return', {
112 code: exportCode,
113 map: { mappings: '' }
114 });
115
116 case 21:
117 _context.prev = 21;
118 _context.t0 = _context['catch'](3);
119 throw _context.t0;
120
121 case 24:
122 case 'end':
123 return _context.stop();
124 }
125 }
126 }, _callee, _this, [[3, 21]]);
127 }))();
128 }
129 };
130};
131
132function isString(str) {
133 if (typeof str == 'string') {
134 return true;
135 } else {
136 return false;
137 }
138}
139
140function isFunc(fn) {
141 if (typeof fn == 'function') {
142 return true;
143 } else {
144 return false;
145 }
146}
147
148export default plugin;
\No newline at end of file