Skip to content

Commit aa64e1b

Browse files
feat: support data as Function (#648)
1 parent a8709c9 commit aa64e1b

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Diff for: README.md

+21
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ If you want to prepend Sass code before the actual entry file, you can set the `
274274
}
275275
```
276276

277+
The `data` option supports `Function` notation:
278+
279+
```javascript
280+
{
281+
loader: "sass-loader",
282+
options: {
283+
data: (loaderContext) => {
284+
// More information about avalaible options https://webpack.js.org/api/loaders/
285+
const { resourcePath, rootContext } = loaderContext;
286+
const relativePath = path.relative(rootContext,resourcePath);
287+
288+
if (relativePath === "styles/foo.scss") {
289+
return "$value: 100px;"
290+
}
291+
292+
return "$value: 200px;"
293+
}
294+
}
295+
}
296+
```
297+
277298
**Please note:** Since you're injecting code, this will break the source mappings in your entry file. Often there's a simpler solution than this, like multiple Sass entry files.
278299

279300
<h2 align="center">Maintainers</h2>

Diff for: lib/normalizeOptions.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ function normalizeOptions(loaderContext, content, webpackImporter) {
2323
const options = cloneDeep(utils.getOptions(loaderContext)) || {};
2424
const { resourcePath } = loaderContext;
2525

26-
options.data = options.data ? options.data + os.EOL + content : content;
26+
let { data } = options;
27+
28+
if (typeof options.data === 'function') {
29+
data = options.data(loaderContext);
30+
}
31+
32+
options.data = data ? data + os.EOL + content : content;
2733

2834
// opt.outputStyle
2935
if (!options.outputStyle && loaderContext.minimize) {

Diff for: test/index.test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Object.defineProperty(loaderContextMock, 'options', {
4747
},
4848
});
4949

50+
/* global should */
51+
5052
implementations.forEach((implementation) => {
5153
const [implementationName] = implementation.info.split('\t');
5254

@@ -185,10 +187,18 @@ implementations.forEach((implementation) => {
185187
}));
186188
});
187189
describe('prepending data', () => {
188-
it('should extend the data-option if present', () =>
190+
it('should extend the data option if present and it is string', () =>
189191
execTest('prepending-data', {
190192
data: `$prepended-data: hotpink${ext === 'sass' ? '\n' : ';'}`,
191193
}));
194+
it('should extend the data option if present and it is function', () =>
195+
execTest('prepending-data', {
196+
data: (loaderContext) => {
197+
should.exist(loaderContext);
198+
199+
return `$prepended-data: hotpink${ext === 'sass' ? '\n' : ';'}`;
200+
},
201+
}));
192202
});
193203
// See https://github.com/webpack-contrib/sass-loader/issues/21
194204
describe('empty files', () => {

0 commit comments

Comments
 (0)