UNPKG

5.13 kBJavaScriptView Raw
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5var _regeneratorRuntime = _interopDefault(require('babel-runtime/regenerator'));
6var _JSON$stringify = _interopDefault(require('babel-runtime/core-js/json/stringify'));
7var _asyncToGenerator = _interopDefault(require('babel-runtime/helpers/asyncToGenerator'));
8var fs = _interopDefault(require('fs-extra'));
9var path = require('path');
10var less = _interopDefault(require('less'));
11var rollupPluginutils = require('rollup-pluginutils');
12
13/*
14 * create a style tag and append to head tag
15 * @params {String} css style
16 */
17
18function insertStyle(css) {
19 if (!css) return;
20
21 if (typeof window == 'undefined') return;
22 var style = document.createElement('style');
23 style.setAttribute('media', 'screen');
24
25 style.innerHTML = css;
26 document.head.appendChild(style);
27 return css;
28}
29
30var renderSync = function renderSync(code, option) {
31 return less.render(code, option).then(function (output) {
32 return output.css;
33 }, function (error) {
34 throw error;
35 });
36};
37
38var fileCount = 0;
39
40function plugin() {
41 var options = arguments.length <= 0 || arguments[0] === undefined ? { insert: false } : arguments[0];
42
43 var filter = rollupPluginutils.createFilter(options.include || ['**/*.less', '**/*.css'], options.exclude || 'node_modules/**');
44
45 var injectFnName = '__$styleInject';
46 return {
47 name: 'less',
48 intro: function intro() {
49 return insertStyle.toString().replace(/insertStyle/, injectFnName);
50 },
51 transform: function transform(code, id) {
52 var _this = this;
53
54 return _asyncToGenerator(_regeneratorRuntime.mark(function _callee() {
55 var css, exportCode;
56 return _regeneratorRuntime.wrap(function _callee$(_context) {
57 while (1) {
58 switch (_context.prev = _context.next) {
59 case 0:
60 if (filter(id)) {
61 _context.next = 2;
62 break;
63 }
64
65 return _context.abrupt('return', null);
66
67 case 2:
68 fileCount++;
69
70 _context.prev = 3;
71
72 options.option = options.option || {};
73 options.option['filename'] = id;
74 options.output = options.output || 'rollup.build.css';
75
76 _context.next = 9;
77 return renderSync(code, options.option);
78
79 case 9:
80 css = _context.sent;
81
82 if (!(options.output && isFunc(options.output))) {
83 _context.next = 14;
84 break;
85 }
86
87 _context.next = 13;
88 return options.output(css, id);
89
90 case 13:
91 css = _context.sent;
92
93 case 14:
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 20:
117 _context.prev = 20;
118 _context.t0 = _context['catch'](3);
119 throw _context.t0;
120
121 case 23:
122 case 'end':
123 return _context.stop();
124 }
125 }
126 }, _callee, _this, [[3, 20]]);
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
148module.exports = plugin;
\No newline at end of file