Skip to content

Commit

Permalink
fix: sourcesContent missing in source maps
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jhnns committed Mar 30, 2017
1 parent 786aa59 commit df28035
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/getOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ function getOptions(loaderContext) {
options.plugins.push(createWebpackLessPlugin(loaderContext));
}

if (options.sourceMap) {
if (typeof options.sourceMap === 'boolean') {
options.sourceMap = {};
}
if ('outputSourceFiles' in options.sourceMap === false) {
// Include source files as `sourceContents` as sane default since this makes source maps "just work" in most cases
options.sourceMap.outputSourceFiles = true;
}
}

return options;
}

Expand Down
1 change: 1 addition & 0 deletions test/helpers/createSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const lessOptions = {
'--source-map',
`--source-map-basepath=${projectPath}`,
`--source-map-rootpath=${projectPath}`,
'--source-map-less-inline',
],
'import-paths': [
`--include-path=${path.resolve(fixturesPath, 'node_modules')}`,
Expand Down
22 changes: 21 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ test('should transform urls', async () => {
await compileAndCompare('url-path');
});

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

expect(Array.isArray(actualMap.sourcesContent)).toBe(true);
expect(actualMap.sourcesContent.length).toBe(2);

// We can't actually compare the sourcesContent because it's slightly different because of our import rewriting
delete actualMap.sourcesContent;
delete expectedMap.sourcesContent;

expect(actualCss).toEqual(expectedCss);
expect(actualMap).toEqual(expectedMap);
});

test('should be possible to override sourceMap.outputSourceFiles', async () => {
let inspect;
const rules = moduleRules.basic({ sourceMap: { outputSourceFiles: false } }, {}, (i) => {
inspect = i;
});

await compile('source-map', rules);

const [actualMap] = inspect.arguments;

expect(actualMap).not.toHaveProperty('sourcesContent');
});

test('should install plugins', async () => {
let pluginInstalled = false;
// Using prototype inheritance here since Less plugins are usually instances of classes
Expand Down

0 comments on commit df28035

Please sign in to comment.