Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ Thankfully there are a two solutions to this problem:
By default all options passed to loader also passed to to [Node Sass](https://github.com/sass/node-sass) or [Dart Sass](http://sass-lang.com/dart-sass)

> ℹ️ The `indentedSyntax` option has `true` value for the `sass` extension.

> ℹ️ Options such as `file` and `outFile` are unavailable.

> ℹ️ Only the "expanded" and "compressed" values of outputStyle are supported for `dart-sass`.

> ℹ We recommend don't use `sourceMapContents`, `sourceMapEmbed`, `sourceMapRoot` options because loader automatically setup this options.

There is a slight difference between the `node-sass` and `sass` options. We recommend look documentation before used them:
Expand Down Expand Up @@ -236,7 +239,7 @@ module.exports = {
};
```

### `data`
### `prependData`

Type: `String|Function`
Default: `undefined`
Expand All @@ -262,7 +265,7 @@ module.exports = {
{
loader: 'sass-loader',
options: {
data: '$env: ' + process.env.NODE_ENV + ';',
prependData: '$env: ' + process.env.NODE_ENV + ';',
},
},
],
Expand All @@ -286,8 +289,8 @@ module.exports = {
{
loader: 'sass-loader',
options: {
data: (loaderContext) => {
// More information about avalaible options https://webpack.js.org/api/loaders/
prependData: (loaderContext) => {
// More information about available properties https://webpack.js.org/api/loaders/
const { resourcePath, rootContext } = loaderContext;
const relativePath = path.relative(rootContext, resourcePath);

Expand Down
8 changes: 4 additions & 4 deletions src/getSassOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ function getSassOptions(loaderContext, loaderOptions, content) {
options.functions = options.functions(loaderContext);
}

let { data } = options;
let { prependData } = options;

if (typeof options.data === 'function') {
data = options.data(loaderContext);
if (typeof prependData === 'function') {
prependData = prependData(loaderContext);
}

options.data = data ? data + os.EOL + content : content;
options.data = prependData ? prependData + os.EOL + content : content;

// opt.outputStyle
if (!options.outputStyle && isProductionLikeMode(loaderContext)) {
Expand Down
32 changes: 0 additions & 32 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -658,38 +658,6 @@ exports[`loader should work with the "bootstrap-sass" package, import as a packa

exports[`loader should work with the "bootstrap-sass" package, import as a package (node-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should work with the "data" option as a function (dart-sass) (sass): errors 1`] = `Array []`;

exports[`loader should work with the "data" option as a function (dart-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should work with the "data" option as a function (dart-sass) (scss): errors 1`] = `Array []`;

exports[`loader should work with the "data" option as a function (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should work with the "data" option as a function (node-sass) (sass): errors 1`] = `Array []`;

exports[`loader should work with the "data" option as a function (node-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should work with the "data" option as a function (node-sass) (scss): errors 1`] = `Array []`;

exports[`loader should work with the "data" option as a function (node-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should work with the "data" option as a string (dart-sass) (sass): errors 1`] = `Array []`;

exports[`loader should work with the "data" option as a string (dart-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should work with the "data" option as a string (dart-sass) (scss): errors 1`] = `Array []`;

exports[`loader should work with the "data" option as a string (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should work with the "data" option as a string (node-sass) (sass): errors 1`] = `Array []`;

exports[`loader should work with the "data" option as a string (node-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should work with the "data" option as a string (node-sass) (scss): errors 1`] = `Array []`;

exports[`loader should work with the "data" option as a string (node-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should work with the "functions" option as a function (dart-sass) (sass): errors 1`] = `Array []`;

exports[`loader should work with the "functions" option as a function (dart-sass) (sass): warnings 1`] = `Array []`;
Expand Down
33 changes: 33 additions & 0 deletions test/__snapshots__/prependData-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prependData option should work with the "data" option as a function (dart-sass) (sass): errors 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a function (dart-sass) (sass): warnings 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a function (dart-sass) (scss): errors 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a function (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a function (node-sass) (sass): errors 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a function (node-sass) (sass): warnings 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a function (node-sass) (scss): errors 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a function (node-sass) (scss): warnings 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a string (dart-sass) (sass): errors 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a string (dart-sass) (sass): warnings 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a string (dart-sass) (scss): errors 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a string (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a string (node-sass) (sass): errors 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a string (node-sass) (sass): warnings 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a string (node-sass) (scss): errors 1`] = `Array []`;

exports[`prependData option should work with the "data" option as a string (node-sass) (scss): warnings 1`] = `Array []`;
2 changes: 1 addition & 1 deletion test/helpers/getCodeFromSass.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getCodeFromSass(testId, options) {

const isSass = /\.sass$/i.test(testId);

if (sassOptions.data) {
if (sassOptions.prependData) {
sassOptions.indentedSyntax = isSass;
sassOptions.data = `$prepended-data: hotpink${
sassOptions.indentedSyntax ? '\n' : ';'
Expand Down
36 changes: 0 additions & 36 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,42 +109,6 @@ describe('loader', () => {
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it(`should work with the "data" option as a string (${implementationName}) (${syntax})`, async () => {
const testId = getTestId('prepending-data', syntax);
const options = {
implementation: getImplementationByName(implementationName),
data: `$prepended-data: hotpink${syntax === 'sass' ? '\n' : ';'}`,
};
const stats = await compile(testId, { loader: { options } });

expect(getCodeFromBundle(stats).css).toBe(
getCodeFromSass(testId, options).css
);

expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it(`should work with the "data" option as a function (${implementationName}) (${syntax})`, async () => {
const testId = getTestId('prepending-data', syntax);
const options = {
implementation: getImplementationByName(implementationName),
data: (loaderContext) => {
expect(loaderContext).toBeDefined();

return `$prepended-data: hotpink${syntax === 'sass' ? '\n' : ';'}`;
},
};
const stats = await compile(testId, { loader: { options } });

expect(getCodeFromBundle(stats).css).toBe(
getCodeFromSass(testId, options).css
);

expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it(`should work with the "includePaths" option (${implementationName}) (${syntax})`, async () => {
const testId = getTestId('import-include-paths', syntax);
const options = {
Expand Down
62 changes: 62 additions & 0 deletions test/prependData-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @jest-environment node
*/
import nodeSass from 'node-sass';
import dartSass from 'sass';

import {
compile,
getTestId,
getCodeFromBundle,
getCodeFromSass,
getImplementationByName,
} from './helpers';

const implementations = [nodeSass, dartSass];
const syntaxStyles = ['scss', 'sass'];

describe('prependData option', () => {
implementations.forEach((implementation) => {
const [implementationName] = implementation.info.split('\t');

syntaxStyles.forEach((syntax) => {
it(`should work with the "data" option as a string (${implementationName}) (${syntax})`, async () => {
const testId = getTestId('prepending-data', syntax);
const options = {
implementation: getImplementationByName(implementationName),
prependData: `$prepended-data: hotpink${
syntax === 'sass' ? '\n' : ';'
}`,
};
const stats = await compile(testId, { loader: { options } });

expect(getCodeFromBundle(stats).css).toBe(
getCodeFromSass(testId, options).css
);

expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it(`should work with the "data" option as a function (${implementationName}) (${syntax})`, async () => {
const testId = getTestId('prepending-data', syntax);
const options = {
implementation: getImplementationByName(implementationName),
prependData: (loaderContext) => {
expect(loaderContext).toBeDefined();

return `$prepended-data: hotpink${syntax === 'sass' ? '\n' : ';'}`;
},
};
const stats = await compile(testId, { loader: { options } });

expect(getCodeFromBundle(stats).css).toBe(
getCodeFromSass(testId, options).css
);

expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});
});
});
});