Skip to content

Commit df28035

Browse files
committed
fix: sourcesContent missing in source maps
less-loader 4 did not automatically apply the sourceMap.outputSourceFiles option anymore. This led to a situation where source maps didn't work out of the box anymore (while it still was possible to set this option manually). Fixes 183
1 parent 786aa59 commit df28035

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/getOptions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ function getOptions(loaderContext) {
2424
options.plugins.push(createWebpackLessPlugin(loaderContext));
2525
}
2626

27+
if (options.sourceMap) {
28+
if (typeof options.sourceMap === 'boolean') {
29+
options.sourceMap = {};
30+
}
31+
if ('outputSourceFiles' in options.sourceMap === false) {
32+
// Include source files as `sourceContents` as sane default since this makes source maps "just work" in most cases
33+
options.sourceMap.outputSourceFiles = true;
34+
}
35+
}
36+
2737
return options;
2838
}
2939

test/helpers/createSpec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const lessOptions = {
2828
'--source-map',
2929
`--source-map-basepath=${projectPath}`,
3030
`--source-map-rootpath=${projectPath}`,
31+
'--source-map-less-inline',
3132
],
3233
'import-paths': [
3334
`--include-path=${path.resolve(fixturesPath, 'node_modules')}`,

test/index.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ test('should transform urls', async () => {
165165
await compileAndCompare('url-path');
166166
});
167167

168-
test('should generate source maps', async () => {
168+
test('should generate source maps with sourcesContent by default', async () => {
169169
let inspect;
170170
const rules = moduleRules.basic({ sourceMap: true }, {}, (i) => {
171171
inspect = i;
@@ -177,10 +177,30 @@ test('should generate source maps', async () => {
177177
]);
178178
const [actualCss, actualMap] = inspect.arguments;
179179

180+
expect(Array.isArray(actualMap.sourcesContent)).toBe(true);
181+
expect(actualMap.sourcesContent.length).toBe(2);
182+
183+
// We can't actually compare the sourcesContent because it's slightly different because of our import rewriting
184+
delete actualMap.sourcesContent;
185+
delete expectedMap.sourcesContent;
186+
180187
expect(actualCss).toEqual(expectedCss);
181188
expect(actualMap).toEqual(expectedMap);
182189
});
183190

191+
test('should be possible to override sourceMap.outputSourceFiles', async () => {
192+
let inspect;
193+
const rules = moduleRules.basic({ sourceMap: { outputSourceFiles: false } }, {}, (i) => {
194+
inspect = i;
195+
});
196+
197+
await compile('source-map', rules);
198+
199+
const [actualMap] = inspect.arguments;
200+
201+
expect(actualMap).not.toHaveProperty('sourcesContent');
202+
});
203+
184204
test('should install plugins', async () => {
185205
let pluginInstalled = false;
186206
// Using prototype inheritance here since Less plugins are usually instances of classes

0 commit comments

Comments
 (0)