Skip to content

Commit fb94605

Browse files
feat: added the appendData option (#336)
1 parent 24021cd commit fb94605

File tree

5 files changed

+128
-8
lines changed

5 files changed

+128
-8
lines changed

README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ module.exports = {
7777
options: {
7878
lessOptions: {
7979
strictMath: true,
80-
noIeCompat: true,
8180
},
8281
},
8382
},
@@ -197,6 +196,75 @@ module.exports = {
197196
};
198197
```
199198

199+
### `appendData`
200+
201+
Type: `String|Function`
202+
Default: `undefined`
203+
204+
AppendData `Less` code after the actual entry file.
205+
206+
This can be useful when you need to rewrite some of your Less variables.:
207+
208+
> ℹ 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.
209+
210+
#### `String`
211+
212+
```js
213+
module.exports = {
214+
module: {
215+
rules: [
216+
{
217+
test: /\.less$/,
218+
use: [
219+
'style-loader',
220+
'css-loader',
221+
{
222+
loader: 'less-loader',
223+
options: {
224+
appendData: `@env: ${process.env.NODE_ENV};`,
225+
},
226+
},
227+
],
228+
},
229+
],
230+
},
231+
};
232+
```
233+
234+
#### `Function`
235+
236+
```js
237+
module.exports = {
238+
module: {
239+
rules: [
240+
{
241+
test: /\.less$/,
242+
use: [
243+
'style-loader',
244+
'css-loader',
245+
{
246+
loader: 'less-loader',
247+
options: {
248+
appendData: (loaderContext) => {
249+
// More information about available properties https://webpack.js.org/api/loaders/
250+
const { resourcePath, rootContext } = loaderContext;
251+
const relativePath = path.relative(rootContext, resourcePath);
252+
253+
if (relativePath === 'styles/foo.less') {
254+
return '@value: 100px;';
255+
}
256+
257+
return '@value: 200px;';
258+
},
259+
},
260+
},
261+
],
262+
},
263+
],
264+
},
265+
};
266+
```
267+
200268
### `sourceMap`
201269

202270
Type: `Boolean`

src/getLessOptions.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os from 'os';
2-
31
import clone from 'clone';
42

53
import createWebpackLessPlugin from './createWebpackLessPlugin';
@@ -13,6 +11,26 @@ import createWebpackLessPlugin from './createWebpackLessPlugin';
1311
* @returns {Object}
1412
*/
1513
function getLessOptions(loaderContext, loaderOptions, content) {
14+
function prependData(target, addedData) {
15+
if (!addedData) {
16+
return target;
17+
}
18+
19+
return typeof addedData === 'function'
20+
? `${addedData(loaderContext)}\n${target}`
21+
: `${addedData}\n${target}`;
22+
}
23+
24+
function appendData(target, addedData) {
25+
if (!addedData) {
26+
return target;
27+
}
28+
29+
return typeof addedData === 'function'
30+
? `${target}\n${addedData(loaderContext)}`
31+
: `${target}\n${addedData}`;
32+
}
33+
1634
const options = clone(
1735
loaderOptions.lessOptions
1836
? typeof loaderOptions.lessOptions === 'function'
@@ -21,11 +39,9 @@ function getLessOptions(loaderContext, loaderOptions, content) {
2139
: {}
2240
);
2341

24-
const data = loaderOptions.prependData
25-
? typeof loaderOptions.prependData === 'function'
26-
? loaderOptions.prependData(loaderContext) + os.EOL + content
27-
: loaderOptions.prependData + os.EOL + content
28-
: content;
42+
let data = content;
43+
data = prependData(data, loaderOptions.prependData);
44+
data = appendData(data, loaderOptions.appendData);
2945

3046
const lessOptions = {
3147
plugins: [],

src/options.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
}
2525
]
2626
},
27+
"appendData": {
28+
"description": "Add `Less` code after the actual entry file (https://github.com/webpack-contrib/less-loader#postponeddata).",
29+
"anyOf": [
30+
{
31+
"type": "string"
32+
},
33+
{
34+
"instanceof": "Function"
35+
}
36+
]
37+
},
2738
"sourceMap": {
2839
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/less-loader#sourcemap).",
2940
"type": "boolean"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@color1: red;
2+
3+
.background {
4+
color: @color1;
5+
}

test/index.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ test('should prepend data', async () => {
151151
expect(css).toEqual('.background {\n color: red;\n}\n');
152152
});
153153

154+
test('should append data', async () => {
155+
const loaderOptions = {
156+
appendData() {
157+
return `@color1: coral;`;
158+
},
159+
};
160+
161+
let inspect;
162+
163+
const rules = moduleRules.basic(loaderOptions, {}, (i) => {
164+
inspect = i;
165+
});
166+
167+
await compile('append-data', rules).catch((e) => e);
168+
169+
const [css] = inspect.arguments;
170+
171+
expect(css).toEqual('.background {\n color: coral;\n}\n');
172+
});
173+
154174
test('should allow a function to be passed through for `lessOptions`', async () => {
155175
await compileAndCompare('import-paths', {
156176
lessLoaderOptions: {

0 commit comments

Comments
 (0)