Skip to content

Commit

Permalink
feat: allow to pass options to built-in loaders
Browse files Browse the repository at this point in the history
Using `options.use`. Close #130

Co-authored-by: BenjaminVanRyseghem <[email protected]>
  • Loading branch information
himself65 and BenjaminVanRyseghem authored Mar 7, 2020
1 parent 77074b6 commit 94d23fa
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ to properly resolve path, rebase and copy assets.

### use

Type: `name[]` `[name, options][]`<br>
Type: `name[]` `[name, options][]` `{ sass: options, stylus: options, less: options }`

Default: `['sass', 'stylus', 'less']`

Use a loader, currently built-in loaders are:
Expand All @@ -264,6 +265,9 @@ Use a loader, currently built-in loaders are:

They are executed from right to left.

If you pass the `object`, then its property `sass`, `stylus` and `less` will
be pass in the corresponding loader.

### loaders

Type: `Loader[]`
Expand Down
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ export default (options = {}) => {
exec: options.exec
}
}
const use = options.use || ['sass', 'stylus', 'less']
let use = ['sass', 'stylus', 'less']
if (Array.isArray(options.use)) {
use = options.use
} else if (options.use !== null && typeof options.use === 'object') {
use = [
['sass', options.use.sass || {}],
['stylus', options.use.stylus || {}],
['less', options.use.less || {}]
]
}
use.unshift(['postcss', postcssLoaderOptions])

const loaders = new Loaders({
Expand Down
35 changes: 35 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,41 @@ styleInject(css);
"
`;

exports[`sass data-prepend: js code 2`] = `
"'use strict';
function styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;
if (!css || typeof document === 'undefined') { return; }
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
var css = \\".special {\\\\n color: pink; }\\\\n\\";
styleInject(css);
"
`;

exports[`sass default: js code 1`] = `
"'use strict';
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@ snapshotMany('sass', [
]
}
},
{
title: 'data-prepend',
input: 'sass-data-prepend/index.js',
options: {
use: {
sass: { data: '@import \'prepend\';' }
}
}
},
{
title: 'import',
input: 'sass-import/index.js'
Expand Down

0 comments on commit 94d23fa

Please sign in to comment.