diff --git a/src/utils.js b/src/utils.js index c6b2b554..b6339c77 100644 --- a/src/utils.js +++ b/src/utils.js @@ -929,8 +929,19 @@ function normalizeSourceMapForRuntime(map, loaderContext) { if (resultMap) { delete resultMap.file; - resultMap.sourceRoot = ""; + /* eslint-disable no-underscore-dangle */ + if ( + loaderContext._compilation && + loaderContext._compilation.options && + loaderContext._compilation.options.devtool && + loaderContext._compilation.options.devtool.includes("nosources") + ) { + /* eslint-enable no-underscore-dangle */ + + delete resultMap.sourcesContent; + } + resultMap.sourceRoot = ""; resultMap.sources = resultMap.sources.map((source) => { // Non-standard syntax from `postcss` if (source.indexOf("<") === 0) { @@ -989,9 +1000,16 @@ function getModuleCode(result, api, replacements, options, loaderContext) { return ""; } - const sourceMapValue = options.sourceMap - ? `,${normalizeSourceMapForRuntime(result.map, loaderContext)}` - : ""; + let sourceMapValue = ""; + + if (options.sourceMap) { + const sourceMap = result.map; + + sourceMapValue = `,${normalizeSourceMapForRuntime( + sourceMap, + loaderContext + )}`; + } let code = JSON.stringify(result.css); diff --git a/test/__snapshots__/sourceMap-option.test.js.snap b/test/__snapshots__/sourceMap-option.test.js.snap index 5f5d4191..92c54a79 100644 --- a/test/__snapshots__/sourceMap-option.test.js.snap +++ b/test/__snapshots__/sourceMap-option.test.js.snap @@ -1,215 +1,64 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`"sourceMap" option false should generate source maps when css was extracted: errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps and do not change "[contenthash]" on different platform: errors 1`] = `Array []`; -exports[`"sourceMap" option false should generate source maps when css was extracted: extracted css 1`] = ` -"/*!*******************************************************************************************!*\\\\ - !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/nested/nested.css ***! - \\\\*******************************************************************************************/ -.nested { - color: blue; -} - -/*!***********************************************************************************!*\\\\ - !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/basic.css ***! - \\\\***********************************************************************************/ -.class { - color: red; -} - -" -`; - -exports[`"sourceMap" option false should generate source maps when css was extracted: warnings 1`] = `Array []`; - -exports[`"sourceMap" option false should not generate source maps when previous loader does not generate source maps: errors 1`] = `Array []`; - -exports[`"sourceMap" option false should not generate source maps when previous loader does not generate source maps: module 1`] = ` -"// Imports -import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\"; -import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; -import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; -var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___); -___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); -// Module -___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]); -// Exports -export default ___CSS_LOADER_EXPORT___; -" -`; - -exports[`"sourceMap" option false should not generate source maps when previous loader does not generate source maps: result 1`] = ` -Array [ - Array [ - "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", - ".nested { - color: blue; -} -", - "", - ], - Array [ - "./source-map/basic.css", - ".class { - color: red; -} -", - "", - ], -] -`; - -exports[`"sourceMap" option false should not generate source maps when previous loader does not generate source maps: warnings 1`] = `Array []`; - -exports[`"sourceMap" option false should not generate source maps when previous loader generates source maps: errors 1`] = `Array []`; - -exports[`"sourceMap" option false should not generate source maps when previous loader generates source maps: module 1`] = ` -"// Imports -import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\"; -import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; -import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; -var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___); -___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); -// Module -___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]); -// Exports -export default ___CSS_LOADER_EXPORT___; -" -`; - -exports[`"sourceMap" option false should not generate source maps when previous loader generates source maps: result 1`] = ` -Array [ - Array [ - "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", - ".nested { - color: blue; -} -", - "", - ], - Array [ - "./source-map/basic.css", - ".class { - color: red; -} -", - "", - ], -] -`; - -exports[`"sourceMap" option false should not generate source maps when previous loader generates source maps: warnings 1`] = `Array []`; - -exports[`"sourceMap" option false should not generate source maps: errors 1`] = `Array []`; - -exports[`"sourceMap" option false should not generate source maps: module 1`] = ` +exports[`"sourceMap" option should generate source maps and do not change "[contenthash]" on different platform: module 1`] = ` "// Imports -import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\"; +import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; -import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; -var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___); +import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].rules[0]!./nested/nested.css\\"; +var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_SOURCEMAP_IMPORT___); ___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); // Module -___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]); +___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\",{\\"version\\":3,\\"sources\\":[\\"webpack://./test/fixtures/source-map/basic.css\\"],\\"names\\":[],\\"mappings\\":\\"AAEA;EACE,UAAU;AACZ\\",\\"sourcesContent\\":[\\"@import \\\\\\"./nested/nested.css\\\\\\";\\\\n\\\\n.class {\\\\n color: red;\\\\n}\\\\n\\"],\\"sourceRoot\\":\\"\\"}]); // Exports export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option false should not generate source maps: result 1`] = ` +exports[`"sourceMap" option should generate source maps and do not change "[contenthash]" on different platform: result 1`] = ` Array [ Array [ - "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", + "./src/index.js??ruleSet[1].rules[0].rules[0]!./test/fixtures/source-map/nested/nested.css", ".nested { color: blue; } ", "", - ], - Array [ - "./source-map/basic.css", - ".class { - color: red; -} -", - "", - ], -] -`; - -exports[`"sourceMap" option false should not generate source maps: warnings 1`] = `Array []`; - -exports[`"sourceMap" option not specified should not generate source maps: errors 1`] = `Array []`; - -exports[`"sourceMap" option not specified should not generate source maps: module 1`] = ` -"// Imports -import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\"; -import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; -import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; -var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___); -___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); -// Module -___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]); -// Exports -export default ___CSS_LOADER_EXPORT___; -" -`; - -exports[`"sourceMap" option not specified should not generate source maps: result 1`] = ` -Array [ - Array [ - "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", - ".nested { + Object { + "mappings": "AAAA;EACE,WAAW;AACb", + "names": Array [], + "sourceRoot": "", + "sources": Array [ + "webpack://./test/fixtures/source-map/nested/nested.css", + ], + "sourcesContent": Array [ + ".nested { color: blue; } ", - "", + ], + "version": 3, + }, ], Array [ - "./source-map/basic.css", + "./test/fixtures/source-map/basic.css", ".class { color: red; } -", - "", - ], -] -`; - -exports[`"sourceMap" option not specified should not generate source maps: warnings 1`] = `Array []`; - -exports[`"sourceMap" option true should generate source maps #2: errors 1`] = `Array []`; - -exports[`"sourceMap" option true should generate source maps #2: module 1`] = ` -"// Imports -import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; -import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; -var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_SOURCEMAP_IMPORT___); -// Module -___CSS_LOADER_EXPORT___.push([module.id, \\".foo {\\\\n color: red;\\\\n}\\\\n\\", \\"\\",{\\"version\\":3,\\"sources\\":[\\"webpack://./source-map/with-query.css\\"],\\"names\\":[],\\"mappings\\":\\"AAAA;EACE,UAAU;AACZ\\",\\"sourcesContent\\":[\\".foo {\\\\n color: red;\\\\n}\\\\n\\"],\\"sourceRoot\\":\\"\\"}]); -// Exports -export default ___CSS_LOADER_EXPORT___; -" -`; - -exports[`"sourceMap" option true should generate source maps #2: result 1`] = ` -Array [ - Array [ - "./source-map/with-query.css?url=false", - ".foo { - color: red; -} ", "", Object { - "mappings": "AAAA;EACE,UAAU;AACZ", + "mappings": "AAEA;EACE,UAAU;AACZ", "names": Array [], "sourceRoot": "", "sources": Array [ - "webpack://./source-map/with-query.css", + "webpack://./test/fixtures/source-map/basic.css", ], "sourcesContent": Array [ - ".foo { + "@import \\"./nested/nested.css\\"; + +.class { color: red; } ", @@ -220,28 +69,28 @@ Array [ ] `; -exports[`"sourceMap" option true should generate source maps #2: warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps and do not change "[contenthash]" on different platform: warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps and do not change "[contenthash]" on different platform: errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps and respect 'nosources' value: errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps and do not change "[contenthash]" on different platform: module 1`] = ` +exports[`"sourceMap" option should generate source maps and respect 'nosources' value: module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; -import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].rules[0]!./nested/nested.css\\"; +import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_SOURCEMAP_IMPORT___); ___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); // Module -___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\",{\\"version\\":3,\\"sources\\":[\\"webpack://./test/fixtures/source-map/basic.css\\"],\\"names\\":[],\\"mappings\\":\\"AAEA;EACE,UAAU;AACZ\\",\\"sourcesContent\\":[\\"@import \\\\\\"./nested/nested.css\\\\\\";\\\\n\\\\n.class {\\\\n color: red;\\\\n}\\\\n\\"],\\"sourceRoot\\":\\"\\"}]); +___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\",{\\"version\\":3,\\"sources\\":[\\"webpack://./source-map/basic.css\\"],\\"names\\":[],\\"mappings\\":\\"AAEA;EACE,UAAU;AACZ\\",\\"sourceRoot\\":\\"\\"}]); // Exports export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps and do not change "[contenthash]" on different platform: result 1`] = ` +exports[`"sourceMap" option should generate source maps and respect 'nosources' value: result 1`] = ` Array [ Array [ - "./src/index.js??ruleSet[1].rules[0].rules[0]!./test/fixtures/source-map/nested/nested.css", + "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", ".nested { color: blue; } @@ -252,19 +101,13 @@ Array [ "names": Array [], "sourceRoot": "", "sources": Array [ - "webpack://./test/fixtures/source-map/nested/nested.css", - ], - "sourcesContent": Array [ - ".nested { - color: blue; -} -", + "webpack://./source-map/nested/nested.css", ], "version": 3, }, ], Array [ - "./test/fixtures/source-map/basic.css", + "./source-map/basic.css", ".class { color: red; } @@ -275,15 +118,7 @@ Array [ "names": Array [], "sourceRoot": "", "sources": Array [ - "webpack://./test/fixtures/source-map/basic.css", - ], - "sourcesContent": Array [ - "@import \\"./nested/nested.css\\"; - -.class { - color: red; -} -", + "webpack://./source-map/basic.css", ], "version": 3, }, @@ -291,11 +126,11 @@ Array [ ] `; -exports[`"sourceMap" option true should generate source maps and do not change "[contenthash]" on different platform: warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps and respect 'nosources' value: warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when css was extracted and do not change "[contenthash]" on different platform: errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when css was extracted and do not change "[contenthash]" on different platform: errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when css was extracted and do not change "[contenthash]" on different platform: extracted css 1`] = ` +exports[`"sourceMap" option should generate source maps when css was extracted and do not change "[contenthash]" on different platform: extracted css 1`] = ` "/*!*****************************************************************************************************!*\\\\ !*** css ./src/index.js??ruleSet[1].rules[0].rules[1]!./test/fixtures/source-map/nested/nested.css ***! \\\\*****************************************************************************************************/ @@ -314,7 +149,7 @@ exports[`"sourceMap" option true should generate source maps when css was extrac /*# sourceMappingURL=main.xxxxxxxxxxxxxxxxxxxx.css.map*/" `; -exports[`"sourceMap" option true should generate source maps when css was extracted and do not change "[contenthash]" on different platform: source map 1`] = ` +exports[`"sourceMap" option should generate source maps when css was extracted and do not change "[contenthash]" on different platform: source map 1`] = ` Object { "file": "main.xxxxxxxxxxxxxxxxxxxx.css", "mappings": ";;;AAAA;EACE,WAAW;AACb;;;;;ACAA;EACE,UAAU;AACZ", @@ -340,11 +175,132 @@ Object { } `; -exports[`"sourceMap" option true should generate source maps when css was extracted and do not change "[contenthash]" on different platform: warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when css was extracted and do not change "[contenthash]" on different platform: warnings 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'hidden-nosources-source-map': errors 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'hidden-nosources-source-map': extracted css 1`] = ` +"/*!*******************************************************************************************!*\\\\ + !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/nested/nested.css ***! + \\\\*******************************************************************************************/ +.nested { + color: blue; +} + +/*!***********************************************************************************!*\\\\ + !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/basic.css ***! + \\\\***********************************************************************************/ +.class { + color: red; +} + +" +`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'hidden-nosources-source-map': source map 1`] = ` +Object { + "file": "main.css", + "mappings": ";;;AAAA;EACE,WAAW;AACb;;;;;ACAA;EACE,UAAU;AACZ", + "names": Array [], + "sourceRoot": "", + "sources": Array [ + "webpack:///./source-map/nested/nested.css", + "webpack:///./source-map/basic.css", + ], + "version": 3, +} +`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'hidden-nosources-source-map': warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when css was extracted: errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'hidden-source-map': errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when css was extracted: extracted css 1`] = ` +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'hidden-source-map': extracted css 1`] = ` +"/*!*******************************************************************************************!*\\\\ + !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/nested/nested.css ***! + \\\\*******************************************************************************************/ +.nested { + color: blue; +} + +/*!***********************************************************************************!*\\\\ + !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/basic.css ***! + \\\\***********************************************************************************/ +.class { + color: red; +} + +" +`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'hidden-source-map': source map 1`] = ` +Object { + "file": "main.css", + "mappings": ";;;AAAA;EACE,WAAW;AACb;;;;;ACAA;EACE,UAAU;AACZ", + "names": Array [], + "sourceRoot": "", + "sources": Array [ + "webpack:///./source-map/nested/nested.css", + "webpack:///./source-map/basic.css", + ], + "sourcesContent": Array [ + ".nested { + color: blue; +} +", + "@import \\"./nested/nested.css\\"; + +.class { + color: red; +} +", + ], + "version": 3, +} +`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'hidden-source-map': warnings 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'nosources-source-map': errors 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'nosources-source-map': extracted css 1`] = ` +"/*!*******************************************************************************************!*\\\\ + !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/nested/nested.css ***! + \\\\*******************************************************************************************/ +.nested { + color: blue; +} + +/*!***********************************************************************************!*\\\\ + !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/basic.css ***! + \\\\***********************************************************************************/ +.class { + color: red; +} + + +/*# sourceMappingURL=main.css.map*/" +`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'nosources-source-map': source map 1`] = ` +Object { + "file": "main.css", + "mappings": ";;;AAAA;EACE,WAAW;AACb;;;;;ACAA;EACE,UAAU;AACZ", + "names": Array [], + "sourceRoot": "", + "sources": Array [ + "webpack:///./source-map/nested/nested.css", + "webpack:///./source-map/basic.css", + ], + "version": 3, +} +`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'nosources-source-map': warnings 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'source-map': errors 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'source-map': extracted css 1`] = ` "/*!*******************************************************************************************!*\\\\ !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/nested/nested.css ***! \\\\*******************************************************************************************/ @@ -363,7 +319,7 @@ exports[`"sourceMap" option true should generate source maps when css was extrac /*# sourceMappingURL=main.css.map*/" `; -exports[`"sourceMap" option true should generate source maps when css was extracted: source map 1`] = ` +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'source-map': source map 1`] = ` Object { "file": "main.css", "mappings": ";;;AAAA;EACE,WAAW;AACb;;;;;ACAA;EACE,UAAU;AACZ", @@ -389,11 +345,33 @@ Object { } `; -exports[`"sourceMap" option true should generate source maps when css was extracted: warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when css was extracted when the 'devtool' property is 'source-map': warnings 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when css was extracted: errors 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when css was extracted: extracted css 1`] = ` +"/*!*******************************************************************************************!*\\\\ + !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/nested/nested.css ***! + \\\\*******************************************************************************************/ +.nested { + color: blue; +} + +/*!***********************************************************************************!*\\\\ + !*** css ../../src/index.js??ruleSet[1].rules[0].rules[1]!./source-map/basic.css ***! + \\\\***********************************************************************************/ +.class { + color: red; +} + +" +`; + +exports[`"sourceMap" option should generate source maps when css was extracted: warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader does not generate source maps: errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader does not generate source maps: errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader does not generate source maps: module 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader does not generate source maps: module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -407,7 +385,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps when previous loader does not generate source maps: result 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader does not generate source maps: result 1`] = ` Array [ Array [ "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", @@ -460,11 +438,11 @@ Array [ ] `; -exports[`"sourceMap" option true should generate source maps when previous loader does not generate source maps: warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader does not generate source maps: warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates different source in source maps: errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates different source in source maps: errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates different source in source maps: module 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates different source in source maps: module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -478,7 +456,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps when previous loader generates different source in source maps: result 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates different source in source maps: result 1`] = ` Array [ Array [ "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", @@ -531,11 +509,11 @@ Array [ ] `; -exports[`"sourceMap" option true should generate source maps when previous loader generates different source in source maps: warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates different source in source maps: warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("less-loader"): errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("less-loader"): errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("less-loader"): module 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("less-loader"): module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -547,7 +525,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("less-loader"): result 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("less-loader"): result 1`] = ` Array [ Array [ "./source-map/base.less", @@ -580,11 +558,11 @@ body { ] `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("less-loader"): warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("less-loader"): warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("postcss-loader"): errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("postcss-loader"): errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("postcss-loader"): module 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("postcss-loader"): module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -598,7 +576,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("postcss-loader"): result 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("postcss-loader"): result 1`] = ` Array [ Array [ "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.postcss.css", @@ -721,11 +699,11 @@ a { ] `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("postcss-loader"): warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("postcss-loader"): warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("sass-loader"): errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("sass-loader"): errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("sass-loader"): module 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("sass-loader"): module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -737,7 +715,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("sass-loader"): result 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("sass-loader"): result 1`] = ` Array [ Array [ "./source-map/basic.scss", @@ -769,11 +747,11 @@ body { ] `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("sass-loader"): warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("sass-loader"): warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("stylus-loader"): errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("stylus-loader"): errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("stylus-loader"): module 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("stylus-loader"): module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -785,7 +763,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("stylus-loader"): result 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("stylus-loader"): result 1`] = ` Array [ Array [ "./source-map/base.styl", @@ -819,11 +797,11 @@ body #logo { ] `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps ("stylus-loader"): warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps ("stylus-loader"): warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps with "sourceRoot": errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps with "sourceRoot": errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps with "sourceRoot": module 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps with "sourceRoot": module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -837,7 +815,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps with "sourceRoot": result 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps with "sourceRoot": result 1`] = ` Array [ Array [ "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", @@ -890,11 +868,11 @@ Array [ ] `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps with "sourceRoot": warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps with "sourceRoot": warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps without "sourceRoot": errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps without "sourceRoot": errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps without "sourceRoot": module 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps without "sourceRoot": module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -908,7 +886,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps without "sourceRoot": result 1`] = ` +exports[`"sourceMap" option should generate source maps when previous loader generates source maps without "sourceRoot": result 1`] = ` Array [ Array [ "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", @@ -961,11 +939,55 @@ Array [ ] `; -exports[`"sourceMap" option true should generate source maps when previous loader generates source maps without "sourceRoot": warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when previous loader generates source maps without "sourceRoot": warnings 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when the 'sourceMap' option is 'true' #2: errors 1`] = `Array []`; + +exports[`"sourceMap" option should generate source maps when the 'sourceMap' option is 'true' #2: module 1`] = ` +"// Imports +import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; +import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; +var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_SOURCEMAP_IMPORT___); +// Module +___CSS_LOADER_EXPORT___.push([module.id, \\".foo {\\\\n color: red;\\\\n}\\\\n\\", \\"\\",{\\"version\\":3,\\"sources\\":[\\"webpack://./source-map/with-query.css\\"],\\"names\\":[],\\"mappings\\":\\"AAAA;EACE,UAAU;AACZ\\",\\"sourcesContent\\":[\\".foo {\\\\n color: red;\\\\n}\\\\n\\"],\\"sourceRoot\\":\\"\\"}]); +// Exports +export default ___CSS_LOADER_EXPORT___; +" +`; + +exports[`"sourceMap" option should generate source maps when the 'sourceMap' option is 'true' #2: result 1`] = ` +Array [ + Array [ + "./source-map/with-query.css?url=false", + ".foo { + color: red; +} +", + "", + Object { + "mappings": "AAAA;EACE,UAAU;AACZ", + "names": Array [], + "sourceRoot": "", + "sources": Array [ + "webpack://./source-map/with-query.css", + ], + "sourcesContent": Array [ + ".foo { + color: red; +} +", + ], + "version": 3, + }, + ], +] +`; + +exports[`"sourceMap" option should generate source maps when the 'sourceMap' option is 'true' #2: warnings 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps: errors 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when the 'sourceMap' option is 'true': errors 1`] = `Array []`; -exports[`"sourceMap" option true should generate source maps: module 1`] = ` +exports[`"sourceMap" option should generate source maps when the 'sourceMap' option is 'true': module 1`] = ` "// Imports import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/sourceMaps.js\\"; import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; @@ -979,7 +1001,7 @@ export default ___CSS_LOADER_EXPORT___; " `; -exports[`"sourceMap" option true should generate source maps: result 1`] = ` +exports[`"sourceMap" option should generate source maps when the 'sourceMap' option is 'true': result 1`] = ` Array [ Array [ "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", @@ -1032,4 +1054,160 @@ Array [ ] `; -exports[`"sourceMap" option true should generate source maps: warnings 1`] = `Array []`; +exports[`"sourceMap" option should generate source maps when the 'sourceMap' option is 'true': warnings 1`] = `Array []`; + +exports[`"sourceMap" option should not generate source maps by default when the 'devtool' option is false: errors 1`] = `Array []`; + +exports[`"sourceMap" option should not generate source maps by default when the 'devtool' option is false: module 1`] = ` +"// Imports +import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\"; +import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; +import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; +var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___); +___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); +// Module +___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]); +// Exports +export default ___CSS_LOADER_EXPORT___; +" +`; + +exports[`"sourceMap" option should not generate source maps by default when the 'devtool' option is false: result 1`] = ` +Array [ + Array [ + "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", + ".nested { + color: blue; +} +", + "", + ], + Array [ + "./source-map/basic.css", + ".class { + color: red; +} +", + "", + ], +] +`; + +exports[`"sourceMap" option should not generate source maps by default when the 'devtool' option is false: warnings 1`] = `Array []`; + +exports[`"sourceMap" option should not generate source maps when 'false' value is used: errors 1`] = `Array []`; + +exports[`"sourceMap" option should not generate source maps when 'false' value is used: module 1`] = ` +"// Imports +import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\"; +import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; +import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; +var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___); +___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); +// Module +___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]); +// Exports +export default ___CSS_LOADER_EXPORT___; +" +`; + +exports[`"sourceMap" option should not generate source maps when 'false' value is used: result 1`] = ` +Array [ + Array [ + "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", + ".nested { + color: blue; +} +", + "", + ], + Array [ + "./source-map/basic.css", + ".class { + color: red; +} +", + "", + ], +] +`; + +exports[`"sourceMap" option should not generate source maps when 'false' value is used: warnings 1`] = `Array []`; + +exports[`"sourceMap" option should not generate source maps when previous loader does not generate source maps: errors 1`] = `Array []`; + +exports[`"sourceMap" option should not generate source maps when previous loader does not generate source maps: module 1`] = ` +"// Imports +import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\"; +import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; +import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; +var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___); +___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); +// Module +___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]); +// Exports +export default ___CSS_LOADER_EXPORT___; +" +`; + +exports[`"sourceMap" option should not generate source maps when previous loader does not generate source maps: result 1`] = ` +Array [ + Array [ + "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", + ".nested { + color: blue; +} +", + "", + ], + Array [ + "./source-map/basic.css", + ".class { + color: red; +} +", + "", + ], +] +`; + +exports[`"sourceMap" option should not generate source maps when previous loader does not generate source maps: warnings 1`] = `Array []`; + +exports[`"sourceMap" option should not generate source maps when previous loader generates source maps: errors 1`] = `Array []`; + +exports[`"sourceMap" option should not generate source maps when previous loader generates source maps: module 1`] = ` +"// Imports +import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from \\"../../../src/runtime/noSourceMaps.js\\"; +import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\"; +import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./nested/nested.css\\"; +var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___); +___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___); +// Module +___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]); +// Exports +export default ___CSS_LOADER_EXPORT___; +" +`; + +exports[`"sourceMap" option should not generate source maps when previous loader generates source maps: result 1`] = ` +Array [ + Array [ + "../../src/index.js??ruleSet[1].rules[0].use[0]!./source-map/nested/nested.css", + ".nested { + color: blue; +} +", + "", + ], + Array [ + "./source-map/basic.css", + ".class { + color: red; +} +", + "", + ], +] +`; + +exports[`"sourceMap" option should not generate source maps when previous loader generates source maps: warnings 1`] = `Array []`; diff --git a/test/sourceMap-option.test.js b/test/sourceMap-option.test.js index 89a26093..8776db07 100644 --- a/test/sourceMap-option.test.js +++ b/test/sourceMap-option.test.js @@ -16,768 +16,923 @@ import { jest.setTimeout(10000); describe('"sourceMap" option', () => { - describe("not specified", () => { - it("should not generate source maps", async () => { - const compiler = getCompiler("./source-map/basic.js"); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + it("should not generate source maps by default when the 'devtool' option is false", async () => { + const compiler = getCompiler("./source-map/basic.js"); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); - describe("true", () => { - it("should generate source maps", async () => { - const compiler = getCompiler("./source-map/basic.js", { - sourceMap: true, - }); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); + it("should generate source maps when the 'sourceMap' option is 'true'", async () => { + const compiler = getCompiler("./source-map/basic.js", { + sourceMap: true, }); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it("should generate source maps #2", async () => { - const compiler = getCompiler("./source-map/with-query.js", { - sourceMap: true, - }); - const stats = await compile(compiler); - - expect( - getModuleSource("./source-map/with-query.css?url=false", stats) - ).toMatchSnapshot("module"); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); + it("should generate source maps when the 'sourceMap' option is 'true' #2", async () => { + const compiler = getCompiler("./source-map/with-query.js", { + sourceMap: true, }); + const stats = await compile(compiler); + + expect( + getModuleSource("./source-map/with-query.css?url=false", stats) + ).toMatchSnapshot("module"); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it("should generate source maps when previous loader does not generate source maps", async () => { - const compiler = getCompiler( - "./source-map/basic.js", - {}, - { - module: { - rules: [ - { - test: /\.css$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: true }, - }, - { - loader: path.resolve( - __dirname, - "./fixtures/source-map-loader.js" - ), - options: { - sourceMap: null, - }, - }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + it("should generate source maps and respect 'nosources' value", async () => { + const compiler = getCompiler( + "./source-map/basic.js", + {}, + { + devtool: "nosources-source-map", + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it('should generate source maps when previous loader generates source maps without "sourceRoot"', async () => { - const absolutePath = path.resolve( - __dirname, - "fixtures", - "source-map", - "basic.css" - ); - - const compiler = getCompiler( - "./source-map/basic.js", - {}, - { - module: { - rules: [ - { - test: /\.css$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: true }, - }, - { - loader: path.resolve( - __dirname, - "./fixtures/source-map-loader.js" - ), - options: { - sourceMap: JSON.stringify({ - version: 3, - sources: [absolutePath], - names: [], - mappings: "AAAA,6BAA6B;;AAE7B;EACE,UAAU;AACZ", - file: absolutePath, - sourcesContent: [ - '@import "./nested/nested.css";\n\n.class {\n color: red;\n}\n', - ], - }), - }, + it("should generate source maps when previous loader does not generate source maps", async () => { + const compiler = getCompiler( + "./source-map/basic.js", + {}, + { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + { + loader: path.resolve( + __dirname, + "./fixtures/source-map-loader.js" + ), + options: { + sourceMap: null, }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it("should generate source maps when previous loader generates different source in source maps", async () => { - const absoluteSourceRoot = path.resolve( - __dirname, - "fixtures", - "source-map" - ); - const absolutePath = path.resolve(absoluteSourceRoot, "basic-1.css"); - const relativePath = path.relative( - absoluteSourceRoot, - path.resolve(__dirname, "fixtures", "source-map", "basic-2.css") - ); - - const compiler = getCompiler( - "./source-map/basic.js", - {}, - { - module: { - rules: [ - { - test: /\.css$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: true }, - }, - { - loader: path.resolve( - __dirname, - "./fixtures/source-map-loader.js" - ), - options: { - sourceMap: JSON.stringify({ - version: 3, - sourceRoot: absoluteSourceRoot, - sources: [ - // Absolute path - absolutePath, - // Relative path - relativePath, - // Absolute URL - "https://example.com/foo.css", - // Scheme-relative URL, - "//example.com/foo.css", - // Non-standard postcss syntax - ` { + const absolutePath = path.resolve( + __dirname, + "fixtures", + "source-map", + "basic.css" + ); + + const compiler = getCompiler( + "./source-map/basic.js", + {}, + { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + { + loader: path.resolve( + __dirname, + "./fixtures/source-map-loader.js" + ), + options: { + sourceMap: JSON.stringify({ + version: 3, + sources: [absolutePath], + names: [], + mappings: "AAAA,6BAA6B;;AAE7B;EACE,UAAU;AACZ", + file: absolutePath, + sourcesContent: [ + '@import "./nested/nested.css";\n\n.class {\n color: red;\n}\n', + ], + }), }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it('should generate source maps when previous loader generates source maps with "sourceRoot"', async () => { - const absoluteSourceRoot = path.resolve( - __dirname, - "fixtures", - "source-map" - ); - const compiler = getCompiler( - "./source-map/basic.js", - {}, - { - module: { - rules: [ - { - test: /\.css$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: true }, + it("should generate source maps when previous loader generates different source in source maps", async () => { + const absoluteSourceRoot = path.resolve( + __dirname, + "fixtures", + "source-map" + ); + const absolutePath = path.resolve(absoluteSourceRoot, "basic-1.css"); + const relativePath = path.relative( + absoluteSourceRoot, + path.resolve(__dirname, "fixtures", "source-map", "basic-2.css") + ); + + const compiler = getCompiler( + "./source-map/basic.js", + {}, + { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + { + loader: path.resolve( + __dirname, + "./fixtures/source-map-loader.js" + ), + options: { + sourceMap: JSON.stringify({ + version: 3, + sourceRoot: absoluteSourceRoot, + sources: [ + // Absolute path + absolutePath, + // Relative path + relativePath, + // Absolute URL + "https://example.com/foo.css", + // Scheme-relative URL, + "//example.com/foo.css", + // Non-standard postcss syntax + ` { + const absoluteSourceRoot = path.resolve( + __dirname, + "fixtures", + "source-map" + ); + const compiler = getCompiler( + "./source-map/basic.js", + {}, + { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + { + loader: path.resolve( + __dirname, + "./fixtures/source-map-loader.js" + ), + options: { + sourceMap: JSON.stringify({ + foo: "bar", + version: 3, + sources: ["basic.css"], + sourceRoot: absoluteSourceRoot, + names: [], + mappings: "AAAA,6BAA6B;;AAE7B;EACE,UAAU;AACZ", + file: "basic.css", + sourcesContent: [ + '@import "./nested/nested.css";\n\n.class {\n color: red;\n}\n', + ], + }), }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it('should generate source maps when previous loader generates source maps ("postcss-loader")', async () => { - const compiler = getCompiler( - "./source-map/basic-postcss.js", - {}, - { - module: { - rules: [ - { - test: /\.css$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { - sourceMap: true, - }, + it('should generate source maps when previous loader generates source maps ("postcss-loader")', async () => { + const compiler = getCompiler( + "./source-map/basic-postcss.js", + {}, + { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { + sourceMap: true, }, - { - loader: "postcss-loader", - options: { - postcssOptions: { - plugins: [postcssPresetEnv({ stage: 0 })], - }, - sourceMap: true, + }, + { + loader: "postcss-loader", + options: { + postcssOptions: { + plugins: [postcssPresetEnv({ stage: 0 })], }, + sourceMap: true, }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect( - getModuleSource("./source-map/basic.postcss.css", stats) - ).toMatchSnapshot("module"); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect( + getModuleSource("./source-map/basic.postcss.css", stats) + ).toMatchSnapshot("module"); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it('should generate source maps when previous loader generates source maps ("sass-loader")', async () => { - const compiler = getCompiler( - "./source-map/basic-scss.js", - {}, - { - module: { - rules: [ - { - test: /\.s[ca]ss$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { - sourceMap: true, - }, + it('should generate source maps when previous loader generates source maps ("sass-loader")', async () => { + const compiler = getCompiler( + "./source-map/basic-scss.js", + {}, + { + module: { + rules: [ + { + test: /\.s[ca]ss$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { + sourceMap: true, }, - { - loader: "sass-loader", - options: { - // eslint-disable-next-line global-require - implementation: require("sass"), - sourceMap: true, - }, + }, + { + loader: "sass-loader", + options: { + // eslint-disable-next-line global-require + implementation: require("sass"), + sourceMap: true, }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.scss", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.scss", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it('should generate source maps when previous loader generates source maps ("less-loader")', async () => { - const compiler = getCompiler( - "./source-map/basic-less.js", - {}, - { - module: { - rules: [ - { - test: /\.less$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { - sourceMap: true, - }, + it('should generate source maps when previous loader generates source maps ("less-loader")', async () => { + const compiler = getCompiler( + "./source-map/basic-less.js", + {}, + { + module: { + rules: [ + { + test: /\.less$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { + sourceMap: true, }, - { - loader: "less-loader", - options: { - sourceMap: true, - }, + }, + { + loader: "less-loader", + options: { + sourceMap: true, }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/base.less", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/base.less", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it('should generate source maps when previous loader generates source maps ("stylus-loader")', async () => { - const compiler = getCompiler( - "./source-map/basic-styl.js", - {}, - { - module: { - rules: [ - { - test: /\.styl$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { - sourceMap: true, - }, + it('should generate source maps when previous loader generates source maps ("stylus-loader")', async () => { + const compiler = getCompiler( + "./source-map/basic-styl.js", + {}, + { + module: { + rules: [ + { + test: /\.styl$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { + sourceMap: true, }, - { - loader: "stylus-loader", - options: { - sourceMap: true, - }, + }, + { + loader: "stylus-loader", + options: { + sourceMap: true, }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/base.styl", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/base.styl", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it('should generate source maps and do not change "[contenthash]" on different platform', async () => { - const compiler = getCompiler( - "./source-map/basic.js", - {}, - { - devtool: "source-map", - // webpack@4 has bug - // It uses readableIdentifier to generate the sources, which uses the RequestShortener, - // which has some problems with paths that are 2 folders above the context - context: path.resolve(__dirname, ".."), - output: { - path: path.resolve(__dirname, "./outputs"), - filename: "[name].[contenthash].bundle.js", - chunkFilename: "[name].[contenthash].chunk.js", - publicPath: "/webpack/public/path/", - }, - module: { - rules: [ - { - test: /\.css$/i, - rules: [ - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: true }, - }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - const chunkName = Object.keys(stats.compilation.assets).find( - (assetName) => /\.js$/.test(assetName) - ); - - expect(chunkName).toBe("main.25338c5bc4249008fa47.bundle.js"); - expect( - getModuleSource("fixtures/source-map/basic.css", stats) - ).toMatchSnapshot("module"); - expect(getExecutedCode(chunkName, compiler, stats)).toMatchSnapshot( - "result" - ); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + it('should generate source maps and do not change "[contenthash]" on different platform', async () => { + const compiler = getCompiler( + "./source-map/basic.js", + {}, + { + devtool: "source-map", + // webpack@4 has bug + // It uses readableIdentifier to generate the sources, which uses the RequestShortener, + // which has some problems with paths that are 2 folders above the context + context: path.resolve(__dirname, ".."), + output: { + path: path.resolve(__dirname, "./outputs"), + filename: "[name].[contenthash].bundle.js", + chunkFilename: "[name].[contenthash].chunk.js", + publicPath: "/webpack/public/path/", + }, + module: { + rules: [ + { + test: /\.css$/i, + rules: [ + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + const chunkName = Object.keys(stats.compilation.assets).find((assetName) => + /\.js$/.test(assetName) + ); + + expect(chunkName).toBe("main.25338c5bc4249008fa47.bundle.js"); + expect( + getModuleSource("fixtures/source-map/basic.css", stats) + ).toMatchSnapshot("module"); + expect(getExecutedCode(chunkName, compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it("should generate source maps when css was extracted", async () => { - const compiler = getCompiler( - "./source-map/extract.js", - {}, - { - devtool: "source-map", - output: { - path: path.resolve(__dirname, "../outputs"), - filename: "[name].bundle.js", - chunkFilename: "[name].chunk.js", - publicPath: "/webpack/public/path/", - }, - plugins: [ - new MiniCssExtractPlugin({ - filename: "[name].css", - }), + it("should generate source maps when css was extracted when the 'devtool' property is 'source-map'", async () => { + const compiler = getCompiler( + "./source-map/extract.js", + {}, + { + devtool: "source-map", + output: { + path: path.resolve(__dirname, "../outputs"), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", + publicPath: "/webpack/public/path/", + }, + plugins: [ + new MiniCssExtractPlugin({ + filename: "[name].css", + }), + ], + module: { + rules: [ + { + test: /\.css$/i, + rules: [ + { + loader: MiniCssExtractPlugin.loader, + }, + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + ], + }, ], - module: { - rules: [ - { - test: /\.css$/i, - rules: [ - { - loader: MiniCssExtractPlugin.loader, - }, - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: true }, - }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(readAsset("main.css", compiler, stats)).toMatchSnapshot( - "extracted css" - ); - expect( - JSON.parse(readAsset("main.css.map", compiler, stats)) - ).toMatchSnapshot("source map"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + } + ); + const stats = await compile(compiler); + + expect(readAsset("main.css", compiler, stats)).toMatchSnapshot( + "extracted css" + ); + expect( + JSON.parse(readAsset("main.css.map", compiler, stats)) + ).toMatchSnapshot("source map"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it('should generate source maps when css was extracted and do not change "[contenthash]" on different platform', async () => { - const compiler = getCompiler( - "./source-map/extract.js", - {}, - { - devtool: "source-map", - // webpack@4 has bug - // It uses readableIdentifier to generate the sources, which uses the RequestShortener, - // which has some problems with paths that are 2 folders above the context - context: path.resolve(__dirname, ".."), - output: { - path: path.resolve(__dirname, "../outputs"), - filename: "[name].bundle.js", - chunkFilename: "[name].chunk.js", - publicPath: "/webpack/public/path/", - }, - plugins: [ - new MiniCssExtractPlugin({ - filename: "[name].[contenthash].css", - }), + it("should generate source maps when css was extracted when the 'devtool' property is 'nosources-source-map'", async () => { + const compiler = getCompiler( + "./source-map/extract.js", + {}, + { + devtool: "nosources-source-map", + output: { + path: path.resolve(__dirname, "../outputs"), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", + publicPath: "/webpack/public/path/", + }, + plugins: [ + new MiniCssExtractPlugin({ + filename: "[name].css", + }), + ], + module: { + rules: [ + { + test: /\.css$/i, + rules: [ + { + loader: MiniCssExtractPlugin.loader, + }, + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + ], + }, ], - module: { - rules: [ - { - test: /\.css$/i, - rules: [ - { - loader: MiniCssExtractPlugin.loader, - }, - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: true }, - }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - const chunkName = Object.keys(stats.compilation.assets).find( - (assetName) => /\.css$/.test(assetName) - ); - - const extractedCSS = readAsset(chunkName, compiler, stats); - - expect(chunkName).toBe("main.166835fe802ce402ea56.css"); - - expect( - extractedCSS.replace( - /=(.+?)\..+?\.css\.map/, - "=$1.xxxxxxxxxxxxxxxxxxxx.css.map" - ) - ).toMatchSnapshot("extracted css"); - - const sourceMap = JSON.parse( - readAsset(`${chunkName}.map`, compiler, stats) - ); - - sourceMap.file = sourceMap.file.replace( - /^(.+?)\..+?\.css$/, - "$1.xxxxxxxxxxxxxxxxxxxx.css" - ); - sourceMap.sources = sourceMap.sources.map((source) => - source.replace("css-loader", "") - ); - - expect(sourceMap).toMatchSnapshot("source map"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + } + ); + const stats = await compile(compiler); + + expect(readAsset("main.css", compiler, stats)).toMatchSnapshot( + "extracted css" + ); + expect( + JSON.parse(readAsset("main.css.map", compiler, stats)) + ).toMatchSnapshot("source map"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); - describe("false", () => { - it("should not generate source maps", async () => { - const compiler = getCompiler("./source-map/basic.js", { - sourceMap: false, - }); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + it("should generate source maps when css was extracted when the 'devtool' property is 'hidden-nosources-source-map'", async () => { + const compiler = getCompiler( + "./source-map/extract.js", + {}, + { + devtool: "hidden-nosources-source-map", + output: { + path: path.resolve(__dirname, "../outputs"), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", + publicPath: "/webpack/public/path/", + }, + plugins: [ + new MiniCssExtractPlugin({ + filename: "[name].css", + }), + ], + module: { + rules: [ + { + test: /\.css$/i, + rules: [ + { + loader: MiniCssExtractPlugin.loader, + }, + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(readAsset("main.css", compiler, stats)).toMatchSnapshot( + "extracted css" + ); + expect( + JSON.parse(readAsset("main.css.map", compiler, stats)) + ).toMatchSnapshot("source map"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it("should not generate source maps when previous loader does not generate source maps", async () => { - const compiler = getCompiler( - "./source-map/basic.js", - {}, - { - module: { - rules: [ - { - test: /\.css$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: false }, - }, - { - loader: path.resolve( - __dirname, - "./fixtures/source-map-loader.js" - ), - options: { - // eslint-disable-next-line no-undefined - sourceMap: undefined, - }, - }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); + it("should generate source maps when css was extracted when the 'devtool' property is 'hidden-source-map'", async () => { + const compiler = getCompiler( + "./source-map/extract.js", + {}, + { + devtool: "hidden-source-map", + output: { + path: path.resolve(__dirname, "../outputs"), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", + publicPath: "/webpack/public/path/", + }, + plugins: [ + new MiniCssExtractPlugin({ + filename: "[name].css", + }), + ], + module: { + rules: [ + { + test: /\.css$/i, + rules: [ + { + loader: MiniCssExtractPlugin.loader, + }, + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(readAsset("main.css", compiler, stats)).toMatchSnapshot( + "extracted css" + ); + expect( + JSON.parse(readAsset("main.css.map", compiler, stats)) + ).toMatchSnapshot("source map"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + + it('should generate source maps when css was extracted and do not change "[contenthash]" on different platform', async () => { + const compiler = getCompiler( + "./source-map/extract.js", + {}, + { + devtool: "source-map", + // webpack@4 has bug + // It uses readableIdentifier to generate the sources, which uses the RequestShortener, + // which has some problems with paths that are 2 folders above the context + context: path.resolve(__dirname, ".."), + output: { + path: path.resolve(__dirname, "../outputs"), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", + publicPath: "/webpack/public/path/", + }, + plugins: [ + new MiniCssExtractPlugin({ + filename: "[name].[contenthash].css", + }), + ], + module: { + rules: [ + { + test: /\.css$/i, + rules: [ + { + loader: MiniCssExtractPlugin.loader, + }, + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: true }, + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + const chunkName = Object.keys(stats.compilation.assets).find((assetName) => + /\.css$/.test(assetName) + ); + + const extractedCSS = readAsset(chunkName, compiler, stats); + + expect(chunkName).toBe("main.166835fe802ce402ea56.css"); + + expect( + extractedCSS.replace( + /=(.+?)\..+?\.css\.map/, + "=$1.xxxxxxxxxxxxxxxxxxxx.css.map" + ) + ).toMatchSnapshot("extracted css"); + + const sourceMap = JSON.parse( + readAsset(`${chunkName}.map`, compiler, stats) + ); + + sourceMap.file = sourceMap.file.replace( + /^(.+?)\..+?\.css$/, + "$1.xxxxxxxxxxxxxxxxxxxx.css" + ); + sourceMap.sources = sourceMap.sources.map((source) => + source.replace("css-loader", "") + ); + + expect(sourceMap).toMatchSnapshot("source map"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + + it("should not generate source maps when 'false' value is used", async () => { + const compiler = getCompiler("./source-map/basic.js", { + sourceMap: false, }); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it("should not generate source maps when previous loader generates source maps", async () => { - const compiler = getCompiler( - "./source-map/basic.js", - {}, - { - module: { - rules: [ - { - test: /\.css$/i, - use: [ - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: false }, + it("should not generate source maps when previous loader does not generate source maps", async () => { + const compiler = getCompiler( + "./source-map/basic.js", + {}, + { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: false }, + }, + { + loader: path.resolve( + __dirname, + "./fixtures/source-map-loader.js" + ), + options: { + // eslint-disable-next-line no-undefined + sourceMap: undefined, }, - { - loader: path.resolve( - __dirname, - "./fixtures/source-map-loader.js" - ), - options: { - sourceMap: JSON.stringify({ - foo: "bar", - version: 3, - sources: ["basic.css"], - names: [], - mappings: "AAAA;EACE,UAAU;AACZ", - file: "basic.css", - sourcesContent: [".class {\n color: red;\n}\n"], - }), - }, + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + + it("should not generate source maps when previous loader generates source maps", async () => { + const compiler = getCompiler( + "./source-map/basic.js", + {}, + { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: false }, + }, + { + loader: path.resolve( + __dirname, + "./fixtures/source-map-loader.js" + ), + options: { + sourceMap: JSON.stringify({ + foo: "bar", + version: 3, + sources: ["basic.css"], + names: [], + mappings: "AAAA;EACE,UAAU;AACZ", + file: "basic.css", + sourcesContent: [".class {\n color: red;\n}\n"], + }), }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( - "module" - ); - expect( - getExecutedCode("main.bundle.js", compiler, stats) - ).toMatchSnapshot("result"); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + ], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource("./source-map/basic.css", stats)).toMatchSnapshot( + "module" + ); + expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot( + "result" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); - it("should generate source maps when css was extracted", async () => { - const compiler = getCompiler( - "./source-map/extract.js", - {}, - { - output: { - path: path.resolve(__dirname, "../outputs"), - filename: "[name].bundle.js", - chunkFilename: "[name].chunk.js", - publicPath: "/webpack/public/path/", - }, - plugins: [ - new MiniCssExtractPlugin({ - filename: "[name].css", - }), + it("should generate source maps when css was extracted", async () => { + const compiler = getCompiler( + "./source-map/extract.js", + {}, + { + output: { + path: path.resolve(__dirname, "../outputs"), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", + publicPath: "/webpack/public/path/", + }, + plugins: [ + new MiniCssExtractPlugin({ + filename: "[name].css", + }), + ], + module: { + rules: [ + { + test: /\.css$/i, + rules: [ + { + loader: MiniCssExtractPlugin.loader, + }, + { + loader: path.resolve(__dirname, "../src"), + options: { sourceMap: false }, + }, + ], + }, ], - module: { - rules: [ - { - test: /\.css$/i, - rules: [ - { - loader: MiniCssExtractPlugin.loader, - }, - { - loader: path.resolve(__dirname, "../src"), - options: { sourceMap: false }, - }, - ], - }, - ], - }, - } - ); - const stats = await compile(compiler); - - expect(readAsset("main.css", compiler, stats)).toMatchSnapshot( - "extracted css" - ); - expect(getWarnings(stats)).toMatchSnapshot("warnings"); - expect(getErrors(stats)).toMatchSnapshot("errors"); - }); + }, + } + ); + const stats = await compile(compiler); + + expect(readAsset("main.css", compiler, stats)).toMatchSnapshot( + "extracted css" + ); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); });