UNPKG

rollup-plugin-stylus

Version:

[DEPRECATED]

138 lines (104 loc) 4.72 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var _regeneratorRuntime = _interopDefault(require('babel-runtime/regenerator')); var _JSON$stringify = _interopDefault(require('babel-runtime/core-js/json/stringify')); var _toConsumableArray = _interopDefault(require('babel-runtime/helpers/toConsumableArray')); var _asyncToGenerator = _interopDefault(require('babel-runtime/helpers/asyncToGenerator')); var _Set = _interopDefault(require('babel-runtime/core-js/set')); var fs = _interopDefault(require('fs')); var path = _interopDefault(require('path')); var CssModules = _interopDefault(require('css-modules-loader-core')); var compileStylus = _interopDefault(require('stylus')); var rollupPluginutils = require('rollup-pluginutils'); /** * rollup-plugin-stylus * @param {Object} options * @param {Array || String} include, exclude - A minimatch pattern, or an array of minimatch patterns of including ID, or excluding ID (optional) * @param {Boolean} sourceMap - If true is specified, source map to be embedded in the output CSS (default is true) * @param {Function} fn - A function invoked with the Stylus renderer (it will be passed to use() function of the Stylus) * @param {Function || String} output - Output destination (optional) * @param {Function} identName - names of css classes * @return {Object} rollup plugin with transform function */ function rollupPluginStylus() { var _this = this; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var cssModules = new CssModules(); var filter = rollupPluginutils.createFilter(options.include, options.exclude); var fn = options.fn; var sourceMap = options.sourceMap !== false; /* output */ var outputFile = typeof options.output === 'string'; var outputFunction = typeof options.output === 'function'; /* ident */ var identFunction = typeof options.identName === 'function'; var customObject = {}; var allCss = new _Set(); return { transform: function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(code, id) { var localIdentName, style, css, _ref2, injectableSource, exportTokens, currentCss; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(!filter(id) || path.extname(id) !== '.styl')) { _context.next = 2; break; } return _context.abrupt('return', null); case 2: localIdentName = !sourceMap && identFunction ? options.identName(process.cwd(), id, customObject) : path.relative(process.cwd(), id); /* compile stylus syntax to css */ style = compileStylus(code); style.set('filename', localIdentName); if (sourceMap) style.set('sourcemap', { inline: true }); if (fn) style.use(fn); _context.next = 9; return style.render(); case 9: css = _context.sent; _context.next = 12; return cssModules.load(css, localIdentName, null); case 12: _ref2 = _context.sent; injectableSource = _ref2.injectableSource; exportTokens = _ref2.exportTokens; allCss.add(injectableSource); currentCss = [].concat(_toConsumableArray(allCss)).join(''); if (!outputFile) { _context.next = 22; break; } _context.next = 20; return fs.writeFile(options.output, currentCss); case 20: _context.next = 25; break; case 22: if (!outputFunction) { _context.next = 25; break; } _context.next = 25; return options.output(currentCss); case 25: return _context.abrupt('return', { id: id + '.css', code: 'export default ' + _JSON$stringify(exportTokens), map: { mappings: '' } }); case 26: case 'end': return _context.stop(); } } }, _callee, _this); })); return function transform(_x2, _x3) { return _ref.apply(this, arguments); }; }() }; } module.exports = rollupPluginStylus;