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