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

Add additionalData option #374

Merged
merged 1 commit into from
Aug 24, 2020
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
35 changes: 12 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ And run `webpack` via your preferred method.

## Options

| Name | Type | Default | Description |
| :-------------------------------------: | :------------------: | :----------------------: | :----------------------------------------------- |
| **[`lessOptions`](#lessoptions)** | `{Object\|Function}` | `{ relativeUrls: true }` | Options for Less. |
| **[`prependData`](#prependdata)** | `{String\|Function}` | `undefined` | Prepends Less code before the actual entry file. |
| **[`appendData`](#appenddata)** | `{String\|Function}` | `undefined` | Prepends Less code after the actual entry file. |
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps. |
| **[`implementation`](#implementation)** | `{Object}` | `less` | Setup Less implementation to use. |
| Name | Type | Default | Description |
| :-------------------------------------: | :------------------: | :----------------------: | :----------------------------------------------------- |
| **[`lessOptions`](#lessoptions)** | `{Object\|Function}` | `{ relativeUrls: true }` | Options for Less. |
| **[`additionalData`](#additionalData)** | `{String\|Function}` | `undefined` | Prepends/Appends `Less` code to the actual entry file. |
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps. |
| **[`implementation`](#implementation)** | `{Object}` | `less` | Setup Less implementation to use. |

### `lessOptions`

Expand Down Expand Up @@ -134,12 +133,13 @@ module.exports = {
};
```

### `prependData`
### `additionalData`

Type: `String|Function`
Default: `undefined`

Prepends `Less` code before the actual entry file.
In this case, the `less-loader` will not override the source but just **prepend** the entry's content.

This is especially useful when some of your Less variables depend on the environment:

Expand All @@ -159,7 +159,7 @@ module.exports = {
{
loader: 'less-loader',
options: {
prependData: `@env: ${process.env.NODE_ENV};`,
additionalData: `@env: ${process.env.NODE_ENV};`,
},
},
],
Expand All @@ -183,16 +183,16 @@ module.exports = {
{
loader: 'less-loader',
options: {
prependData: (loaderContext) => {
additionalData: (content, loaderContext) => {
// More information about available properties https://webpack.js.org/api/loaders/
const { resourcePath, rootContext } = loaderContext;
const relativePath = path.relative(rootContext, resourcePath);

if (relativePath === 'styles/foo.less') {
return '@value: 100px;';
return '@value: 100px;' + content;
}

return '@value: 200px;';
return '@value: 200px;' + content;
},
},
},
Expand All @@ -203,17 +203,6 @@ module.exports = {
};
```

### `appendData`

Type: `String|Function`
Default: `undefined`

AppendData `Less` code after the actual entry file.

This can be useful when you need to rewrite some of your Less variables.:

> ℹ 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 Less entry files.

#### `String`

```js
Expand Down
15 changes: 4 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,11 @@ function lessLoader(source) {

let data = source;

if (typeof options.prependData !== 'undefined') {
if (typeof options.additionalData !== 'undefined') {
data =
typeof options.prependData === 'function'
? `${options.prependData(this)}\n${data}`
: `${options.prependData}\n${data}`;
}

if (typeof options.appendData !== 'undefined') {
data =
typeof options.appendData === 'function'
? `${data}\n${options.appendData(this)}`
: `${data}\n${options.appendData}`;
typeof options.additionalData === 'function'
? `${options.additionalData(data, this)}`
: `${options.additionalData}\n${data}`;
}

getLessImplementation(options.implementation)
Expand Down
15 changes: 2 additions & 13 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,8 @@
}
]
},
"prependData": {
"description": "Prepends `Less` code before the actual entry file (https://github.com/webpack-contrib/less-loader#prependdata).",
"anyOf": [
{
"type": "string"
},
{
"instanceof": "Function"
}
]
},
"appendData": {
"description": "Add `Less` code after the actual entry file (https://github.com/webpack-contrib/less-loader#postponeddata).",
"additionalData": {
"description": "Prepends/Appends `Less` code to the actual entry file (https://github.com/webpack-contrib/less-loader#additionalData).",
"anyOf": [
{
"type": "string"
Expand Down
27 changes: 27 additions & 0 deletions test/__snapshots__/additionalData-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`additionalData option should work additionalData data as function: css 1`] = `
"/* RelativePath: additional-data.less; */
.background {
color: coral;
}
.custom-class {
color: red;
}
"
`;

exports[`additionalData option should work additionalData data as function: errors 1`] = `Array []`;

exports[`additionalData option should work additionalData data as function: warnings 1`] = `Array []`;

exports[`additionalData option should work additionalData data as string: css 1`] = `
".background {
color: coral;
}
"
`;

exports[`additionalData option should work additionalData data as string: errors 1`] = `Array []`;

exports[`additionalData option should work additionalData data as string: warnings 1`] = `Array []`;
23 changes: 0 additions & 23 deletions test/__snapshots__/appendData-option.test.js.snap

This file was deleted.

23 changes: 0 additions & 23 deletions test/__snapshots__/prependData-option.test.js.snap

This file was deleted.

120 changes: 30 additions & 90 deletions test/__snapshots__/validate-options.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`validate options should throw an error on the "appendData" option with "/test/" value 1`] = `
exports[`validate options should throw an error on the "additionalData" option with "/test/" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.appendData should be one of these:
- options.additionalData should be one of these:
string | function
-> Add \`Less\` code after the actual entry file (https://github.com/webpack-contrib/less-loader#postponeddata).
-> Prepends/Appends \`Less\` code to the actual entry file (https://github.com/webpack-contrib/less-loader#additionalData).
Details:
* options.appendData should be a string.
* options.appendData should be an instance of function."
* options.additionalData should be a string.
* options.additionalData should be an instance of function."
`;

exports[`validate options should throw an error on the "appendData" option with "[]" value 1`] = `
exports[`validate options should throw an error on the "additionalData" option with "[]" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.appendData should be one of these:
- options.additionalData should be one of these:
string | function
-> Add \`Less\` code after the actual entry file (https://github.com/webpack-contrib/less-loader#postponeddata).
-> Prepends/Appends \`Less\` code to the actual entry file (https://github.com/webpack-contrib/less-loader#additionalData).
Details:
* options.appendData should be a string.
* options.appendData should be an instance of function."
* options.additionalData should be a string.
* options.additionalData should be an instance of function."
`;

exports[`validate options should throw an error on the "appendData" option with "{}" value 1`] = `
exports[`validate options should throw an error on the "additionalData" option with "{}" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.appendData should be one of these:
- options.additionalData should be one of these:
string | function
-> Add \`Less\` code after the actual entry file (https://github.com/webpack-contrib/less-loader#postponeddata).
-> Prepends/Appends \`Less\` code to the actual entry file (https://github.com/webpack-contrib/less-loader#additionalData).
Details:
* options.appendData should be a string.
* options.appendData should be an instance of function."
* options.additionalData should be a string.
* options.additionalData should be an instance of function."
`;

exports[`validate options should throw an error on the "appendData" option with "1" value 1`] = `
exports[`validate options should throw an error on the "additionalData" option with "1" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.appendData should be one of these:
- options.additionalData should be one of these:
string | function
-> Add \`Less\` code after the actual entry file (https://github.com/webpack-contrib/less-loader#postponeddata).
-> Prepends/Appends \`Less\` code to the actual entry file (https://github.com/webpack-contrib/less-loader#additionalData).
Details:
* options.appendData should be a string.
* options.appendData should be an instance of function."
* options.additionalData should be a string.
* options.additionalData should be an instance of function."
`;

exports[`validate options should throw an error on the "appendData" option with "false" value 1`] = `
exports[`validate options should throw an error on the "additionalData" option with "false" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.appendData should be one of these:
- options.additionalData should be one of these:
string | function
-> Add \`Less\` code after the actual entry file (https://github.com/webpack-contrib/less-loader#postponeddata).
-> Prepends/Appends \`Less\` code to the actual entry file (https://github.com/webpack-contrib/less-loader#additionalData).
Details:
* options.appendData should be a string.
* options.appendData should be an instance of function."
* options.additionalData should be a string.
* options.additionalData should be an instance of function."
`;

exports[`validate options should throw an error on the "appendData" option with "true" value 1`] = `
exports[`validate options should throw an error on the "additionalData" option with "true" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.appendData should be one of these:
- options.additionalData should be one of these:
string | function
-> Add \`Less\` code after the actual entry file (https://github.com/webpack-contrib/less-loader#postponeddata).
-> Prepends/Appends \`Less\` code to the actual entry file (https://github.com/webpack-contrib/less-loader#additionalData).
Details:
* options.appendData should be a string.
* options.appendData should be an instance of function."
* options.additionalData should be a string.
* options.additionalData should be an instance of function."
`;

exports[`validate options should throw an error on the "implementation" option with "false" value 1`] = `
Expand Down Expand Up @@ -136,66 +136,6 @@ exports[`validate options should throw an error on the "lessOptions" option with
* options.lessOptions should be an instance of function."
`;

exports[`validate options should throw an error on the "prependData" option with "/test/" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.prependData should be one of these:
string | function
-> Prepends \`Less\` code before the actual entry file (https://github.com/webpack-contrib/less-loader#prependdata).
Details:
* options.prependData should be a string.
* options.prependData should be an instance of function."
`;

exports[`validate options should throw an error on the "prependData" option with "[]" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.prependData should be one of these:
string | function
-> Prepends \`Less\` code before the actual entry file (https://github.com/webpack-contrib/less-loader#prependdata).
Details:
* options.prependData should be a string.
* options.prependData should be an instance of function."
`;

exports[`validate options should throw an error on the "prependData" option with "{}" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.prependData should be one of these:
string | function
-> Prepends \`Less\` code before the actual entry file (https://github.com/webpack-contrib/less-loader#prependdata).
Details:
* options.prependData should be a string.
* options.prependData should be an instance of function."
`;

exports[`validate options should throw an error on the "prependData" option with "1" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.prependData should be one of these:
string | function
-> Prepends \`Less\` code before the actual entry file (https://github.com/webpack-contrib/less-loader#prependdata).
Details:
* options.prependData should be a string.
* options.prependData should be an instance of function."
`;

exports[`validate options should throw an error on the "prependData" option with "false" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.prependData should be one of these:
string | function
-> Prepends \`Less\` code before the actual entry file (https://github.com/webpack-contrib/less-loader#prependdata).
Details:
* options.prependData should be a string.
* options.prependData should be an instance of function."
`;

exports[`validate options should throw an error on the "prependData" option with "true" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.prependData should be one of these:
string | function
-> Prepends \`Less\` code before the actual entry file (https://github.com/webpack-contrib/less-loader#prependdata).
Details:
* options.prependData should be a string.
* options.prependData should be an instance of function."
`;

exports[`validate options should throw an error on the "sourceMap" option with "string" value 1`] = `
"Invalid options object. Less Loader has been initialized using an options object that does not match the API schema.
- options.sourceMap should be a boolean.
Expand Down
Loading