Repository: webpack-contrib/style-loader (js)

Issue:
Does style-loader still takes sourceMap option?
<!-- Before creating an issue please make sure you are using the latest version of style-loader. -->

**Do you want to request a *feature* or report a *bug*?**

Supposedly, documentation bug.

From what I can see, `style-loader` adds source map whenever it is [present][1] these days. Regardless of the value of `sourceMap` option. If that's the case the documentation and validation needs to be updated.

[1]: https://github.com/webpack-contrib/style-loader/blob/v0.23.0/lib/addStyles.js#L386-L389

**What is the current behavior?**

With `sourceMap: false` validation passes and source map is added to css code.

**What is the expected behavior?**

Validation fails. Updated documentation.

Pull request:
fix: respect `sourceMap` option
<!--
  HOLY CRAP a Pull Request. We ❤️ those!

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  Please place an x (no spaces!) in all [ ] that apply
-->

This PR contains a:

- [x] **bugfix**
- [ ] new **feature**
- [ ] **code refactor**
- [x] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

respect `sourceMap` option

### Breaking Changes

No

### Additional Info

fixes #346

Patch:
diff --git a/src/runtime/addStyles.js b/src/runtime/addStyles.js
index 0c0b30e..aea7d22 100644
--- a/src/runtime/addStyles.js
+++ b/src/runtime/addStyles.js
@@ -304,7 +304,7 @@ function addStyle(obj, options) {
     remove = applyToSingletonTag.bind(null, style, styleIndex, true);
   } else {
     style = createStyleElement(options);
-    update = applyToTag.bind(null, style);
+    update = applyToTag.bind(null, style, options);
     remove = function() {
       removeStyleElement(style);
     };
@@ -363,7 +363,7 @@ function applyToSingletonTag(style, index, remove, obj) {
   }
 }
 
