Skip to content

Commit

Permalink
feat(sass-loader): prepend options.data to code payload (#233)
Browse files Browse the repository at this point in the history
Adds `data` prepend feature common on
sass-loader implementations:
https://github.com/differui/rollup-plugin-sass#options-1
  • Loading branch information
himself65 authored Mar 5, 2020
1 parent 271c120 commit 80bb9da
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/sass-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export default {
return new Promise((resolve, reject) => {
const sass = loadSassOrThrow()
const render = pify(sass.render.bind(sass))
const data = this.options.data || ''
return workQueue.add(() =>
render({
...this.options,
file: this.id,
data: code,
data: data + code,
indentedSyntax: /\.sass$/.test(this.id),
sourceMap: this.sourceMap,
importer: [
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 @@ -700,6 +700,41 @@ console.log(undefined, undefined);
"
`;

exports[`sass data-prepend: js code 1`] = `
"'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
1 change: 1 addition & 0 deletions test/fixtures/sass-data-prepend/_prepend.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$special-color: pink;
1 change: 1 addition & 0 deletions test/fixtures/sass-data-prepend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.scss';
3 changes: 3 additions & 0 deletions test/fixtures/sass-data-prepend/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.special {
color: $special-color;
}
12 changes: 12 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ snapshotMany('sass', [
modules: true
}
},
{
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 80bb9da

Please sign in to comment.