Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support data as Function #648

Merged
merged 1 commit into from Dec 14, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -274,6 +274,27 @@ If you want to prepend Sass code before the actual entry file, you can set the `
}
```

The `data` option supports `Function` notation:

```javascript
{
loader: "sass-loader",
options: {
data: (loaderContext) => {
// More information about avalaible options https://webpack.js.org/api/loaders/
const { resourcePath, rootContext } = loaderContext;
const relativePath = path.relative(rootContext,resourcePath);

if (relativePath === "styles/foo.scss") {
return "$value: 100px;"
}

return "$value: 200px;"
}
}
}
```

**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.

<h2 align="center">Maintainers</h2>
Expand Down
8 changes: 7 additions & 1 deletion lib/normalizeOptions.js
Expand Up @@ -23,7 +23,13 @@ function normalizeOptions(loaderContext, content, webpackImporter) {
const options = cloneDeep(utils.getOptions(loaderContext)) || {};
const { resourcePath } = loaderContext;

options.data = options.data ? options.data + os.EOL + content : content;
let { data } = options;

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

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

// opt.outputStyle
if (!options.outputStyle && loaderContext.minimize) {
Expand Down
12 changes: 11 additions & 1 deletion test/index.test.js
Expand Up @@ -47,6 +47,8 @@ Object.defineProperty(loaderContextMock, 'options', {
},
});

/* global should */

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

Expand Down Expand Up @@ -185,10 +187,18 @@ implementations.forEach((implementation) => {
}));
});
describe('prepending data', () => {
it('should extend the data-option if present', () =>
it('should extend the data option if present and it is string', () =>
execTest('prepending-data', {
data: `$prepended-data: hotpink${ext === 'sass' ? '\n' : ';'}`,
}));
it('should extend the data option if present and it is function', () =>
execTest('prepending-data', {
data: (loaderContext) => {
should.exist(loaderContext);

return `$prepended-data: hotpink${ext === 'sass' ? '\n' : ';'}`;
},
}));
});
// See https://github.com/webpack-contrib/sass-loader/issues/21
describe('empty files', () => {
Expand Down