-function applyToTag(style, obj) {
+function applyToTag(style, options, obj) {
   var css = obj.css;
   var media = obj.media;
   var sourceMap = obj.sourceMap;
@@ -372,7 +372,7 @@ function applyToTag(style, obj) {
     style.setAttribute('media', media);
   }
 
-  if (sourceMap) {
+  if (options.sourceMap && sourceMap) {
     css +=
       '\n/*# sourceURL=' +
       sourceMap.sources[0] +


Test patch:
diff --git a/test/runtime/__snapshots__/addStyle.test.js.snap b/test/runtime/__snapshots__/addStyle.test.js.snap
index c463508..88d3ed9 100644
--- a/test/runtime/__snapshots__/addStyle.test.js.snap
+++ b/test/runtime/__snapshots__/addStyle.test.js.snap
@@ -43,12 +43,16 @@ exports[`addStyle should work with "insertInto" option #3 1`] = `"<head><title>T
 
 exports[`addStyle should work with "insertInto" option 1`] = `"<head><title>Title</title><style type=\\"text/css\\">.foo { color: red }</style><style type=\\"text/css\\">.bar { color: blue }</style></head><body><h1>Hello world</h1></body>"`;
 
-exports[`addStyle should work with "sourceMap" option 1`] = `
+exports[`addStyle should work with "sourceMap" option #2 1`] = `
 "<head><title>Title</title><style type=\\"text/css\\">.foo { color: red }
-/*# sourceURL=style-1.css */
-/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLTEuY3NzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQXFCLGVBQWUsRUFBRSIsImZpbGUiOiJzdHlsZS0xLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIi5mb28geyBjb2xvcjogcmVkIH0iXX0= */</style></head><body><h1>Hello world</h1></body>"
+/*# sourceURL=style-13-2.css */
+/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLTEzLTIuY3NzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQXFCLGVBQWUsRUFBRSIsImZpbGUiOiJzdHlsZS0xMy0yLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIi5mb28geyBjb2xvcjogcmVkIH0iXX0= */</style></head><body><h1>Hello world</h1></body>"
 `;
 
+exports[`addStyle should work with "sourceMap" option #3 1`] = `"<head><title>Title</title><style type=\\"text/css\\">.foo { color: red }</style></head><body><h1>Hello world</h1></body>"`;
+
+exports[`addStyle should work with "sourceMap" option 1`] = `"<head><title>Title</title><style type=\\"text/css\\">.foo { color: red }</style></head><body><h1>Hello world</h1></body>"`;
+
 exports[`addStyle should work with "transform" option #2 1`] = `"<head><title>Title</title><style type=\\"text/css\\">.foo { color: yellow }</style></head><body><h1>Hello world</h1></body>"`;
 
 exports[`addStyle should work with "transform" option #3 1`] = `"<head><title>Title</title></head><body><h1>Hello world</h1></body>"`;
@@ -101,29 +105,15 @@ exports[`addStyle should work with updates #11 1`] = `"<head><title>Title</title
 
 exports[`addStyle should work with updates #11 2`] = `"<head><title>Title</title></head><body><h1>Hello world</h1></body>"`;
 
-exports[`addStyle should work with updates #12 1`] = `
-"<head><title>Title</title><style type=\\"text/css\\">.foo { color: red }
-/*# sourceURL=style-40.css */
-/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLTQwLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFxQixlQUFlLEVBQUUiLCJmaWxlIjoic3R5bGUtNDAuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiLmZvbyB7IGNvbG9yOiByZWQgfSJdfQ== */</style></head><body><h1>Hello world</h1></body>"
-`;
+exports[`addStyle should work with updates #12 1`] = `"<head><title>Title</title><style type=\\"text/css\\">.foo { color: red }</style></head><body><h1>Hello world</h1></body>"`;
 
-exports[`addStyle should work with updates #12 2`] = `
-"<head><title>Title</title><style type=\\"text/css\\">.foo { color: black }
-/*# sourceURL=style-40.css */
-/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLTQwLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQkJCQixjQUFxQixlQUFlLEVBQUUiLCJmaWxlIjoic3R5bGUtNDAuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiLmZvbyB7IGNvbG9yOiBibGFjayB9Il19 */</style></head><body><h1>Hello world</h1></body>"
-`;
+exports[`addStyle should work with updates #12 2`] = `"<head><title>Title</title><style type=\\"text/css\\">.foo { color: black }</style></head><body><h1>Hello world</h1></body>"`;
 
 exports[`addStyle should work with updates #13 1`] = `"<head><title>Title</title><style type=\\"text/css\\" media=\\"screen and (min-width:320px)\\">.foo { color: red }</style></head><body><h1>Hello world</h1></body>"`;
 
 exports[`addStyle should work with updates #13 2`] = `"<head><title>Title</title><style type=\\"text/css\\" media=\\"screen and (min-width:640px)\\">.foo { color: black }</style></head><body><h1>Hello world</h1></body>"`;
 
-exports[`addStyle should work with updates #14 1`] = `
-"<head><title>Title</title><style type=\\"text/css\\">.foo { color: red }
-/*# sourceURL=style-42.css */
-/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLTQyLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFxQixlQUFlLEVBQUUiLCJmaWxlIjoic3R5bGUtNDIuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiLmZvbyB7IGNvbG9yOiByZWQgfSJdfQ== */</style><style type=\\"text/css\\">.bar { color: yellow }
-/*# sourceURL=style-43.css */
-/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLTQzLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFxQixlQUFlLEVBQUUiLCJmaWxlIjoic3R5bGUtNDMuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiLmZvbyB7IGNvbG9yOiByZWQgfSJdfQ== */</style></head><body><h1>Hello world</h1></body>"
-`;
+exports[`addStyle should work with updates #14 1`] = `"<head><title>Title</title><style type=\\"text/css\\">.foo { color: red }</style><style type=\\"text/css\\">.bar { color: yellow }</style></head><body><h1>Hello world</h1></body>"`;
 
 exports[`addStyle should work with updates #14 2`] = `"<head><title>Title</title><style type=\\"text/css\\">.foo { color: black }</style><style type=\\"text/css\\">.bar { color: gray }</style></head><body><h1>Hello world</h1></body>"`;
 
diff --git a/test/runtime/addStyle.test.js b/test/runtime/addStyle.test.js
index 9a2029d..f801abe 100644
--- a/test/runtime/addStyle.test.js
+++ b/test/runtime/addStyle.test.js
@@ -154,15 +154,15 @@ describe('addStyle', () => {
   it('should work with "sourceMap" option', () => {
     addStyle([
       [
-        './style-13.css',
+        './style-13-1.css',
         '.foo { color: red }',
         '',
         {
           version: 3,
-          sources: ['style-1.css'],
+          sources: ['style-13-1.css'],
           names: [],
           mappings: 'AAAA,cAAqB,eAAe,EAAE',
-          file: 'style-1.css',
+          file: 'style-13-1.css',
           sourcesContent: ['.foo { color: red }'],
         },
       ],
@@ -171,6 +171,56 @@ describe('addStyle', () => {
     expect(document.documentElement.innerHTML).toMatchSnapshot();
   });
 
+  it('should work with "sourceMap" option #2', () => {
+    addStyle(
+      [
+        [
+          './style-13-2.css',
+          '.foo { color: red }',
+          '',
+          {
+            version: 3,
+            sources: ['style-13-2.css'],
+            names: [],
+            mappings: 'AAAA,cAAqB,eAAe,EAAE',
+            file: 'style-13-2.css',
+            sourcesContent: ['.foo { color: red }'],
+          },
+        ],
+      ],
+      {
+        sourceMap: true,
+      }
+    );
+
+    expect(document.documentElement.innerHTML).toMatchSnapshot();
+  });
+
+  it('should work with "sourceMap" option #3', () => {
+    addStyle(
+      [
+        [
+          './style-13-3.css',
+          '.foo { color: red }',
+          '',
+          {
+            version: 3,
+            sources: ['style-13-3.css'],
+            names: [],
+            mappings: 'AAAA,cAAqB,eAAe,EAAE',
+            file: 'style-13-3.css',
+            sourcesContent: ['.foo { color: red }'],
+          },
+        ],
+      ],
+      {
+        sourceMap: false,
+      }
+    );
+
+    expect(document.documentElement.innerHTML).toMatchSnapshot();
+  });
+
   it('should work with nonce', () => {
     // eslint-disable-next-line no-underscore-dangle
     window.__webpack_nonce__ = 'none';