From df28035387c6ad80b64e1ef21726c8180d3494cd Mon Sep 17 00:00:00 2001 From: Johannes Ewald Date: Thu, 30 Mar 2017 19:50:43 +0200 Subject: [PATCH] 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 --- src/getOptions.js | 10 ++++++++++ test/helpers/createSpec.js | 1 + test/index.test.js | 22 +++++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/getOptions.js b/src/getOptions.js index e7ea0c89..51492ea1 100644 --- a/src/getOptions.js +++ b/src/getOptions.js @@ -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; } diff --git a/test/helpers/createSpec.js b/test/helpers/createSpec.js index 8c98827e..eb712ad3 100644 --- a/test/helpers/createSpec.js +++ b/test/helpers/createSpec.js @@ -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')}`, diff --git a/test/index.test.js b/test/index.test.js index b0fe2b6b..a6c1ea41 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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; @@ -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