Skip to content

Commit

Permalink
Merge pull request #52 from differui/feature/processor
Browse files Browse the repository at this point in the history
Feature/processor
  • Loading branch information
differui authored May 24, 2018
2 parents a42f7fa + fbc6420 commit 6df44f8
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 137 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ sass({
output: 'bundle.css',

// Callback that will be called ongenerate with two arguments:
// - styles: the concatentated styles in order of imported
// - styles: the concatenated styles in order of imported
// - styleNodes: an array of style objects:
// [
// { id: './style1.scss', content: 'body { color: red };' },
Expand All @@ -84,7 +84,7 @@ sass({

### `processor`

+ Type: `Function` _(default: null)_
+ Type: `Function`

If you specify a function as processor which will be called with compiled css before generate phase.

Expand All @@ -102,13 +102,33 @@ sass({
})
```

The `processor` also support object result. Reverse `css` filed for stylesheet, the rest properties can be customized.

```js
sass({
processor(code) {
return {
css: '.body {}',
foo: 'foo',
bar: 'bar',
};
},
})
```

Otherwise, you could do:

```js
import style, { foo, bar } from 'stylesheet’;
```
### `options`
+ Type: `Object` _(default: null)_
+ Type: `Object`
Options for [node-sass](https://github.com/sass/node-sass#options).
If you specfiy `data`, the plugin will treat as prepend sass string.
If you specify `data`, the plugin will treat as prepend sass string.
Since you can inject variables during sass compilation with node.
```js
Expand Down
92 changes: 43 additions & 49 deletions dist/rollup-plugin-sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var _regeneratorRuntime = _interopDefault(require('babel-runtime/regenerator'));
var _Object$keys = _interopDefault(require('babel-runtime/core-js/object/keys'));
var _toConsumableArray = _interopDefault(require('babel-runtime/helpers/toConsumableArray'));
var _JSON$stringify = _interopDefault(require('babel-runtime/core-js/json/stringify'));
var _Object$keys = _interopDefault(require('babel-runtime/core-js/object/keys'));
var _typeof = _interopDefault(require('babel-runtime/helpers/typeof'));
var _Object$assign = _interopDefault(require('babel-runtime/core-js/object/assign'));
var _asyncToGenerator = _interopDefault(require('babel-runtime/helpers/asyncToGenerator'));
var path = require('path');
Expand Down Expand Up @@ -33,7 +35,6 @@ function insertStyle(css) {
style.setAttribute('type', 'text/css');
style.innerHTML = css;
document.head.appendChild(style);

return css;
}

Expand All @@ -48,8 +49,6 @@ function plugin() {

options.output = options.output || false;
options.insert = options.insert || false;
options.processor = options.processor || null;
options.options = options.options || null;

if (options.options && options.options.data) {
prependSass = options.options.data;
Expand All @@ -66,7 +65,7 @@ function plugin() {
},
transform: function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(code, id) {
var paths, baseConfig, sassConfig, css, _code, rest, restCode;
var paths, baseSassOptions, sassOptions, css, _code, restExports, processResult;

return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
Expand All @@ -77,56 +76,64 @@ function plugin() {
break;
}

return _context.abrupt('return', null);
return _context.abrupt('return');

case 2:
paths = [path.dirname(id), process.cwd()];
baseConfig = prependSass ? { data: '' + prependSass + code } : { file: id };
sassConfig = _Object$assign(baseConfig, options.options);
baseSassOptions = prependSass ? { data: '' + prependSass + code } : { file: id };
sassOptions = _Object$assign(baseSassOptions, options.options);


sassConfig.includePaths = sassConfig.includePaths ? sassConfig.includePaths.concat(paths) : paths;
sassOptions.includePaths = sassOptions.includePaths ? sassOptions.includePaths.concat(paths) : paths;

_context.prev = 6;
css = nodeSass.renderSync(sassConfig).css.toString();
css = nodeSass.renderSync(sassOptions).css.toString().trim();
_code = '';
rest = void 0;
restExports = void 0;

if (!css.trim()) {
_context.next = 24;
if (!css) {
_context.next = 26;
break;
}

if (!util.isFunction(options.processor)) {
_context.next = 21;
_context.next = 24;
break;
}

_context.next = 14;
return options.processor(css, id);

case 14:
css = _context.sent;
processResult = _context.sent;

if (!(typeof css !== 'string')) {
_context.next = 21;
if (!((typeof processResult === 'undefined' ? 'undefined' : _typeof(processResult)) === 'object')) {
_context.next = 23;
break;
}

if (!(typeof css.css !== 'string')) {
if (!(typeof processResult.css !== 'string')) {
_context.next = 18;
break;
}

throw new Error('You need to return the styles using the `css` property');
throw new Error('You need to return the styles using the `css` property. See https://github.com/differui/rollup-plugin-sass#processor');

case 18:
css = processResult.css;
delete processResult.css;
restExports = _Object$keys(processResult).map(function (name) {
return 'export const ' + name + ' = ' + _JSON$stringify(processResult[name]) + ';';
});
_context.next = 24;
break;

rest = css;
delete rest.css;
css = rest.css;
case 23:
if (typeof processResult === 'string') {
css = processResult;
}

case 21:
case 24:
if (styleMaps[id]) {
styleMaps[id].content = css;
} else {
Expand All @@ -135,32 +142,17 @@ function plugin() {
content: css
});
}

css = _JSON$stringify(css);

if (options.insert === true) {
_code = insertFnName + '(' + css + ');';
_code = insertFnName + '(' + _JSON$stringify(css) + ');';
} else if (options.output === false) {
_code = css;
_code = _JSON$stringify(css);
} else {
_code = '"";';
}

case 24:

_code = 'export default ' + _code + ';';
if (rest && !options.insert) {
restCode = _Object$keys(rest).map(function (name) {
var value = _JSON$stringify(rest[name]);
return 'export const ' + name + ' = ' + value + ';';
}).join('\n');


_code = _code + '\n' + restCode;
}

case 26:
return _context.abrupt('return', {
code: _code,
code: ['export default ' + _code + ';'].concat(_toConsumableArray(restExports || [])).join('\n'),
map: { mappings: '' }
});

Expand All @@ -184,7 +176,7 @@ function plugin() {
return transform;
}(),
ongenerate: function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(opts) {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(generateOptions) {
var css, dest;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
Expand All @@ -201,10 +193,9 @@ function plugin() {
css = styles.map(function (style) {
return style.content;
}).join('');
dest = opts.file;

if (!util.isString(options.output)) {
_context2.next = 9;
_context2.next = 8;
break;
}

Expand All @@ -215,20 +206,23 @@ function plugin() {
});
return _context2.abrupt('return', fs.writeFileSync(options.output, css));

case 9:
case 8:
if (!util.isFunction(options.output)) {
_context2.next = 13;
_context2.next = 12;
break;
}

return _context2.abrupt('return', options.output(css, styles));

case 13:
if (!(!options.insert && dest)) {
case 12:
if (!(!options.insert && generateOptions.file)) {
_context2.next = 18;
break;
}

dest = generateOptions.file;


if (dest.endsWith('.js') || dest.endsWith('.ts')) {
dest = dest.slice(0, -3);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-sass",
"version": "0.6.1",
"version": "0.7.0",
"description": "Rollup .sass files.",
"main": "dist/rollup-plugin-sass.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default {
'babel-runtime/core-js/object/assign',
'babel-runtime/core-js/object/keys',
'babel-runtime/helpers/asyncToGenerator',
'babel-runtime/helpers/toConsumableArray',
'babel-runtime/helpers/typeof',
'path',
'fs',
'node-sass',
Expand Down
Loading

0 comments on commit 6df44f8

Please sign in to comment.