From e4497c6dd1a3a3bb1724a41ae163b1d9cd6eab85 Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Fri, 3 Aug 2018 18:53:39 -0400 Subject: [PATCH 01/11] Allow the user to opt out of legacy handling, but keep it as the default out of sensitivity to existing scripts that may expect this behavior. Signed-off-by: Nick Petruzzelli --- README.md | 15 +++++++++++ lib/loader.js | 65 +++++++++++++++++++++++++++++++++++++++++------ lib/processCss.js | 10 ++++++-- 3 files changed, 80 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d0b8f631..3d302f98 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,21 @@ They are not enabled by default because they expose a runtime overhead and incre } ``` +#### Opting out of legacy source maps behavior + +To receive source maps as URLs relative to `loader.resourcePath`, set the `legacySourceMaps` option to `false`. + + +```javascript +{ + loader: 'css-loader', + options: { + legacySourceMaps: false, + sourceMap: true + } +} +``` + ### `camelCase` By default, the exported JSON keys mirror the class names. If you want to camelize class names (useful in JS), pass the query parameter `camelCase` to css-loader. diff --git a/lib/loader.js b/lib/loader.js index b76c86de..d0349ed7 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -2,11 +2,30 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ +var path = require("path"); var loaderUtils = require("loader-utils"); var processCss = require("./processCss"); var getImportPrefix = require("./getImportPrefix"); var compileExports = require("./compile-exports"); +/** + * If the file was "renamed" (for the purposes of source maps), then honor it, + * otherwise just use the resourcePath. + * @param {String} resourcePath - The absolute file system path for the sass file. + * @param {Object} map + * @return {String} + */ +function processFrom(resourcePath, map) { + var effectiveResourcePath; + if (map && map.file && typeof map.file === 'string' && path.dirname(map.file) === '.') { + // Something else has already changed the file name or extension, so + // honor it for the purpose of creating the next source map. + effectiveResourcePath = path.join(path.dirname(resourcePath), map.file); + } else { + effectiveResourcePath = resourcePath; + } + return effectiveResourcePath; +} module.exports = function(content, map) { var callback = this.async(); @@ -14,6 +33,8 @@ module.exports = function(content, map) { var moduleMode = query.modules; var camelCaseKeys = query.camelCase; var sourceMap = query.sourceMap || false; + var processCssFrom; + var processCssTo; if(sourceMap) { if (map) { @@ -33,10 +54,36 @@ module.exports = function(content, map) { map = null; } + if (query.legacySourceMaps !== false) { + processCssFrom = loaderUtils.getRemainingRequest(this).split("!").pop(); + processCssTo = loaderUtils.getCurrentRequest(this).split("!").pop(); + } else { + /** + * > To ensure that PostCSS generates source maps and displays better syntax + * > errors, runners must specify the from and to options. If your runner + * > does not handle writing to disk (for example, a gulp transform), you + * > should set both options to point to the same file + * @see postcss [PostCSS Runner Guidelines]{@link https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#21-set-from-and-to-processing-options} + * + * `css-loader` isn't responsible for writing the map, so it doesn't have to + * worry about updating the map with a transformation that changes locations + * (suchs as map.file or map.sources). + * + * Changing the file extension counts as changing the location because it + * changes the path. + * + * PostCSS's `from` and `to` arguments are only concerned with the file + * system. They don't know about, care about, or understand the webpack + * loader's current request or remaining request. + */ + processCssFrom = processFrom(this.resourcePath, map); + processCssTo = processCssFrom; + } + processCss(content, map, { mode: moduleMode ? "local" : "global", - from: loaderUtils.getRemainingRequest(this).split("!").pop(), - to: loaderUtils.getCurrentRequest(this).split("!").pop(), + from: processCssFrom, + to: processCssTo, query: query, loaderContext: this, sourceMap: sourceMap @@ -112,13 +159,15 @@ module.exports = function(content, map) { if(sourceMap && result.map) { // add a SourceMap map = result.map; - if(map.sources) { - map.sources = map.sources.map(function(source) { - return source.split("!").pop().replace(/\\/g, '/'); - }, this); - map.sourceRoot = ""; + if (query.legacySourceMaps !== false) { + if(map.sources) { + map.sources = map.sources.map(function(source) { + return source.split("!").pop().replace(/\\/g, '/'); + }, this); + map.sourceRoot = ""; + } + map.file = map.file.split("!").pop().replace(/\\/g, '/'); } - map.file = map.file.split("!").pop().replace(/\\/g, '/'); map = JSON.stringify(map); moduleJs = "exports.push([module.id, " + cssAsString + ", \"\", " + map + "]);"; } else { diff --git a/lib/processCss.js b/lib/processCss.js index 61eb7ce5..cb2db471 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -179,9 +179,15 @@ module.exports = function processCss(inputSource, inputMap, options, callback) { parserPlugin(parserOptions) ]); - pipeline.process(inputSource, { + if (options.query.legacySourceMaps !== false) { // we need a prefix to avoid path rewriting of PostCSS - from: "/css-loader!" + options.from, + postCssFrom = "/css-loader!" + options.from; + } else { + postCssFrom = options.from; + } + + pipeline.process(inputSource, { + from: postCssFrom, to: options.to, map: options.sourceMap ? { prev: inputMap, From 7f4f7a8196aadf863ce5fa708a42c6b5e59d5328 Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Fri, 3 Aug 2018 19:58:21 -0400 Subject: [PATCH 02/11] Address Lint errors and missing variable declaration. Signed-off-by: Nick Petruzzelli --- lib/loader.js | 7 ++++--- lib/processCss.js | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/loader.js b/lib/loader.js index d0349ed7..ba4206fa 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -12,8 +12,8 @@ var compileExports = require("./compile-exports"); * If the file was "renamed" (for the purposes of source maps), then honor it, * otherwise just use the resourcePath. * @param {String} resourcePath - The absolute file system path for the sass file. - * @param {Object} map - * @return {String} + * @param {Object|null} map - An existing source map, if any. + * @return {String} - The effective path to use for the `from` argument. */ function processFrom(resourcePath, map) { var effectiveResourcePath; @@ -58,6 +58,7 @@ module.exports = function(content, map) { processCssFrom = loaderUtils.getRemainingRequest(this).split("!").pop(); processCssTo = loaderUtils.getCurrentRequest(this).split("!").pop(); } else { + /** * > To ensure that PostCSS generates source maps and displays better syntax * > errors, runners must specify the from and to options. If your runner @@ -71,7 +72,7 @@ module.exports = function(content, map) { * * Changing the file extension counts as changing the location because it * changes the path. - * + * * PostCSS's `from` and `to` arguments are only concerned with the file * system. They don't know about, care about, or understand the webpack * loader's current request or remaining request. diff --git a/lib/processCss.js b/lib/processCss.js index cb2db471..306f96fd 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -179,6 +179,7 @@ module.exports = function processCss(inputSource, inputMap, options, callback) { parserPlugin(parserOptions) ]); + var postCssFrom; if (options.query.legacySourceMaps !== false) { // we need a prefix to avoid path rewriting of PostCSS postCssFrom = "/css-loader!" + options.from; From 8cf05a9519edd394f05cd183e40e771b7b84dafa Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Sun, 5 Aug 2018 13:51:57 -0400 Subject: [PATCH 03/11] Remove optional support of legacy handling. Signed-off-by: Nick Petruzzelli --- README.md | 15 ------------- lib/loader.js | 55 +++++++++++++++++------------------------------ lib/processCss.js | 10 +-------- 3 files changed, 21 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 3d302f98..d0b8f631 100644 --- a/README.md +++ b/README.md @@ -294,21 +294,6 @@ They are not enabled by default because they expose a runtime overhead and incre } ``` -#### Opting out of legacy source maps behavior - -To receive source maps as URLs relative to `loader.resourcePath`, set the `legacySourceMaps` option to `false`. - - -```javascript -{ - loader: 'css-loader', - options: { - legacySourceMaps: false, - sourceMap: true - } -} -``` - ### `camelCase` By default, the exported JSON keys mirror the class names. If you want to camelize class names (useful in JS), pass the query parameter `camelCase` to css-loader. diff --git a/lib/loader.js b/lib/loader.js index ba4206fa..e302a83c 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -54,32 +54,26 @@ module.exports = function(content, map) { map = null; } - if (query.legacySourceMaps !== false) { - processCssFrom = loaderUtils.getRemainingRequest(this).split("!").pop(); - processCssTo = loaderUtils.getCurrentRequest(this).split("!").pop(); - } else { - - /** - * > To ensure that PostCSS generates source maps and displays better syntax - * > errors, runners must specify the from and to options. If your runner - * > does not handle writing to disk (for example, a gulp transform), you - * > should set both options to point to the same file - * @see postcss [PostCSS Runner Guidelines]{@link https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#21-set-from-and-to-processing-options} - * - * `css-loader` isn't responsible for writing the map, so it doesn't have to - * worry about updating the map with a transformation that changes locations - * (suchs as map.file or map.sources). - * - * Changing the file extension counts as changing the location because it - * changes the path. - * - * PostCSS's `from` and `to` arguments are only concerned with the file - * system. They don't know about, care about, or understand the webpack - * loader's current request or remaining request. - */ - processCssFrom = processFrom(this.resourcePath, map); - processCssTo = processCssFrom; - } + /** + * > To ensure that PostCSS generates source maps and displays better syntax + * > errors, runners must specify the from and to options. If your runner + * > does not handle writing to disk (for example, a gulp transform), you + * > should set both options to point to the same file + * @see postcss [PostCSS Runner Guidelines]{@link https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#21-set-from-and-to-processing-options} + * + * `css-loader` isn't responsible for writing the map, so it doesn't have to + * worry about updating the map with a transformation that changes locations + * (suchs as map.file or map.sources). + * + * Changing the file extension counts as changing the location because it + * changes the path. + * + * PostCSS's `from` and `to` arguments are only concerned with the file + * system. They don't know about, care about, or understand the webpack + * loader's current request or remaining request. + */ + processCssFrom = processFrom(this.resourcePath, map); + processCssTo = processCssFrom; processCss(content, map, { mode: moduleMode ? "local" : "global", @@ -160,15 +154,6 @@ module.exports = function(content, map) { if(sourceMap && result.map) { // add a SourceMap map = result.map; - if (query.legacySourceMaps !== false) { - if(map.sources) { - map.sources = map.sources.map(function(source) { - return source.split("!").pop().replace(/\\/g, '/'); - }, this); - map.sourceRoot = ""; - } - map.file = map.file.split("!").pop().replace(/\\/g, '/'); - } map = JSON.stringify(map); moduleJs = "exports.push([module.id, " + cssAsString + ", \"\", " + map + "]);"; } else { diff --git a/lib/processCss.js b/lib/processCss.js index 306f96fd..430cf830 100644 --- a/lib/processCss.js +++ b/lib/processCss.js @@ -179,16 +179,8 @@ module.exports = function processCss(inputSource, inputMap, options, callback) { parserPlugin(parserOptions) ]); - var postCssFrom; - if (options.query.legacySourceMaps !== false) { - // we need a prefix to avoid path rewriting of PostCSS - postCssFrom = "/css-loader!" + options.from; - } else { - postCssFrom = options.from; - } - pipeline.process(inputSource, { - from: postCssFrom, + from: options.from, to: options.to, map: options.sourceMap ? { prev: inputMap, From 027c8ca210f05a7261ba104e5bb2bc6e0e02d034 Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Mon, 13 Aug 2018 18:19:38 -0400 Subject: [PATCH 04/11] Update assertions used in tests for source maps. Signed-off-by: Nick Petruzzelli --- test/sourceMapTest.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/test/sourceMapTest.js b/test/sourceMapTest.js index faf7578c..3364aba5 100644 --- a/test/sourceMapTest.js +++ b/test/sourceMapTest.js @@ -44,7 +44,6 @@ describe("source maps", function() { file: 'test.css', mappings: 'AAAA,SAAS,SAAS,EAAE', names: [], - sourceRoot: '', sources: [ '/folder/test.css' ], sourcesContent: [ '.class { a: b c d; }' ], version: 3 @@ -70,7 +69,6 @@ describe("source maps", function() { file: 'test.css', mappings: 'AAAA,SAAS,SAAS,EAAE', names: [], - sourceRoot: '', sources: [ '/folder/test.css' ], sourcesContent: [ '.class { a: b c d; }' ], version: 3 @@ -83,6 +81,7 @@ describe("source maps", function() { testMap("generate sourceMap (1 loader)", ".class { a: b c d; }", undefined, { loaders: [{request: "/path/css-loader"}], resource: "/folder/test.css", + resourcePath: "/folder/test.css", request: "/path/css-loader!/folder/test.css", query: "?sourceMap" }, [ @@ -90,8 +89,7 @@ describe("source maps", function() { file: 'test.css', mappings: 'AAAA,SAAS,SAAS,EAAE', names: [], - sourceRoot: '', - sources: [ '/folder/test.css' ], + sources: [ 'test.css' ], sourcesContent: [ '.class { a: b c d; }' ], version: 3 }] @@ -99,6 +97,7 @@ describe("source maps", function() { testMap("generate sourceMap (1 loader, relative)", ".class { a: b c d; }", undefined, { loaders: [{request: "/path/css-loader"}], resource: "/folder/test.css", + resourcePath: "/folder/test.css", request: "/path/css-loader!/folder/test.css", query: "?sourceMap" }, [ @@ -106,8 +105,7 @@ describe("source maps", function() { file: 'test.css', mappings: 'AAAA,SAAS,SAAS,EAAE', names: [], - sourceRoot: '', - sources: [ '/folder/test.css' ], + sources: [ 'test.css' ], sourcesContent: [ '.class { a: b c d; }' ], version: 3 }] @@ -115,6 +113,7 @@ describe("source maps", function() { testMap("generate sourceMap (1 loader, data url)", ".class { background-image: url(\"data:image/svg+xml;charset=utf-8,\"); }", undefined, { loaders: [{request: "/path/css-loader"}], resource: "/folder/test.css", + resourcePath: "/folder/test.css", request: "/path/css-loader!/folder/test.css", query: "?sourceMap" }, [ @@ -122,8 +121,7 @@ describe("source maps", function() { file: 'test.css', mappings: 'AAAA,SAAS,6WAA6W,EAAE', names: [], - sourceRoot: '', - sources: [ '/folder/test.css' ], + sources: [ 'test.css' ], sourcesContent: [ '.class { background-image: url("data:image/svg+xml;charset=utf-8,"); }' ], version: 3 }] @@ -131,6 +129,7 @@ describe("source maps", function() { testMap("generate sourceMap (1 loader, encoded data url)", ".class { background-image: url(\"data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2042%2026%27%20fill%3D%27%23007aff%27%3E%3Crect%20width%3D%274%27%20height%3D%274%27%2F%3E%3Crect%20x%3D%278%27%20y%3D%271%27%20width%3D%2734%27%20height%3D%272%27%2F%3E%3Crect%20y%3D%2711%27%20width%3D%274%27%20height%3D%274%27%2F%3E%3Crect%20x%3D%278%27%20y%3D%2712%27%20width%3D%2734%27%20height%3D%272%27%2F%3E%3Crect%20y%3D%2722%27%20width%3D%274%27%20height%3D%274%27%2F%3E%3Crect%20x%3D%278%27%20y%3D%2723%27%20width%3D%2734%27%20height%3D%272%27%2F%3E%3C%2Fsvg%3E\"); }", undefined, { loaders: [{request: "/path/css-loader"}], resource: "/folder/test.css", + resourcePath: "/folder/test.css", request: "/path/css-loader!/folder/test.css", query: "?sourceMap" }, [ @@ -138,8 +137,7 @@ describe("source maps", function() { file: 'test.css', mappings: 'AAAA,SAAS,mmBAAmmB,EAAE', names: [], - sourceRoot: '', - sources: [ '/folder/test.css' ], + sources: [ 'test.css' ], sourcesContent: [ '.class { background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2042%2026%27%20fill%3D%27%23007aff%27%3E%3Crect%20width%3D%274%27%20height%3D%274%27%2F%3E%3Crect%20x%3D%278%27%20y%3D%271%27%20width%3D%2734%27%20height%3D%272%27%2F%3E%3Crect%20y%3D%2711%27%20width%3D%274%27%20height%3D%274%27%2F%3E%3Crect%20x%3D%278%27%20y%3D%2712%27%20width%3D%2734%27%20height%3D%272%27%2F%3E%3Crect%20y%3D%2722%27%20width%3D%274%27%20height%3D%274%27%2F%3E%3Crect%20x%3D%278%27%20y%3D%2723%27%20width%3D%2734%27%20height%3D%272%27%2F%3E%3C%2Fsvg%3E"); }' ], version: 3 }] @@ -147,6 +145,7 @@ describe("source maps", function() { testMap("generate sourceMap (2 loaders)", ".class { a: b c d; }", undefined, { loaders: [{request: "/path/css-loader"}, {request: "/path/sass-loader"}], resource: "/folder/test.scss", + resourcePath: "/folder/test.scss", request: "/path/css-loader!/path/sass-loader!/folder/test.scss", query: "?sourceMap" }, [ @@ -154,8 +153,7 @@ describe("source maps", function() { file: 'test.scss', mappings: 'AAAA,SAAS,SAAS,EAAE', names: [], - sourceRoot: '', - sources: [ '/folder/test.scss' ], + sources: [ 'test.scss' ], sourcesContent: [ '.class { a: b c d; }' ], version: 3 }] @@ -165,12 +163,13 @@ describe("source maps", function() { mappings: 'AAAA,SAAS,SAAS,EAAE', names: [], sourceRoot: '', - sources: [ '/folder/test.scss' ], + sources: [ 'test.scss' ], sourcesContent: [ '.class { a: b c d; }' ], version: 3 }, { loaders: [{request: "/path/css-loader"}, {request: "/path/sass-loader"}], resource: "/folder/test.scss", + resourcePath: "/folder/test.scss", request: "/path/css-loader!/path/sass-loader!/folder/test.scss", query: "?sourceMap" }, [ @@ -178,8 +177,7 @@ describe("source maps", function() { file: 'test.scss', mappings: 'AAAA,SAAS,SAAS,EAAE', names: [], - sourceRoot: '', - sources: [ '/folder/test.scss' ], + sources: [ 'test.scss' ], sourcesContent: [ '.class { a: b c d; }' ], version: 3 }] From 32e7f46b7f3761892d87cd6ec38a5c4a6c9b6799 Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Thu, 23 Aug 2018 19:19:06 -0400 Subject: [PATCH 05/11] Clean up `from` and `to` arguments for PostCSS processing. Signed-off-by: Nick Petruzzelli --- lib/loader.js | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/lib/loader.js b/lib/loader.js index e302a83c..8221c58b 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -2,39 +2,18 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -var path = require("path"); var loaderUtils = require("loader-utils"); var processCss = require("./processCss"); var getImportPrefix = require("./getImportPrefix"); var compileExports = require("./compile-exports"); -/** - * If the file was "renamed" (for the purposes of source maps), then honor it, - * otherwise just use the resourcePath. - * @param {String} resourcePath - The absolute file system path for the sass file. - * @param {Object|null} map - An existing source map, if any. - * @return {String} - The effective path to use for the `from` argument. - */ -function processFrom(resourcePath, map) { - var effectiveResourcePath; - if (map && map.file && typeof map.file === 'string' && path.dirname(map.file) === '.') { - // Something else has already changed the file name or extension, so - // honor it for the purpose of creating the next source map. - effectiveResourcePath = path.join(path.dirname(resourcePath), map.file); - } else { - effectiveResourcePath = resourcePath; - } - return effectiveResourcePath; -} - module.exports = function(content, map) { var callback = this.async(); var query = loaderUtils.getOptions(this) || {}; var moduleMode = query.modules; var camelCaseKeys = query.camelCase; var sourceMap = query.sourceMap || false; - var processCssFrom; - var processCssTo; + var resourcePath; if(sourceMap) { if (map) { @@ -72,13 +51,12 @@ module.exports = function(content, map) { * system. They don't know about, care about, or understand the webpack * loader's current request or remaining request. */ - processCssFrom = processFrom(this.resourcePath, map); - processCssTo = processCssFrom; + resourcePath = this.resourcePath; processCss(content, map, { mode: moduleMode ? "local" : "global", - from: processCssFrom, - to: processCssTo, + from: resourcePath, + to: resourcePath, query: query, loaderContext: this, sourceMap: sourceMap From ffdd935c22c6a6d1c3d544ff547a74917c8ae38b Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Fri, 30 Nov 2018 14:35:13 -0500 Subject: [PATCH 06/11] Fix linting errors in `lib/loader.js` --- lib/loader.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/loader.js b/lib/loader.js index e8026b38..14b902e6 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -11,8 +11,6 @@ const { getOptions, isUrlRequest, urlToRequest, - getRemainingRequest, - getCurrentRequest, stringifyRequest, } = require('loader-utils'); @@ -118,9 +116,8 @@ module.exports = function loader(content, map) { * system. They don't know about, care about, or understand the webpack * loader's current request or remaining request. */ - const resourcePath = this.resourcePath; + const { resourcePath } = this; - postcss(plugins) .process(content, { from: resourcePath, @@ -257,8 +254,7 @@ module.exports = function loader(content, map) { if (exportCode) { exportCode = `exports.locals = ${exportCode};`; } - let newMap = result.map; - + const newMap = result.map; const runtimeCode = `exports = module.exports = require(${stringifyRequest( this, From 16c317a0de76baf7b069ed4d180087e6cf313e33 Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Fri, 30 Nov 2018 14:38:16 -0500 Subject: [PATCH 07/11] Fix missing hyphen in default devtool value. --- test/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers.js b/test/helpers.js index 8bfaf3d7..48c0edb2 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -154,7 +154,7 @@ function compile(fixture, config = {}, options = {}) { // eslint-disable-next-line no-param-reassign config = { mode: 'development', - devtool: config.devtool || 'sourcemap', + devtool: config.devtool || 'source-map', context: path.resolve(__dirname, 'fixtures'), entry: path.resolve(__dirname, 'fixtures', fixture), output: outputConfig(config), From 68bd963a79efb33a98b0d800675c095ff25638ab Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Fri, 30 Nov 2018 14:39:49 -0500 Subject: [PATCH 08/11] Account for valid falsey values passed for devtool option in test helper --- test/helpers.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/helpers.js b/test/helpers.js index 48c0edb2..b01de145 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -154,7 +154,9 @@ function compile(fixture, config = {}, options = {}) { // eslint-disable-next-line no-param-reassign config = { mode: 'development', - devtool: config.devtool || 'source-map', + + // Account for valid `false` values for devtool + devtool: config.devtool != null ? config.devtool : 'source-map', context: path.resolve(__dirname, 'fixtures'), entry: path.resolve(__dirname, 'fixtures', fixture), output: outputConfig(config), From 26680b0d151375dab0c545713f15b9a0cea12343 Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Fri, 30 Nov 2018 15:02:44 -0500 Subject: [PATCH 09/11] Get the compiled source map object from within a test --- test/sourceMap-option.test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/sourceMap-option.test.js b/test/sourceMap-option.test.js index 76d24369..44689adc 100644 --- a/test/sourceMap-option.test.js +++ b/test/sourceMap-option.test.js @@ -15,6 +15,26 @@ describe('sourceMap option', () => { const { modules } = stats.toJson(); const module = modules.find((m) => m.id === testId); + /** + * Get the actual source map from the OriginalSource object. + * @see {@link https://github.com/webpack/webpack-sources} + * @todo check that the source map exists + * @todo check that the source map has valid and expected property values. + */ + const compilationModules = stats.compilation.modules; + const compilationModule = compilationModules.find((m) => m.id === testId); + const moduleOriginalSource = compilationModule._source; // eslint-disable-line no-underscore-dangle + const moduleSourceMap = moduleOriginalSource.map(); + + /** + * @todo remove `console.log` statments + */ + /* eslint-disable no-console */ + console.log('=========================================================='); + console.log(moduleSourceMap); + console.log('=========================================================='); + /* eslint-enable no-console */ + expect(normalizeSourceMap(evaluated(module.source))).toMatchSnapshot( 'module (evaluated)' ); From c2e272fe42d554fb60f80f5fef4783d47cc816d5 Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Fri, 30 Nov 2018 16:48:32 -0500 Subject: [PATCH 10/11] Remove use of OriginalSource, it did not contain information on the intended item. --- test/sourceMap-option.test.js | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/test/sourceMap-option.test.js b/test/sourceMap-option.test.js index 44689adc..76d24369 100644 --- a/test/sourceMap-option.test.js +++ b/test/sourceMap-option.test.js @@ -15,26 +15,6 @@ describe('sourceMap option', () => { const { modules } = stats.toJson(); const module = modules.find((m) => m.id === testId); - /** - * Get the actual source map from the OriginalSource object. - * @see {@link https://github.com/webpack/webpack-sources} - * @todo check that the source map exists - * @todo check that the source map has valid and expected property values. - */ - const compilationModules = stats.compilation.modules; - const compilationModule = compilationModules.find((m) => m.id === testId); - const moduleOriginalSource = compilationModule._source; // eslint-disable-line no-underscore-dangle - const moduleSourceMap = moduleOriginalSource.map(); - - /** - * @todo remove `console.log` statments - */ - /* eslint-disable no-console */ - console.log('=========================================================='); - console.log(moduleSourceMap); - console.log('=========================================================='); - /* eslint-enable no-console */ - expect(normalizeSourceMap(evaluated(module.source))).toMatchSnapshot( 'module (evaluated)' ); From 625ffb1f06d029aed9b900dfee2b3c1ce6ad2c6d Mon Sep 17 00:00:00 2001 From: Nick Petruzzelli Date: Fri, 30 Nov 2018 18:20:38 -0500 Subject: [PATCH 11/11] Don't set sourceRoot on source maps if the consuming developer did not set it. Update test snapshots to reflect this. --- lib/loader.js | 1 - test/__snapshots__/sourceMap-option.test.js.snap | 5 ----- 2 files changed, 6 deletions(-) diff --git a/lib/loader.js b/lib/loader.js index 14b902e6..19313ffc 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -38,7 +38,6 @@ module.exports = function loader(content, map) { if (map.sources) { map.sources = map.sources.map((source) => source.replace(/\\/g, '/')); - map.sourceRoot = ''; } } } else { diff --git a/test/__snapshots__/sourceMap-option.test.js.snap b/test/__snapshots__/sourceMap-option.test.js.snap index bac0b795..e2904345 100644 --- a/test/__snapshots__/sourceMap-option.test.js.snap +++ b/test/__snapshots__/sourceMap-option.test.js.snap @@ -100,7 +100,6 @@ Array [ "file": "basic.css", "mappings": "AAAA;EACE,WAAW;CACZ", "names": Array [], - "sourceRoot": "", "sources": Array [ "/replaced/original/path/", ], @@ -133,7 +132,6 @@ Array [ "file": "basic.css", "mappings": "AAAA;EACE,WAAW;CACZ", "names": Array [], - "sourceRoot": "", "sources": Array [ "/replaced/original/path/", ], @@ -166,7 +164,6 @@ Array [ "file": "basic.css", "mappings": "AAAA;ECCE,WAAW;CACZ", "names": Array [], - "sourceRoot": "", "sources": Array [ "/replaced/original/path/", "/replaced/original/path/", @@ -201,7 +198,6 @@ Array [ "file": "basic.css", "mappings": "AAAA;ECCE,WAAW;CACZ", "names": Array [], - "sourceRoot": "", "sources": Array [ "/replaced/original/path/", "/replaced/original/path/", @@ -236,7 +232,6 @@ Array [ "file": "basic.css", "mappings": "AAAA;EACE,WAAW;CACZ", "names": Array [], - "sourceRoot": "", "sources": Array [ "/replaced/original/path/", ],